diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-11-05 23:58:59 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-05 22:58:59 +0000 |
commit | fdb4953ea460d5c09ac73f3f37dd570d44893155 (patch) | |
tree | 72c0e9dd7dc7b2677eea474d7e443e8a66ba0cc7 /cli/tools | |
parent | 68a964346d1b4f0509d244c7b13e54146817238f (diff) |
refactor: unify CDP types in a single module (#21094)
This commit moves all Chrome Devtools Protocol messages to `cli/cdp.rs`
and refactors all places using these types to pull them from a common
place.
No functional changes.
Diffstat (limited to 'cli/tools')
-rw-r--r-- | cli/tools/coverage/json_types.rs | 60 | ||||
-rw-r--r-- | cli/tools/coverage/merge.rs | 169 | ||||
-rw-r--r-- | cli/tools/coverage/mod.rs | 27 | ||||
-rw-r--r-- | cli/tools/coverage/range_tree.rs | 14 | ||||
-rw-r--r-- | cli/tools/jupyter/server.rs | 4 | ||||
-rw-r--r-- | cli/tools/repl/cdp.rs | 426 | ||||
-rw-r--r-- | cli/tools/repl/editor.rs | 2 | ||||
-rw-r--r-- | cli/tools/repl/mod.rs | 1 | ||||
-rw-r--r-- | cli/tools/repl/session.rs | 3 | ||||
-rw-r--r-- | cli/tools/run/hmr.rs (renamed from cli/tools/run/hmr/mod.rs) | 47 | ||||
-rw-r--r-- | cli/tools/run/hmr/json_types.rs | 59 |
11 files changed, 146 insertions, 666 deletions
diff --git a/cli/tools/coverage/json_types.rs b/cli/tools/coverage/json_types.rs deleted file mode 100644 index d5c6591f7..000000000 --- a/cli/tools/coverage/json_types.rs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -use serde::Deserialize; -use serde::Serialize; - -#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)] -#[serde(rename_all = "camelCase")] -pub struct CoverageRange { - /// Start character index. - #[serde(rename = "startOffset")] - pub start_char_offset: usize, - /// End character index. - #[serde(rename = "endOffset")] - pub end_char_offset: usize, - pub count: i64, -} - -#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)] -#[serde(rename_all = "camelCase")] -pub struct FunctionCoverage { - pub function_name: String, - pub ranges: Vec<CoverageRange>, - pub is_block_coverage: bool, -} - -#[derive(Debug, Eq, PartialEq, Serialize, Deserialize, Clone)] -#[serde(rename_all = "camelCase")] -pub struct ScriptCoverage { - pub script_id: String, - pub url: String, - pub functions: Vec<FunctionCoverage>, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StartPreciseCoverageParameters { - pub call_count: bool, - pub detailed: bool, - pub allow_triggered_updates: bool, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StartPreciseCoverageReturnObject { - pub timestamp: f64, -} - -#[derive(Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct TakePreciseCoverageReturnObject { - pub result: Vec<ScriptCoverage>, - pub timestamp: f64, -} - -// TODO(bartlomieju): remove me -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ProcessCoverage { - pub result: Vec<ScriptCoverage>, -} diff --git a/cli/tools/coverage/merge.rs b/cli/tools/coverage/merge.rs index c7f9b1524..a15f4b552 100644 --- a/cli/tools/coverage/merge.rs +++ b/cli/tools/coverage/merge.rs @@ -3,24 +3,26 @@ // Forked from https://github.com/demurgos/v8-coverage/tree/d0ca18da8740198681e0bc68971b0a6cdb11db3e/rust // Copyright 2021 Charles Samborski. All rights reserved. MIT license. -use super::json_types::CoverageRange; -use super::json_types::FunctionCoverage; -use super::json_types::ProcessCoverage; -use super::json_types::ScriptCoverage; use super::range_tree::RangeTree; use super::range_tree::RangeTreeArena; +use crate::cdp; use std::collections::BTreeMap; use std::collections::BTreeSet; use std::collections::HashMap; use std::iter::Peekable; +#[derive(Eq, PartialEq, Clone, Debug)] +pub struct ProcessCoverage { + pub result: Vec<cdp::ScriptCoverage>, +} + pub fn merge_processes( mut processes: Vec<ProcessCoverage>, ) -> Option<ProcessCoverage> { if processes.len() <= 1 { return processes.pop(); } - let mut url_to_scripts: BTreeMap<String, Vec<ScriptCoverage>> = + let mut url_to_scripts: BTreeMap<String, Vec<cdp::ScriptCoverage>> = BTreeMap::new(); for process_cov in processes { for script_cov in process_cov.result { @@ -31,12 +33,13 @@ pub fn merge_processes( } } - let result: Vec<ScriptCoverage> = url_to_scripts + let result: Vec<cdp::ScriptCoverage> = url_to_scripts .into_iter() .enumerate() .map(|(script_id, (_, scripts))| (script_id, scripts)) .map(|(script_id, scripts)| { - let mut merged: ScriptCoverage = merge_scripts(scripts.to_vec()).unwrap(); + let mut merged: cdp::ScriptCoverage = + merge_scripts(scripts.to_vec()).unwrap(); merged.script_id = script_id.to_string(); merged }) @@ -46,21 +49,21 @@ pub fn merge_processes( } pub fn merge_scripts( - mut scripts: Vec<ScriptCoverage>, -) -> Option<ScriptCoverage> { + mut scripts: Vec<cdp::ScriptCoverage>, +) -> Option<cdp::ScriptCoverage> { if scripts.len() <= 1 { return scripts.pop(); } let (script_id, url) = { - let first: &ScriptCoverage = &scripts[0]; + let first: &cdp::ScriptCoverage = &scripts[0]; (first.script_id.clone(), first.url.clone()) }; - let mut range_to_funcs: BTreeMap<CharRange, Vec<FunctionCoverage>> = + let mut range_to_funcs: BTreeMap<CharRange, Vec<cdp::FunctionCoverage>> = BTreeMap::new(); for script_cov in scripts { for func_cov in script_cov.functions { let root_range = { - let root_range_cov: &CoverageRange = &func_cov.ranges[0]; + let root_range_cov: &cdp::CoverageRange = &func_cov.ranges[0]; CharRange { start: root_range_cov.start_char_offset, end: root_range_cov.end_char_offset, @@ -70,12 +73,12 @@ pub fn merge_scripts( } } - let functions: Vec<FunctionCoverage> = range_to_funcs + let functions: Vec<cdp::FunctionCoverage> = range_to_funcs .into_values() .map(|funcs| merge_functions(funcs).unwrap()) .collect(); - Some(ScriptCoverage { + Some(cdp::ScriptCoverage { script_id, url, functions, @@ -105,8 +108,8 @@ impl PartialOrd for CharRange { } pub fn merge_functions( - mut funcs: Vec<FunctionCoverage>, -) -> Option<FunctionCoverage> { + mut funcs: Vec<cdp::FunctionCoverage>, +) -> Option<cdp::FunctionCoverage> { if funcs.len() <= 1 { return funcs.pop(); } @@ -124,7 +127,7 @@ pub fn merge_functions( let ranges = merged.to_ranges(); let is_block_coverage: bool = !(ranges.len() == 1 && ranges[0].count == 0); - Some(FunctionCoverage { + Some(cdp::FunctionCoverage { function_name, ranges, is_block_coverage, @@ -439,13 +442,13 @@ mod tests { fn two_flat_trees() { let inputs: Vec<ProcessCoverage> = vec![ ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, - ranges: vec![CoverageRange { + ranges: vec![cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 1, @@ -454,13 +457,13 @@ mod tests { }], }, ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, - ranges: vec![CoverageRange { + ranges: vec![cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 2, @@ -470,13 +473,13 @@ mod tests { }, ]; let expected: Option<ProcessCoverage> = Some(ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, - ranges: vec![CoverageRange { + ranges: vec![cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 3, @@ -492,19 +495,19 @@ mod tests { fn two_trees_with_matching_children() { let inputs: Vec<ProcessCoverage> = vec![ ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 10, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 3, end_char_offset: 6, count: 1, @@ -514,19 +517,19 @@ mod tests { }], }, ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 20, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 3, end_char_offset: 6, count: 2, @@ -537,19 +540,19 @@ mod tests { }, ]; let expected: Option<ProcessCoverage> = Some(ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 30, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 3, end_char_offset: 6, count: 3, @@ -566,19 +569,19 @@ mod tests { fn two_trees_with_partially_overlapping_children() { let inputs: Vec<ProcessCoverage> = vec![ ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 10, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 2, end_char_offset: 5, count: 1, @@ -588,19 +591,19 @@ mod tests { }], }, ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 20, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 4, end_char_offset: 7, count: 2, @@ -611,29 +614,29 @@ mod tests { }, ]; let expected: Option<ProcessCoverage> = Some(ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 30, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 2, end_char_offset: 5, count: 21, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 4, end_char_offset: 5, count: 3, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 5, end_char_offset: 7, count: 12, @@ -650,29 +653,29 @@ mod tests { fn two_trees_with_with_complementary_children_summing_to_the_same_count() { let inputs: Vec<ProcessCoverage> = vec![ ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 1, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 8, count: 6, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 5, count: 5, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 5, end_char_offset: 8, count: 7, @@ -682,29 +685,29 @@ mod tests { }], }, ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 4, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 8, count: 8, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 5, count: 9, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 5, end_char_offset: 8, count: 7, @@ -715,19 +718,19 @@ mod tests { }, ]; let expected: Option<ProcessCoverage> = Some(ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 5, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 8, count: 14, @@ -744,19 +747,19 @@ mod tests { fn merges_a_similar_sliding_chain_a_bc() { let inputs: Vec<ProcessCoverage> = vec![ ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 7, count: 10, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 4, count: 1, @@ -766,24 +769,24 @@ mod tests { }], }, ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 7, count: 20, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 6, count: 11, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 2, end_char_offset: 5, count: 2, @@ -794,29 +797,29 @@ mod tests { }, ]; let expected: Option<ProcessCoverage> = Some(ProcessCoverage { - result: vec![ScriptCoverage { + result: vec![cdp::ScriptCoverage { script_id: String::from("0"), url: String::from("/lib.js"), - functions: vec![FunctionCoverage { + functions: vec![cdp::FunctionCoverage { function_name: String::from("lib"), is_block_coverage: true, ranges: vec![ - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 7, count: 30, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 0, end_char_offset: 6, count: 21, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 1, end_char_offset: 5, count: 12, }, - CoverageRange { + cdp::CoverageRange { start_char_offset: 2, end_char_offset: 4, count: 3, diff --git a/cli/tools/coverage/mod.rs b/cli/tools/coverage/mod.rs index 67566d811..cd4afe360 100644 --- a/cli/tools/coverage/mod.rs +++ b/cli/tools/coverage/mod.rs @@ -3,6 +3,7 @@ use crate::args::CoverageFlags; use crate::args::FileFlags; use crate::args::Flags; +use crate::cdp; use crate::colors; use crate::factory::CliFactory; use crate::npm::CliNpmResolver; @@ -34,11 +35,9 @@ use std::path::PathBuf; use text_lines::TextLines; use uuid::Uuid; -mod json_types; mod merge; mod range_tree; - -use json_types::*; +use merge::ProcessCoverage; pub struct CoverageCollector { pub dir: PathBuf, @@ -84,8 +83,8 @@ impl CoverageCollector { async fn start_precise_coverage( &mut self, - parameters: StartPreciseCoverageParameters, - ) -> Result<StartPreciseCoverageReturnObject, AnyError> { + parameters: cdp::StartPreciseCoverageArgs, + ) -> Result<cdp::StartPreciseCoverageResponse, AnyError> { let return_value = self .session .post_message("Profiler.startPreciseCoverage", Some(parameters)) @@ -98,7 +97,7 @@ impl CoverageCollector { async fn take_precise_coverage( &mut self, - ) -> Result<TakePreciseCoverageReturnObject, AnyError> { + ) -> Result<cdp::TakePreciseCoverageResponse, AnyError> { let return_value = self .session .post_message::<()>("Profiler.takePreciseCoverage", None) @@ -113,7 +112,7 @@ impl CoverageCollector { self.enable_debugger().await?; self.enable_profiler().await?; self - .start_precise_coverage(StartPreciseCoverageParameters { + .start_precise_coverage(cdp::StartPreciseCoverageArgs { call_count: true, detailed: true, allow_triggered_updates: false, @@ -180,7 +179,7 @@ struct CoverageReport { } fn generate_coverage_report( - script_coverage: &ScriptCoverage, + script_coverage: &cdp::ScriptCoverage, script_source: String, maybe_source_map: &Option<Vec<u8>>, output: &Option<PathBuf>, @@ -570,8 +569,8 @@ impl CoverageReporter for PrettyCoverageReporter { fn collect_coverages( files: FileFlags, -) -> Result<Vec<ScriptCoverage>, AnyError> { - let mut coverages: Vec<ScriptCoverage> = Vec::new(); +) -> Result<Vec<cdp::ScriptCoverage>, AnyError> { + let mut coverages: Vec<cdp::ScriptCoverage> = Vec::new(); let file_paths = FileCollector::new(|file_path| { file_path .extension() @@ -590,7 +589,7 @@ fn collect_coverages( for file_path in file_paths { let json = fs::read_to_string(file_path.as_path())?; - let new_coverage: ScriptCoverage = serde_json::from_str(&json)?; + let new_coverage: cdp::ScriptCoverage = serde_json::from_str(&json)?; coverages.push(new_coverage); } @@ -600,11 +599,11 @@ fn collect_coverages( } fn filter_coverages( - coverages: Vec<ScriptCoverage>, + coverages: Vec<cdp::ScriptCoverage>, include: Vec<String>, exclude: Vec<String>, npm_resolver: &dyn CliNpmResolver, -) -> Vec<ScriptCoverage> { +) -> Vec<cdp::ScriptCoverage> { let include: Vec<Regex> = include.iter().map(|e| Regex::new(e).unwrap()).collect(); @@ -629,7 +628,7 @@ fn filter_coverages( (include.is_empty() || is_included) && !is_excluded && !is_internal }) - .collect::<Vec<ScriptCoverage>>() + .collect::<Vec<cdp::ScriptCoverage>>() } pub async fn cover_files( diff --git a/cli/tools/coverage/range_tree.rs b/cli/tools/coverage/range_tree.rs index c3bba7942..c873b2f8e 100644 --- a/cli/tools/coverage/range_tree.rs +++ b/cli/tools/coverage/range_tree.rs @@ -3,7 +3,7 @@ // Forked from https://github.com/demurgos/v8-coverage/tree/d0ca18da8740198681e0bc68971b0a6cdb11db3e/rust // Copyright 2021 Charles Samborski. All rights reserved. MIT license. -use super::json_types::CoverageRange; +use crate::cdp; use std::iter::Peekable; use typed_arena::Arena; @@ -126,12 +126,12 @@ impl<'rt> RangeTree<'rt> { tree } - pub fn to_ranges(&self) -> Vec<CoverageRange> { - let mut ranges: Vec<CoverageRange> = Vec::new(); + pub fn to_ranges(&self) -> Vec<cdp::CoverageRange> { + let mut ranges: Vec<cdp::CoverageRange> = Vec::new(); let mut stack: Vec<(&RangeTree, i64)> = vec![(self, 0)]; while let Some((cur, parent_count)) = stack.pop() { let count: i64 = parent_count + cur.delta; - ranges.push(CoverageRange { + ranges.push(cdp::CoverageRange { start_char_offset: cur.start, end_char_offset: cur.end, count, @@ -145,7 +145,7 @@ impl<'rt> RangeTree<'rt> { pub fn from_sorted_ranges<'a>( rta: &'a RangeTreeArena<'a>, - ranges: &[CoverageRange], + ranges: &[cdp::CoverageRange], ) -> Option<&'a mut RangeTree<'a>> { Self::from_sorted_ranges_inner( rta, @@ -157,7 +157,7 @@ impl<'rt> RangeTree<'rt> { fn from_sorted_ranges_inner<'a, 'b, 'c: 'b>( rta: &'a RangeTreeArena<'a>, - ranges: &'b mut Peekable<impl Iterator<Item = &'c CoverageRange>>, + ranges: &'b mut Peekable<impl Iterator<Item = &'c cdp::CoverageRange>>, parent_end: usize, parent_count: i64, ) -> Option<&'a mut RangeTree<'a>> { @@ -190,7 +190,7 @@ mod tests { #[test] fn from_sorted_ranges_empty() { let rta = RangeTreeArena::new(); - let inputs: Vec<CoverageRange> = vec![CoverageRange { + let inputs: Vec<cdp::CoverageRange> = vec![cdp::CoverageRange { start_char_offset: 0, end_char_offset: 9, count: 1, diff --git a/cli/tools/jupyter/server.rs b/cli/tools/jupyter/server.rs index 6175a33e9..7a44fab64 100644 --- a/cli/tools/jupyter/server.rs +++ b/cli/tools/jupyter/server.rs @@ -8,8 +8,8 @@ use std::collections::HashMap; use std::rc::Rc; use std::sync::Arc; +use crate::cdp; use crate::tools::repl; -use crate::tools::repl::cdp; use deno_core::error::AnyError; use deno_core::futures; use deno_core::serde_json; @@ -382,7 +382,7 @@ impl JupyterServer { } }; - let repl::cdp::EvaluateResponse { + let cdp::EvaluateResponse { result, exception_details, } = evaluate_response.value; diff --git a/cli/tools/repl/cdp.rs b/cli/tools/repl/cdp.rs deleted file mode 100644 index f90a1875e..000000000 --- a/cli/tools/repl/cdp.rs +++ /dev/null @@ -1,426 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -/// <https://chromedevtools.github.io/devtools-protocol/tot/> -use deno_core::serde_json; -use deno_core::serde_json::Value; -use serde::Deserialize; -use serde::Deserializer; -use serde::Serialize; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-awaitPromise> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct AwaitPromiseArgs { - pub promise_object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-awaitPromise> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct AwaitPromiseResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFunctionOnArgs { - pub function_declaration: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_id: Option<RemoteObjectId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub arguments: Option<Vec<CallArgument>>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub user_gesture: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] - pub throw_on_side_effect: Option<bool>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-callFunctionOn> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFunctionOnResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-compileScript> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CompileScriptArgs { - pub expression: String, - #[serde(rename = "sourceURL")] - pub source_url: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-compileScript> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CompileScriptResponse { - pub script_id: Option<ScriptId>, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct EvaluateArgs { - pub expression: String, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde( - rename = "includeCommandLineAPI", - skip_serializing_if = "Option::is_none" - )] - pub include_command_line_api: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub user_gesture: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub throw_on_side_effect: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub timeout: Option<TimeDelta>, - #[serde(skip_serializing_if = "Option::is_none")] - pub disable_breaks: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub repl_mode: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - #[serde(rename = "allowUnsafeEvalBlockedByCSP")] - pub allow_unsafe_eval_blocked_by_csp: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub unique_context_id: Option<String>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-evaluate> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct EvaluateResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-getProperties> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct GetPropertiesArgs { - pub object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub own_properties: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub accessor_properties_only: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub non_indexed_properties_only: Option<bool>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-getProperties> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GetPropertiesResponse { - pub result: Vec<PropertyDescriptor>, - pub internal_properties: Option<Vec<InternalPropertyDescriptor>>, - pub private_properties: Option<Vec<PrivatePropertyDescriptor>>, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-globalLexicalScopeNames> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct GlobalLexicalScopeNamesArgs { - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-globalLexicalScopeNames> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct GlobalLexicalScopeNamesResponse { - pub names: Vec<String>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-queryObjects> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct QueryObjectsArgs { - pub prototype_object_id: RemoteObjectId, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-queryObjects> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct QueryObjectsResponse { - pub objects: RemoteObject, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-releaseObject> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ReleaseObjectArgs { - pub object_id: RemoteObjectId, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-releaseObjectGroup> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct ReleaseObjectGroupArgs { - pub object_group: String, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-runScript> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct RunScriptArgs { - pub script_id: ScriptId, - #[serde(skip_serializing_if = "Option::is_none")] - pub execution_context_id: Option<ExecutionContextId>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_group: Option<String>, - #[serde(skip_serializing_if = "Option::is_none")] - pub silent: Option<bool>, - #[serde( - rename = "includeCommandLineAPI", - skip_serializing_if = "Option::is_none" - )] - pub include_command_line_api: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub return_by_value: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub generate_preview: Option<bool>, - #[serde(skip_serializing_if = "Option::is_none")] - pub await_promise: Option<bool>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-runScript> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct RunScriptResponse { - pub result: RemoteObject, - pub exception_details: Option<ExceptionDetails>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#method-setAsyncCallStackDepth> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct SetAsyncCallStackDepthArgs { - pub max_depth: u64, -} - -// types - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObject> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct RemoteObject { - #[serde(rename = "type")] - pub kind: String, - pub subtype: Option<String>, - pub class_name: Option<String>, - #[serde(default, deserialize_with = "deserialize_some")] - pub value: Option<Value>, - pub unserializable_value: Option<UnserializableValue>, - pub description: Option<String>, - pub object_id: Option<RemoteObjectId>, - pub preview: Option<ObjectPreview>, - pub custom_preview: Option<CustomPreview>, -} - -// Any value that is present is considered Some value, including null. -// ref: https://github.com/serde-rs/serde/issues/984#issuecomment-314143738 -fn deserialize_some<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error> -where - T: Deserialize<'de>, - D: Deserializer<'de>, -{ - Deserialize::deserialize(deserializer).map(Some) -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ObjectPreview> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ObjectPreview { - #[serde(rename = "type")] - pub kind: String, - pub subtype: Option<String>, - pub description: Option<String>, - pub overflow: bool, - pub properties: Vec<PropertyPreview>, - pub entries: Option<Vec<EntryPreview>>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-PropertyPreview> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PropertyPreview { - pub name: String, - #[serde(rename = "type")] - pub kind: String, - pub value: Option<String>, - pub value_preview: Option<ObjectPreview>, - pub subtype: Option<String>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-EntryPreview> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct EntryPreview { - pub key: Option<ObjectPreview>, - pub value: ObjectPreview, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CustomPreview> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CustomPreview { - pub header: String, - pub body_getter_id: RemoteObjectId, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExceptionDetails> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ExceptionDetails { - pub exception_id: u64, - pub text: String, - pub line_number: u64, - pub column_number: u64, - pub script_id: Option<ScriptId>, - pub url: Option<String>, - pub stack_trace: Option<StackTrace>, - pub exception: Option<RemoteObject>, - pub execution_context_id: Option<ExecutionContextId>, - pub exception_meta_data: Option<serde_json::Map<String, Value>>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-StackTrace> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StackTrace { - pub description: Option<String>, - pub call_frames: Vec<CallFrame>, - pub parent: Option<Box<StackTrace>>, - pub parent_id: Option<StackTraceId>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CallFrame> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct CallFrame { - pub function_name: String, - pub script_id: ScriptId, - pub url: String, - pub line_number: u64, - pub column_number: u64, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-StackTraceId> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct StackTraceId { - pub id: String, - pub debugger_id: Option<UniqueDebuggerId>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-CallArgument> -#[derive(Debug, Clone, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct CallArgument { - #[serde(skip_serializing_if = "Option::is_none")] - pub value: Option<Value>, - #[serde(skip_serializing_if = "Option::is_none")] - pub unserializable_value: Option<UnserializableValue>, - #[serde(skip_serializing_if = "Option::is_none")] - pub object_id: Option<RemoteObjectId>, -} - -impl From<&RemoteObject> for CallArgument { - fn from(obj: &RemoteObject) -> Self { - Self { - value: obj.value.clone(), - unserializable_value: obj.unserializable_value.clone(), - object_id: obj.object_id.clone(), - } - } -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-InternalPropertyDescriptor> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, - pub writable: Option<bool>, - pub get: Option<RemoteObject>, - pub set: Option<RemoteObject>, - pub configurable: bool, - pub enumerable: bool, - pub was_thrown: Option<bool>, - pub is_own: Option<bool>, - pub symbol: Option<RemoteObject>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-InternalPropertyDescriptor> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct InternalPropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-PrivatePropertyDescriptor> -#[derive(Debug, Clone, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct PrivatePropertyDescriptor { - pub name: String, - pub value: Option<RemoteObject>, - pub get: Option<RemoteObject>, - pub set: Option<RemoteObject>, -} - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-RemoteObjectId> -pub type RemoteObjectId = String; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ExecutionContextId> -pub type ExecutionContextId = u64; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-ScriptId> -pub type ScriptId = String; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-TimeDelta> -pub type TimeDelta = u64; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-UnserializableValue> -pub type UnserializableValue = String; - -/// <https://chromedevtools.github.io/devtools-protocol/tot/Runtime/#type-UniqueDebuggerId> -pub type UniqueDebuggerId = String; diff --git a/cli/tools/repl/editor.rs b/cli/tools/repl/editor.rs index 52fad4759..0bf2da7c8 100644 --- a/cli/tools/repl/editor.rs +++ b/cli/tools/repl/editor.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::cdp; use crate::colors; use deno_ast::swc::parser::error::SyntaxError; use deno_ast::swc::parser::token::BinOpToken; @@ -37,7 +38,6 @@ use std::sync::atomic::AtomicBool; use std::sync::atomic::Ordering::Relaxed; use std::sync::Arc; -use super::cdp; use super::channel::RustylineSyncMessageSender; use super::session::REPL_INTERNALS_NAME; diff --git a/cli/tools/repl/mod.rs b/cli/tools/repl/mod.rs index c25dc00c6..3700911d6 100644 --- a/cli/tools/repl/mod.rs +++ b/cli/tools/repl/mod.rs @@ -14,7 +14,6 @@ use deno_runtime::permissions::PermissionsContainer; use rustyline::error::ReadlineError; use tokio::sync::mpsc::unbounded_channel; -pub(crate) mod cdp; mod channel; mod editor; mod session; diff --git a/cli/tools/repl/session.rs b/cli/tools/repl/session.rs index df9a63772..b46f73d5a 100644 --- a/cli/tools/repl/session.rs +++ b/cli/tools/repl/session.rs @@ -5,6 +5,7 @@ use std::rc::Rc; use std::sync::Arc; use crate::args::CliOptions; +use crate::cdp; use crate::colors; use crate::lsp::ReplLanguageServer; use crate::npm::CliNpmResolver; @@ -48,8 +49,6 @@ use once_cell::sync::Lazy; use regex::Match; use regex::Regex; -use super::cdp; - fn comment_source_to_position_range( comment_start: SourcePos, m: &Match, diff --git a/cli/tools/run/hmr/mod.rs b/cli/tools/run/hmr.rs index 1a5772307..fb6651fed 100644 --- a/cli/tools/run/hmr/mod.rs +++ b/cli/tools/run/hmr.rs @@ -1,5 +1,6 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use crate::cdp; use crate::emit::Emitter; use crate::util::file_watcher::WatcherCommunicator; use crate::util::file_watcher::WatcherRestartMode; @@ -7,21 +8,45 @@ use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::futures::StreamExt; use deno_core::serde_json::json; +use deno_core::serde_json::Value; use deno_core::serde_json::{self}; use deno_core::url::Url; use deno_core::LocalInspectorSession; use deno_runtime::colors; +use serde::Deserialize; use std::collections::HashMap; use std::path::PathBuf; use std::sync::Arc; use tokio::select; -mod json_types; +// TODO(bartlomieju): the same thing is used in the REPL. Deduplicate. +#[derive(Debug, Deserialize)] +pub struct RpcNotification { + pub method: String, + pub params: Value, +} -use json_types::RpcNotification; -use json_types::ScriptParsed; -use json_types::SetScriptSourceReturnObject; -use json_types::Status; +fn explain(status: &cdp::Status) -> &'static str { + match status { + cdp::Status::Ok => "OK", + cdp::Status::CompileError => "compile error", + cdp::Status::BlockedByActiveGenerator => "blocked by active generator", + cdp::Status::BlockedByActiveFunction => "blocked by active function", + cdp::Status::BlockedByTopLevelEsModuleChange => { + "blocked by top-level ES module change" + } + } +} + +fn should_retry(status: &cdp::Status) -> bool { + match status { + cdp::Status::Ok => false, + cdp::Status::CompileError => false, + cdp::Status::BlockedByActiveGenerator => true, + cdp::Status::BlockedByActiveFunction => true, + cdp::Status::BlockedByTopLevelEsModuleChange => false, + } +} /// This structure is responsible for providing Hot Module Replacement /// functionality. @@ -102,7 +127,7 @@ impl HmrRunner { &mut self, script_id: &str, source: &str, - ) -> Result<SetScriptSourceReturnObject, AnyError> { + ) -> Result<cdp::SetScriptSourceResponse, AnyError> { let result = self .session .post_message( @@ -115,7 +140,7 @@ impl HmrRunner { ) .await?; - Ok(serde_json::from_value::<SetScriptSourceReturnObject>( + Ok(serde_json::from_value::<cdp::SetScriptSourceResponse>( result, )?) } @@ -162,7 +187,7 @@ impl HmrRunner { let description = exception.get("description").and_then(|d| d.as_str()).unwrap_or("undefined"); break Err(generic_error(format!("{text} {description}"))); } else if notification.method == "Debugger.scriptParsed" { - let params = serde_json::from_value::<ScriptParsed>(notification.params)?; + let params = serde_json::from_value::<cdp::ScriptParsed>(notification.params)?; if params.url.starts_with("file://") { let file_url = Url::parse(¶ms.url).unwrap(); let file_path = file_url.to_file_path().unwrap(); @@ -217,14 +242,14 @@ impl HmrRunner { loop { let result = self.set_script_source(&id, source_code.as_str()).await?; - if matches!(result.status, Status::Ok) { + if matches!(result.status, cdp::Status::Ok) { self.dispatch_hmr_event(module_url.as_str()).await?; self.watcher_communicator.print(format!("Replaced changed module {}", module_url.as_str())); break; } - self.watcher_communicator.print(format!("Failed to reload module {}: {}.", module_url, colors::gray(result.status.explain()))); - if result.status.should_retry() && tries <= 2 { + self.watcher_communicator.print(format!("Failed to reload module {}: {}.", module_url, colors::gray(explain(&result.status)))); + if should_retry(&result.status) && tries <= 2 { tries += 1; tokio::time::sleep(std::time::Duration::from_millis(100)).await; continue; diff --git a/cli/tools/run/hmr/json_types.rs b/cli/tools/run/hmr/json_types.rs deleted file mode 100644 index 3ac80344b..000000000 --- a/cli/tools/run/hmr/json_types.rs +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. - -// TODO(bartlomieju): this code should be factored out to `cli/cdp.rs` along -// with code in `cli/tools/repl/` and `cli/tools/coverage/`. These are all -// Chrome Devtools Protocol message types. - -use deno_core::serde_json::Value; -use serde::Deserialize; - -#[derive(Debug, Deserialize)] -pub struct RpcNotification { - pub method: String, - pub params: Value, -} - -#[derive(Debug, Deserialize)] -pub struct SetScriptSourceReturnObject { - pub status: Status, -} - -#[derive(Debug, Deserialize)] -#[serde(rename_all = "camelCase")] -pub struct ScriptParsed { - pub script_id: String, - pub url: String, -} - -#[derive(Debug, Deserialize)] -pub enum Status { - Ok, - CompileError, - BlockedByActiveGenerator, - BlockedByActiveFunction, - BlockedByTopLevelEsModuleChange, -} - -impl Status { - pub(crate) fn explain(&self) -> &'static str { - match self { - Status::Ok => "OK", - Status::CompileError => "compile error", - Status::BlockedByActiveGenerator => "blocked by active generator", - Status::BlockedByActiveFunction => "blocked by active function", - Status::BlockedByTopLevelEsModuleChange => { - "blocked by top-level ES module change" - } - } - } - - pub(crate) fn should_retry(&self) -> bool { - match self { - Status::Ok => false, - Status::CompileError => false, - Status::BlockedByActiveGenerator => true, - Status::BlockedByActiveFunction => true, - Status::BlockedByTopLevelEsModuleChange => false, - } - } -} |