pub struct TypeRegistry { /* private fields */ }Expand description
A registry for collecting and managing TypeScript type definitions.
The TypeRegistry collects named types, resolves their dependency order, and renders them to a TypeScript file. This enables:
- Deduplication: Each named type is emitted once
- Ordering: Types are emitted in dependency order
- Output: Generate valid .ts or .d.ts files
§Example
use ferrotype::{TypeRegistry, TypeScript};
let mut registry = TypeRegistry::new();
registry.register::<User>();
registry.register::<Post>();
let output = registry.render();
std::fs::write("types.ts", output)?;Implementations§
Source§impl TypeRegistry
impl TypeRegistry
Sourcepub fn from_distributed() -> Self
pub fn from_distributed() -> Self
Creates a registry populated with all auto-registered types.
This collects all types that were registered via the #[derive(TypeScript)]
macro using the distributed slice mechanism.
§Example
use ferrotype::{TypeRegistry, TypeScript};
#[derive(TypeScript)]
struct User { name: String, age: u32 }
#[derive(TypeScript)]
struct Post { title: String, author: User }
// Collect all types automatically - no manual registration needed!
let registry = TypeRegistry::from_distributed();
println!("{}", registry.render());Sourcepub fn collect_all(&mut self)
pub fn collect_all(&mut self)
Collects all auto-registered types into this registry.
This is useful when you want to add auto-registered types to an existing registry that may already have some manually registered types.
§Example
let mut registry = TypeRegistry::new();
// Add some manual types first
registry.register::<SomeManualType>();
// Then collect all auto-registered types
registry.collect_all();Sourcepub fn register<T: TypeScript>(&mut self)
pub fn register<T: TypeScript>(&mut self)
Registers a type that implements TypeScript.
This extracts all named types from the type definition and adds them to the registry. Named types are deduplicated by name.
Sourcepub fn add_typedef(&mut self, typedef: TypeDef)
pub fn add_typedef(&mut self, typedef: TypeDef)
Adds a TypeDef to the registry, extracting all named types.
Sourcepub fn type_names(&self) -> impl Iterator<Item = &str>
pub fn type_names(&self) -> impl Iterator<Item = &str>
Returns the names of all registered types.
Sourcepub fn sorted_types(&self) -> Vec<&str>
pub fn sorted_types(&self) -> Vec<&str>
Returns types in dependency order (types with no dependencies first).
Uses Kahn’s algorithm for topological sort.
Sourcepub fn render(&self) -> String
pub fn render(&self) -> String
Renders all registered types to TypeScript declarations.
Types are emitted in dependency order, with proper formatting.
Sourcepub fn render_exported(&self) -> String
pub fn render_exported(&self) -> String
Renders all registered types with export keywords.
For namespaced types, exports the namespace declaration.