TypeRegistry

Struct TypeRegistry 

Source
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

Source

pub fn new() -> Self

Creates a new empty registry.

Source

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.

Source

pub fn add_typedef(&mut self, typedef: TypeDef)

Adds a TypeDef to the registry, extracting all named types.

Source

pub fn len(&self) -> usize

Returns the number of registered types.

Source

pub fn is_empty(&self) -> bool

Returns true if no types are registered.

Source

pub fn type_names(&self) -> impl Iterator<Item = &str>

Returns the names of all registered types.

Source

pub fn get(&self, name: &str) -> Option<&TypeDef>

Gets a type definition by name.

Source

pub fn sorted_types(&self) -> Vec<&str>

Returns types in dependency order (types with no dependencies first).

Uses Kahn’s algorithm for topological sort.

Source

pub fn render(&self) -> String

Renders all registered types to TypeScript declarations.

Types are emitted in dependency order, with proper formatting.

Source

pub fn render_exported(&self) -> String

Renders all registered types with export keywords.

Source

pub fn clear(&mut self)

Clears all registered types.

Trait Implementations§

Source§

impl Debug for TypeRegistry

Source§

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

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

impl Default for TypeRegistry

Source§

fn default() -> TypeRegistry

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.