pub struct Generator { /* private fields */ }Expand description
TypeScript file generator
Collects types and generates TypeScript definition files.
Implementations§
Source§impl Generator
impl Generator
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create a new generator with default config
Sourcepub fn register<T: TS>(&mut self) -> &mut Self
pub fn register<T: TS>(&mut self) -> &mut Self
Register a type for generation
The type must implement the TS trait (usually via derive).
Returns &mut Self for method chaining.
Sourcepub fn add(&mut self, typedef: TypeDef) -> &mut Self
pub fn add(&mut self, typedef: TypeDef) -> &mut Self
Add a TypeDef directly to the registry
Useful when you have a TypeDef from another source.
Sourcepub fn registry(&self) -> &TypeRegistry
pub fn registry(&self) -> &TypeRegistry
Get a reference to the internal registry
Sourcepub fn registry_mut(&mut self) -> &mut TypeRegistry
pub fn registry_mut(&mut self) -> &mut TypeRegistry
Get a mutable reference to the internal registry
Sourcepub fn write(&self) -> Result<()>
pub fn write(&self) -> Result<()>
Generate TypeScript to the configured output file
§Errors
Returns an error if:
- No output path is configured
- The file cannot be written
Sourcepub fn write_if_changed(&self) -> Result<bool>
pub fn write_if_changed(&self) -> Result<bool>
Write only if content has changed
Returns Ok(true) if the file was written, Ok(false) if unchanged.
This is useful in build.rs to avoid unnecessary rebuilds.
§Errors
Returns an error if:
- No output path is configured
- The file cannot be read or written
Sourcepub fn types_by_module(&self) -> HashMap<String, Vec<String>>
pub fn types_by_module(&self) -> HashMap<String, Vec<String>>
Group types by their module path
Returns a map from module path to list of type names in that module. Types without a module path are grouped under “default”.
Sourcepub fn module_to_path(module: &str) -> PathBuf
pub fn module_to_path(module: &str) -> PathBuf
Convert a module path to a file path
For example:
my_crate::models::user->models/user.tsmy_crate::api::requests->api/requests.ts
The crate name is stripped from the beginning.
Sourcepub fn generate_for_module(&self, module: &str, type_names: &[String]) -> String
pub fn generate_for_module(&self, module: &str, type_names: &[String]) -> String
Generate TypeScript content for a specific module
Only includes types from the specified module.
Sourcepub fn write_multi_file(&self, output_dir: impl AsRef<Path>) -> Result<usize>
pub fn write_multi_file(&self, output_dir: impl AsRef<Path>) -> Result<usize>
Write TypeScript to multiple files, organized by module
Types are grouped by their module path and written to corresponding files. For example:
- Types from
my_crate::models::usergo to<output_dir>/models/user.ts - Types from
my_crate::apigo to<output_dir>/api.ts
§Arguments
output_dir- Base directory for output files
§Returns
Returns the number of files written.
§Errors
Returns an error if files cannot be written.