From 01e02d3123b494895b7b7395e81a577970987b74 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sat, 14 Jan 2023 12:39:56 -0500 Subject: refactor: create enum for `--builtin` doc flag (#17423) --- cli/args/flags.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'cli/args') diff --git a/cli/args/flags.rs b/cli/args/flags.rs index 2e99aab82..84c1d083b 100644 --- a/cli/args/flags.rs +++ b/cli/args/flags.rs @@ -94,11 +94,23 @@ pub struct CoverageFlags { pub lcov: bool, } +#[derive(Clone, Debug, Eq, PartialEq)] +pub enum DocSourceFileFlag { + Builtin, + Path(String), +} + +impl Default for DocSourceFileFlag { + fn default() -> Self { + Self::Builtin + } +} + #[derive(Clone, Debug, Eq, PartialEq)] pub struct DocFlags { pub private: bool, pub json: bool, - pub source_file: Option, + pub source_file: DocSourceFileFlag, pub filter: Option, } @@ -2444,7 +2456,16 @@ fn doc_parse(flags: &mut Flags, matches: &clap::ArgMatches) { import_map_arg_parse(flags, matches); reload_arg_parse(flags, matches); - let source_file = matches.value_of("source_file").map(String::from); + let source_file = matches + .value_of("source_file") + .map(|value| { + if value == "--builtin" { + DocSourceFileFlag::Builtin + } else { + DocSourceFileFlag::Path(value.to_string()) + } + }) + .unwrap_or_default(); let private = matches.is_present("private"); let json = matches.is_present("json"); let filter = matches.value_of("filter").map(String::from); @@ -4916,7 +4937,7 @@ mod tests { r.unwrap(), Flags { subcommand: DenoSubcommand::Doc(DocFlags { - source_file: Some("script.ts".to_owned()), + source_file: DocSourceFileFlag::Path("script.ts".to_owned()), private: false, json: false, filter: None, @@ -5922,7 +5943,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: true, - source_file: Some("path/to/module.ts".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.ts".to_string()), filter: None, }), ..Flags::default() @@ -5941,7 +5962,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: Some("path/to/module.ts".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.ts".to_string()), filter: Some("SomeClass.someField".to_string()), }), ..Flags::default() @@ -5955,7 +5976,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: None, + source_file: Default::default(), filter: None, }), ..Flags::default() @@ -5969,7 +5990,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: false, json: false, - source_file: Some("--builtin".to_string()), + source_file: DocSourceFileFlag::Builtin, filter: Some("Deno.Listener".to_string()), }), ..Flags::default() @@ -5984,7 +6005,7 @@ mod tests { subcommand: DenoSubcommand::Doc(DocFlags { private: true, json: false, - source_file: Some("path/to/module.js".to_string()), + source_file: DocSourceFileFlag::Path("path/to/module.js".to_string()), filter: None, }), ..Flags::default() -- cgit v1.2.3