From 334bc20c3a51c0ddb1a15c04609efa17cd64a80f Mon Sep 17 00:00:00 2001 From: AskSkivdal Date: Fri, 12 Dec 2025 17:19:07 +0100 Subject: [PATCH 1/3] Add flag to print to stdout --- bin/main.rs | 35 +++++++++++++++++++++++------ src/lib.rs | 65 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 70 insertions(+), 30 deletions(-) diff --git a/bin/main.rs b/bin/main.rs index 7cef82d..83f0520 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -44,17 +44,38 @@ struct Args { input: Vec, /// Output file (this is the ".d.ts" that gets generated) - #[clap( - short, - long, - help = "Required; file to write generated types to", - required = true - )] + #[clap(short, long, help = "Required; file to write generated types to")] 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.enable_const_enums, + args.debug, + ); + + println!("{}", content.types) + } else { + tsync::generate_typescript_defs( + args.input, + args.output, + args.enable_const_enums, + args.debug, + ); + } } diff --git a/src/lib.rs b/src/lib.rs index b14b90b..e1105be 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, - debug: bool, + uses_type_interface: bool, enable_const_enums: bool, -) { + debug: 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, + enable_const_enums: bool, + debug: 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, enable_const_enums, debug); + if debug { println!("======================================"); println!("FINAL FILE:"); From 823604563722fd521c7aa7eeb9cb29c6fdb8df9d Mon Sep 17 00:00:00 2001 From: AskSkivdal Date: Fri, 12 Dec 2025 17:24:14 +0100 Subject: [PATCH 2/3] Fix --- bin/main.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bin/main.rs b/bin/main.rs index 83f0520..ab22b9c 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -44,7 +44,12 @@ struct Args { input: Vec, /// Output file (this is the ".d.ts" that gets generated) - #[clap(short, long, help = "Required; file to write generated types to")] + #[clap( + short, + long, + help = "Required; file to write generated types to", + required = true + )] output: PathBuf, /// Output to stdout. From ead7793caf568a273a0cf159c155a22580ca2819 Mon Sep 17 00:00:00 2001 From: AskSkivdal Date: Fri, 12 Dec 2025 17:26:43 +0100 Subject: [PATCH 3/3] Restore function signatures --- bin/main.rs | 4 ++-- src/lib.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bin/main.rs b/bin/main.rs index ab22b9c..0394a75 100644 --- a/bin/main.rs +++ b/bin/main.rs @@ -70,8 +70,8 @@ fn main() { let content = tsync::generate_typescript_defs_inner( args.input, uses_type_interface, - args.enable_const_enums, args.debug, + args.enable_const_enums, ); println!("{}", content.types) @@ -79,8 +79,8 @@ fn main() { tsync::generate_typescript_defs( args.input, args.output, - args.enable_const_enums, args.debug, + args.enable_const_enums, ); } } diff --git a/src/lib.rs b/src/lib.rs index e1105be..87335c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -203,8 +203,8 @@ fn process_dir_entry>(path: P, state: &mut BuildState, config: &B pub fn generate_typescript_defs_inner( input: Vec, uses_type_interface: bool, - enable_const_enums: bool, debug: bool, + enable_const_enums: bool, ) -> BuildState { DEBUG.set(debug); let config = BuildSettings { @@ -234,8 +234,8 @@ pub fn generate_typescript_defs_inner( pub fn generate_typescript_defs( input: Vec, output: PathBuf, - enable_const_enums: bool, debug: bool, + enable_const_enums: bool, ) { DEBUG.set(debug); @@ -245,7 +245,7 @@ pub fn generate_typescript_defs( .unwrap_or(true); let state = - generate_typescript_defs_inner(input, uses_type_interface, enable_const_enums, debug); + generate_typescript_defs_inner(input, uses_type_interface, debug, enable_const_enums); if debug { println!("======================================");