diff --git a/bin/main.rs b/bin/main.rs index 7cef82d..0394a75 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -51,10 +51,36 @@ struct Args { required = true )] output: PathBuf, + + /// Output to stdout. + #[clap(short, long, help = "Output to stdout, not file", required = false)] + stdout: bool, } fn main() { let args: Args = Args::parse(); - tsync::generate_typescript_defs(args.input, args.output, args.debug, args.enable_const_enums); + let uses_type_interface = args + .output + .to_str() + .map(|x| x.ends_with(".d.ts")) + .unwrap_or(true); + + if args.stdout { + let content = tsync::generate_typescript_defs_inner( + args.input, + uses_type_interface, + args.debug, + args.enable_const_enums, + ); + + println!("{}", content.types) + } else { + tsync::generate_typescript_defs( + args.input, + args.output, + args.debug, + args.enable_const_enums, + ); + } } diff --git a/src/lib.rs b/src/lib.rs index b14b90b..87335c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,19 +64,16 @@ impl BuildState { let indentation = utils::build_indentation(indentation_amount); match comments.len() { 0 => (), - 1 => { - self.types - .push_str(&format!("{}/** {} */\n", indentation, &comments[0])) - } + 1 => self + .types + .push_str(&format!("{}/** {} */\n", indentation, &comments[0])), _ => { - self.types - .push_str(&format!("{}/**\n", indentation)); + self.types.push_str(&format!("{}/**\n", indentation)); for comment in comments { self.types .push_str(&format!("{} * {}\n", indentation, &comment)) } - self.types - .push_str(&format!("{} */\n", indentation)) + self.types.push_str(&format!("{} */\n", indentation)) } } } @@ -118,12 +115,16 @@ fn process_rust_file>( } let Ok(src) = std::fs::read_to_string(input_path.as_ref()) else { - state.unprocessed_files.push(input_path.as_ref().to_path_buf()); + state + .unprocessed_files + .push(input_path.as_ref().to_path_buf()); return; }; let Ok(syntax) = syn::parse_file(&src) else { - state.unprocessed_files.push(input_path.as_ref().to_path_buf()); + state + .unprocessed_files + .push(input_path.as_ref().to_path_buf()); return; }; @@ -135,7 +136,9 @@ fn process_rust_file>( fn check_path>(path: P, state: &mut BuildState) -> bool { if !path.as_ref().exists() { - if *DEBUG.get() { println!("Path `{:#?}` does not exist", path.as_ref()); } + if *DEBUG.get() { + println!("Path `{:#?}` does not exist", path.as_ref()); + } state.unprocessed_files.push(path.as_ref().to_path_buf()); return false; } @@ -148,7 +151,7 @@ fn check_extension>(ext: &OsStr, path: P) -> bool { if *DEBUG.get() { println!("Encountered non-rust file `{:#?}`", path.as_ref()); } - return false + return false; } true @@ -170,7 +173,10 @@ fn validate_dir_entry(entry_result: walkdir::Result, path: &Path) -> O Some(entry) } Err(e) => { - println!("An error occurred whilst walking directory `{}`...", path.display()); + println!( + "An error occurred whilst walking directory `{}`...", + path.display() + ); println!("Details: {e:?}"); None } @@ -194,19 +200,13 @@ fn process_dir_entry>(path: P, state: &mut BuildState, config: &B }) } -pub fn generate_typescript_defs( +pub fn generate_typescript_defs_inner( input: Vec, - output: PathBuf, + uses_type_interface: bool, debug: bool, enable_const_enums: bool, -) { +) -> BuildState { DEBUG.set(debug); - - let uses_type_interface = output - .to_str() - .map(|x| x.ends_with(".d.ts")) - .unwrap_or(true); - let config = BuildSettings { uses_type_interface, enable_const_enums, @@ -228,6 +228,25 @@ pub fn generate_typescript_defs( } }); + state +} + +pub fn generate_typescript_defs( + input: Vec, + output: PathBuf, + debug: bool, + enable_const_enums: bool, +) { + DEBUG.set(debug); + + let uses_type_interface = output + .to_str() + .map(|x| x.ends_with(".d.ts")) + .unwrap_or(true); + + let state = + generate_typescript_defs_inner(input, uses_type_interface, debug, enable_const_enums); + if debug { println!("======================================"); println!("FINAL FILE:");