Generator

Struct Generator 

Source
pub struct Generator { /* private fields */ }
Expand description

TypeScript file generator

Collects types and generates TypeScript definition files.

Implementations§

Source§

impl Generator

Source

pub fn new(config: Config) -> Self

Create a new generator with the given config

Source

pub fn with_defaults() -> Self

Create a new generator with default config

Source

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.

Source

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.

Source

pub fn registry(&self) -> &TypeRegistry

Get a reference to the internal registry

Source

pub fn registry_mut(&mut self) -> &mut TypeRegistry

Get a mutable reference to the internal registry

Source

pub fn generate(&self) -> String

Generate TypeScript and return as string

Source

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
Source

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
Source

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”.

Source

pub fn module_to_path(module: &str) -> PathBuf

Convert a module path to a file path

For example:

  • my_crate::models::user -> models/user.ts
  • my_crate::api::requests -> api/requests.ts

The crate name is stripped from the beginning.

Source

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.

Source

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::user go to <output_dir>/models/user.ts
  • Types from my_crate::api go 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.

Source

pub fn write_multi_file_if_changed( &self, output_dir: impl AsRef<Path>, ) -> Result<usize>

Write multi-file only if content has changed

Returns the number of files that were written (changed).

Trait Implementations§

Source§

impl Debug for Generator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Generator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.