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: StateImplementations§
Source§impl TypeGen
impl TypeGen
Sourcepub fn register_app<A: App>(&mut self) -> Resultwhere
A::Capabilities: Export,
A::Event: Deserialize<'static>,
A::ViewModel: Deserialize<'static> + 'static,
pub fn register_app<A: App>(&mut self) -> Resultwhere
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.
Sourcepub fn register_samples<'de, T>(&mut self, sample_data: Vec<T>) -> Resultwhere
T: Deserialize<'de> + Serialize,
pub fn register_samples<'de, T>(&mut self, sample_data: Vec<T>) -> Resultwhere
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.
Sourcepub fn register_type<'de, T>(&mut self) -> Resultwhere
T: Deserialize<'de>,
pub fn register_type<'de, T>(&mut self) -> Resultwhere
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(())
}Sourcepub fn register_type_with_samples<'de, T>(
&'de mut self,
sample_data: Vec<T>,
) -> Resultwhere
T: Deserialize<'de> + Serialize,
pub fn register_type_with_samples<'de, T>(
&'de mut self,
sample_data: Vec<T>,
) -> Resultwhere
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.
Sourcepub fn swift(&mut self, module_name: &str, path: impl AsRef<Path>) -> Result
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"))?;