Module crux_core::typegen

source ·
Expand description

Generation of foreign language types (currently Swift, Java, TypeScript) for Crux

In order to use this module, you’ll need a separate crate from your shared library, possibly called shared_types. This is necessary because we need to reference types from your shared library during the build process (build.rs).

This module is behind the feature called typegen, and is not compiled into the default crate.

Ensure that you have the following line in the Cargo.toml of your shared_types library.

[build-dependencies]
crux_core = { version = "0.6", features = ["typegen"] }
  • Your shared_types library, will have an empty lib.rs, since we only use it for generating foreign language type declarations.
  • Create a build.rs in your shared_types library, that looks something like this:
use shared::{App, EffectFfi, Event};
use crux_core::{bridge::Request, typegen::TypeGen};
use uuid::Uuid;

#[test]
fn generate_types() {
   let mut gen = TypeGen::new();

   let sample_events = vec![Event::SendUuid(Uuid::new_v4())];
   gen.register_type_with_samples(sample_events).unwrap();

   gen.register_app::<App>().unwrap();

   let temp = assert_fs::TempDir::new().unwrap();
   let output_root = temp.join("crux_core_typegen_test");

   gen.swift("SharedTypes", output_root.join("swift"))
       .expect("swift type gen failed");

   gen.java("com.example.counter.shared_types", output_root.join("java"))
       .expect("java type gen failed");

   gen.typescript("shared_types", output_root.join("typescript"))
       .expect("typescript type gen failed");
}

Structs

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

Enums

Traits

Type Aliases