crux_core::typegen

Struct TypeGen

Source
pub struct TypeGen {
    pub state: State,
}
Expand description

The TypeGen struct stores the registered types so that they can be generated for foreign languages use TypeGen::new() to create an instance

Fields§

§state: State

Implementations§

Source§

impl TypeGen

Source

pub fn new() -> Self

Creates an instance of the TypeGen struct

Source

pub fn register_app<A: App>(&mut self) -> Result
where A::Capabilities: Export, A::Event: Deserialize<'static>, A::ViewModel: Deserialize<'static> + 'static,

Register all the types used in app A to be shared with the Shell.

Do this before calling TypeGen::swift, TypeGen::java or TypeGen::typescript. This method would normally be called in a build.rs file of a sister crate responsible for creating “foreign language” type definitions for the FFI boundary. See the section on creating the shared types crate in the Crux book for more information.

Source

pub fn register_samples<'de, T>(&mut self, sample_data: Vec<T>) -> Result
where T: Deserialize<'de> + Serialize,

Register sample values for types with custom serialization. This is necessary because the type registration relies on Serde to understand the structure of the types, and as part of the process runs a faux deserialization on each of them, with a best guess of a default value. If that default value does not deserialize, the type registration will fail. You can prevent this problem by registering a valid sample value (or values), which the deserialization will use instead.

Source

pub fn register_type<'de, T>(&mut self) -> Result
where T: Deserialize<'de>,

For each of the types that you want to share with the Shell, call this method: e.g.

#[derive(Serialize, Deserialize)]
enum MyNestedEnum { None }
#[derive(Serialize, Deserialize)]
enum MyEnum { None, Nested(MyNestedEnum) }
fn register() -> Result<(), Error> {
  let mut gen = TypeGen::new();
  gen.register_type::<MyEnum>()?;
  gen.register_type::<MyNestedEnum>()?;
  Ok(())
}
Source

pub fn register_type_with_samples<'de, T>( &'de mut self, sample_data: Vec<T>, ) -> Result
where T: Deserialize<'de> + Serialize,

Usually, the simple register_type() method can generate the types you need. Sometimes, though, you need to provide samples of your type. The Uuid type, for example, requires a sample struct to help the typegen system understand what it looks like. Use this method to provide samples when you register a type.

For each of the types that you want to share with the Shell, call this method, providing samples of the type: e.g.

  let sample_data = vec![MyUuid(Uuid::new_v4())];
  gen.register_type_with_samples::<MyUuid>(sample_data)?;

Note: Because of the way that enums are handled by serde_reflection, you may need to ensure that enums provided as samples have a first variant that does not use custom deserialization.

Source

pub fn swift(&mut self, module_name: &str, path: impl AsRef<Path>) -> Result

Generates types for Swift e.g.

gen.swift("SharedTypes", output_root.join("swift"))?;
Source

pub fn java(&mut self, package_name: &str, path: impl AsRef<Path>) -> Result

Generates types for Java (for use with Kotlin) e.g.

gen.java(
    "com.redbadger.crux_core.shared_types",
    output_root.join("java"),
)?;
Source

pub fn typescript( &mut self, module_name: &str, path: impl AsRef<Path>, ) -> Result

Generates types for TypeScript e.g.

gen.typescript("shared_types", output_root.join("typescript"))?;

Trait Implementations§

Source§

impl Default for TypeGen

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl Freeze for TypeGen

§

impl !RefUnwindSafe for TypeGen

§

impl !Send for TypeGen

§

impl !Sync for TypeGen

§

impl Unpin for TypeGen

§

impl !UnwindSafe for TypeGen

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.