Refactor types, add a Request trait

pull/574/head
Blaž Hrastnik 3 years ago
parent 2d1ae2e44b
commit 3a9e1c305b

@ -3,8 +3,8 @@ use crate::{
types::*, types::*,
Result, Result,
}; };
use log::{error, info}; pub use log::{error, info};
use serde::{Deserialize, Serialize}; use serde::Serialize;
use serde_json::{from_value, to_value, Value}; use serde_json::{from_value, to_value, Value};
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -208,7 +208,7 @@ impl Client {
} }
pub async fn initialize(&mut self, adapter_id: String) -> Result<()> { pub async fn initialize(&mut self, adapter_id: String) -> Result<()> {
let args = InitializeArguments { let args = requests::InitializeArguments {
client_id: Some("hx".to_owned()), client_id: Some("hx".to_owned()),
client_name: Some("helix".to_owned()), client_name: Some("helix".to_owned()),
adapter_id, adapter_id,
@ -262,7 +262,7 @@ impl Client {
file: String, file: String,
breakpoints: Vec<SourceBreakpoint>, breakpoints: Vec<SourceBreakpoint>,
) -> Result<Option<Vec<Breakpoint>>> { ) -> Result<Option<Vec<Breakpoint>>> {
let args = SetBreakpointsArguments { let args = requests::SetBreakpointsArguments {
source: Source { source: Source {
path: Some(file), path: Some(file),
name: None, name: None,
@ -280,7 +280,7 @@ impl Client {
let response = self let response = self
.request("setBreakpoints".to_owned(), to_value(args).ok()) .request("setBreakpoints".to_owned(), to_value(args).ok())
.await?; .await?;
let body: Option<SetBreakpointsResponseBody> = from_value(response.body.unwrap()).ok(); let body: Option<requests::SetBreakpointsResponse> = from_value(response.body.unwrap()).ok();
Ok(body.map(|b| b.breakpoints).unwrap()) Ok(body.map(|b| b.breakpoints).unwrap())
} }
@ -291,13 +291,13 @@ impl Client {
} }
pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> { pub async fn continue_thread(&mut self, thread_id: usize) -> Result<Option<bool>> {
let args = ContinueArguments { thread_id }; let args = requests::ContinueArguments { thread_id };
let response = self let response = self
.request("continue".to_owned(), to_value(args).ok()) .request("continue".to_owned(), to_value(args).ok())
.await?; .await?;
let body: Option<ContinueResponseBody> = from_value(response.body.unwrap()).ok(); let body: Option<requests::ContinueResponse> = from_value(response.body.unwrap()).ok();
Ok(body.map(|b| b.all_threads_continued).unwrap()) Ok(body.map(|b| b.all_threads_continued).unwrap())
} }
@ -306,7 +306,7 @@ impl Client {
&mut self, &mut self,
thread_id: usize, thread_id: usize,
) -> Result<(Vec<StackFrame>, Option<usize>)> { ) -> Result<(Vec<StackFrame>, Option<usize>)> {
let args = StackTraceArguments { let args = requests::StackTraceArguments {
thread_id, thread_id,
start_frame: None, start_frame: None,
levels: None, levels: None,
@ -317,7 +317,7 @@ impl Client {
.request("stackTrace".to_owned(), to_value(args).ok()) .request("stackTrace".to_owned(), to_value(args).ok())
.await?; .await?;
let body: StackTraceResponseBody = from_value(response.body.unwrap()).unwrap(); let body: requests::StackTraceResponse = from_value(response.body.unwrap()).unwrap();
Ok((body.stack_frames, body.total_frames)) Ok((body.stack_frames, body.total_frames))
} }
@ -325,25 +325,25 @@ impl Client {
pub async fn threads(&mut self) -> Result<Vec<Thread>> { pub async fn threads(&mut self) -> Result<Vec<Thread>> {
let response = self.request("threads".to_owned(), None).await?; let response = self.request("threads".to_owned(), None).await?;
let body: ThreadsResponseBody = from_value(response.body.unwrap()).unwrap(); let body: requests::ThreadsResponse = from_value(response.body.unwrap()).unwrap();
Ok(body.threads) Ok(body.threads)
} }
pub async fn scopes(&mut self, frame_id: usize) -> Result<Vec<Scope>> { pub async fn scopes(&mut self, frame_id: usize) -> Result<Vec<Scope>> {
let args = ScopesArguments { frame_id }; let args = requests::ScopesArguments { frame_id };
let response = self let response = self
.request("scopes".to_owned(), to_value(args).ok()) .request("scopes".to_owned(), to_value(args).ok())
.await?; .await?;
let body: ScopesResponseBody = from_value(response.body.unwrap()).unwrap(); let body: requests::ScopesResponse = from_value(response.body.unwrap()).unwrap();
Ok(body.scopes) Ok(body.scopes)
} }
pub async fn variables(&mut self, variables_reference: usize) -> Result<Vec<Variable>> { pub async fn variables(&mut self, variables_reference: usize) -> Result<Vec<Variable>> {
let args = VariablesArguments { let args = requests::VariablesArguments {
variables_reference, variables_reference,
filter: None, filter: None,
start: None, start: None,
@ -355,7 +355,7 @@ impl Client {
.request("variables".to_owned(), to_value(args).ok()) .request("variables".to_owned(), to_value(args).ok())
.await?; .await?;
let body: VariablesResponseBody = from_value(response.body.unwrap()).unwrap(); let body: requests::VariablesResponse = from_value(response.body.unwrap()).unwrap();
Ok(body.variables) Ok(body.variables)
} }

@ -3,7 +3,7 @@ mod transport;
mod types; mod types;
pub use client::Client; pub use client::Client;
pub use transport::{Event, Payload, Request, Response, Transport}; pub use transport::{Event, Payload, Response, Transport};
pub use types::*; pub use types::*;
use thiserror::Error; use thiserror::Error;

@ -1,6 +1,12 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
pub trait Request {
type Arguments: serde::de::DeserializeOwned + serde::Serialize;
type Result: serde::de::DeserializeOwned + serde::Serialize;
const COMMAND: &'static str;
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ColumnDescriptor { pub struct ColumnDescriptor {
@ -74,28 +80,6 @@ impl std::ops::Deref for DebuggerCapabilities {
} }
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeArguments {
#[serde(rename = "clientID")]
pub client_id: Option<String>,
pub client_name: Option<String>,
#[serde(rename = "adapterID")]
pub adapter_id: String,
pub locale: Option<String>,
#[serde(rename = "linesStartAt1")]
pub lines_start_at_one: Option<bool>,
#[serde(rename = "columnsStartAt1")]
pub columns_start_at_one: Option<bool>,
pub path_format: Option<String>,
pub supports_variable_type: Option<bool>,
pub supports_variable_paging: Option<bool>,
pub supports_run_in_terminal_request: Option<bool>,
pub supports_memory_references: Option<bool>,
pub supports_progress_reporting: Option<bool>,
pub supports_invalidated_event: Option<bool>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Checksum { pub struct Checksum {
@ -126,15 +110,6 @@ pub struct SourceBreakpoint {
pub log_message: Option<String>, pub log_message: Option<String>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetBreakpointsArguments {
pub source: Source,
pub breakpoints: Option<Vec<SourceBreakpoint>>,
// lines is deprecated
pub source_modified: Option<bool>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Breakpoint { pub struct Breakpoint {
@ -150,24 +125,6 @@ pub struct Breakpoint {
pub offset: Option<usize>, pub offset: Option<usize>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetBreakpointsResponseBody {
pub breakpoints: Option<Vec<Breakpoint>>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueArguments {
pub thread_id: usize,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueResponseBody {
pub all_threads_continued: Option<bool>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StackFrameFormat { pub struct StackFrameFormat {
@ -180,15 +137,6 @@ pub struct StackFrameFormat {
pub include_all: Option<bool>, pub include_all: Option<bool>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StackTraceArguments {
pub thread_id: usize,
pub start_frame: Option<usize>,
pub levels: Option<usize>,
pub format: Option<StackFrameFormat>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StackFrame { pub struct StackFrame {
@ -205,13 +153,6 @@ pub struct StackFrame {
pub presentation_hint: Option<String>, pub presentation_hint: Option<String>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StackTraceResponseBody {
pub total_frames: Option<usize>,
pub stack_frames: Vec<StackFrame>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Thread { pub struct Thread {
@ -219,18 +160,6 @@ pub struct Thread {
pub name: String, pub name: String,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreadsResponseBody {
pub threads: Vec<Thread>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopesArguments {
pub frame_id: usize,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Scope { pub struct Scope {
@ -247,28 +176,12 @@ pub struct Scope {
pub end_column: Option<usize>, pub end_column: Option<usize>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopesResponseBody {
pub scopes: Vec<Scope>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ValueFormat { pub struct ValueFormat {
pub hex: Option<bool>, pub hex: Option<bool>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VariablesArguments {
pub variables_reference: usize,
pub filter: Option<String>,
pub start: Option<usize>,
pub count: Option<usize>,
pub format: Option<ValueFormat>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct VariablePresentationHint { pub struct VariablePresentationHint {
@ -292,15 +205,169 @@ pub struct Variable {
pub memory_reference: Option<String>, pub memory_reference: Option<String>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] pub mod requests {
#[serde(rename_all = "camelCase")] use super::*;
pub struct VariablesResponseBody { #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct InitializeArguments {
#[serde(rename = "clientID")]
pub client_id: Option<String>,
pub client_name: Option<String>,
#[serde(rename = "adapterID")]
pub adapter_id: String,
pub locale: Option<String>,
#[serde(rename = "linesStartAt1")]
pub lines_start_at_one: Option<bool>,
#[serde(rename = "columnsStartAt1")]
pub columns_start_at_one: Option<bool>,
pub path_format: Option<String>,
pub supports_variable_type: Option<bool>,
pub supports_variable_paging: Option<bool>,
pub supports_run_in_terminal_request: Option<bool>,
pub supports_memory_references: Option<bool>,
pub supports_progress_reporting: Option<bool>,
pub supports_invalidated_event: Option<bool>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetBreakpointsArguments {
pub source: Source,
pub breakpoints: Option<Vec<SourceBreakpoint>>,
// lines is deprecated
pub source_modified: Option<bool>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SetBreakpointsResponse {
pub breakpoints: Option<Vec<Breakpoint>>,
}
#[derive(Debug)]
pub enum SetBreakpoints {}
impl Request for SetBreakpoints {
type Arguments = SetBreakpointsArguments;
type Result = SetBreakpointsResponse;
const COMMAND: &'static str = "setBreakpoints";
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueArguments {
pub thread_id: usize,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ContinueResponse {
pub all_threads_continued: Option<bool>,
}
#[derive(Debug)]
pub enum Continue {}
impl Request for Continue {
type Arguments = ContinueArguments;
type Result = ContinueResponse;
const COMMAND: &'static str = "continue";
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StackTraceArguments {
pub thread_id: usize,
pub start_frame: Option<usize>,
pub levels: Option<usize>,
pub format: Option<StackFrameFormat>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StackTraceResponse {
pub total_frames: Option<usize>,
pub stack_frames: Vec<StackFrame>,
}
#[derive(Debug)]
pub enum StackTrace {}
impl Request for StackTrace {
type Arguments = StackTraceArguments;
type Result = StackTraceResponse;
const COMMAND: &'static str = "stackTrace";
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ThreadsResponse {
pub threads: Vec<Thread>,
}
#[derive(Debug)]
pub enum Threads {}
impl Request for Threads {
type Arguments = ();
type Result = ThreadsResponse;
const COMMAND: &'static str = "threads";
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopesArguments {
pub frame_id: usize,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ScopesResponse {
pub scopes: Vec<Scope>,
}
#[derive(Debug)]
pub enum Scopes {}
impl Request for Scopes {
type Arguments = ScopesArguments;
type Result = ScopesResponse;
const COMMAND: &'static str = "scopes";
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VariablesArguments {
pub variables_reference: usize,
pub filter: Option<String>,
pub start: Option<usize>,
pub count: Option<usize>,
pub format: Option<ValueFormat>,
}
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VariablesResponse {
pub variables: Vec<Variable>, pub variables: Vec<Variable>,
}
#[derive(Debug)]
pub enum Variables {}
impl Request for Variables {
type Arguments = VariablesArguments;
type Result = VariablesResponse;
const COMMAND: &'static str = "scopes";
}
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] // Events
#[serde(rename_all = "camelCase")]
pub struct OutputEventBody { pub mod events {
use super::*;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Output {
pub output: String, pub output: String,
pub category: Option<String>, pub category: Option<String>,
pub group: Option<String>, pub group: Option<String>,
@ -309,11 +376,11 @@ pub struct OutputEventBody {
pub variables_reference: Option<usize>, pub variables_reference: Option<usize>,
pub source: Option<Source>, pub source: Option<Source>,
pub data: Option<Value>, pub data: Option<Value>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StoppedEventBody { pub struct Stopped {
pub reason: String, pub reason: String,
pub description: Option<String>, pub description: Option<String>,
pub thread_id: Option<usize>, pub thread_id: Option<usize>,
@ -321,4 +388,5 @@ pub struct StoppedEventBody {
pub text: Option<String>, pub text: Option<String>,
pub all_threads_stopped: Option<bool>, pub all_threads_stopped: Option<bool>,
pub hit_breakpoint_ids: Option<Vec<usize>>, pub hit_breakpoint_ids: Option<Vec<usize>>,
}
} }

Loading…
Cancel
Save