Skip to content

Commit

Permalink
Added example to export typescript types
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmcquilkin committed Feb 25, 2024
1 parent 6bd239f commit 09a8090
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"rust-analyzer.cargo.features": ["ts-gen"]
}
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ name = "basic_serial"
[[example]]
name = "basic_tcp"

[[example]]
name = "message_filtering"

[[example]]
name = "generate_typescript_types"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[build-dependencies]
Expand Down
29 changes: 29 additions & 0 deletions examples/generate_typescript_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// This example connects to a radio via serial, and demonstrates how to
/// configure handlers for different types of decoded radio packets.
/// https://meshtastic.org/docs/supported-hardware
///
/// Run this example with the command `cargo run --example generate_typescript_types --features "ts-gen"`
extern crate meshtastic;

use meshtastic::ts::specta::{
export::ts_with_cfg,
ts::{BigIntExportBehavior, ExportConfiguration, ModuleExportBehavior, TsExportError},
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Exports relative to the root workspace directory
export_ts_types("./examples/bindings.ts")?;

Ok(())
}

fn export_ts_types(file_path: &str) -> Result<(), TsExportError> {
// Sets up a default configuration for exporting typescript types
let ts_export_config = ExportConfiguration::default()
.bigint(BigIntExportBehavior::String)
.modules(ModuleExportBehavior::Enabled);

// Use Specta helper function to export typescript types
ts_with_cfg(file_path, &ts_export_config)
}

0 comments on commit 09a8090

Please sign in to comment.