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.7", features = ["typegen"] }- Your
shared_typeslibrary, will have an emptylib.rs, since we only use it for generating foreign language type declarations. - Create a
build.rsin yourshared_typeslibrary, that looks something like this:
use shared::{App, EffectFfi, Event};
use crux_core::{bridge::Request, typegen::TypeGen};
use uuid::Uuid;
#[test]
fn generate_types() -> anyhow::Result<()> {
let mut gen = TypeGen::new();
let sample_events = vec![Event::SendUuid(Uuid::new_v4())];
gen.register_type_with_samples(sample_events)?;
gen.register_app::<App>()?;
let temp = assert_fs::TempDir::new()?;
let output_root = temp.join("crux_core_typegen_test");
gen.swift("SharedTypes", output_root.join("swift"))?;
gen.java("com.example.counter.shared_types", output_root.join("java"))?;
gen.typescript("shared_types", output_root.join("typescript"))?;
}§Custom extensions
May you need to use customized files for one of:
generated/typescript/*,generated/swift/(requests | Package).swift-generated/java/Requests.java
Then create the typegen_extensions/{target}/{target-file}
with the desired content next to your build.rs file.
For example typegen_extensions/swift/Package.swift:
// swift-tools-version: 5.7.1
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "SharedTypes",
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SharedTypes",
targets: ["SharedTypes"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Serde",
dependencies: []),
.target(
name: "SharedTypes",
dependencies: ["Serde"]),
]
)Structs§
- The
TypeGenstruct stores the registered types so that they can be generated for foreign languages useTypeGen::new()to create an instance