Skip to content

Rust SDK Quickstart

Get up and running with PhyTrace Rust SDK in 5 minutes.

Installation

cargo add phytrace-sdk
cargo add tokio --features full

Step 1: Basic Event Building

use phytrace_sdk::{UdmEvent, EventType, SourceType};
use phytrace_sdk::models::domains::*;

fn main() {
    let event = UdmEvent::new(SourceType::Amr)
        .with_event_type(EventType::TelemetryPeriodic)
        .with_identity(IdentityDomain {
            source_id: Some("robot-001".to_string()),
            fleet_id: Some("warehouse-fleet".to_string()),
            site_id: Some("site-nyc-01".to_string()),
            ..Default::default()
        })
        .with_location(LocationDomain {
            position_2d: Some(Position2D { x: 10.5, y: 20.3 }),
            heading_rad: Some(1.57),
            ..Default::default()
        })
        .with_power(PowerDomain {
            battery_soc_pct: Some(85.0),
            is_charging: Some(false),
            ..Default::default()
        });

    println!("{}", event.to_json_pretty().unwrap());
}

Step 2: Using the Agent

use phytrace_sdk::{PhyTraceAgent, PhyTraceConfig, SourceType};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let config = PhyTraceConfig::new("robot-001")
        .with_endpoint("https://api.phycloud.io/v1/events")
        .with_api_key("your-api-key")
        .with_source_type(SourceType::Amr);

    let agent = PhyTraceAgent::from_config(config).await?;
    agent.start().await?;

    // Emit events using the fluent builder
    agent.emit()
        .position_2d(10.0, 20.0)
        .heading(1.57)
        .speed(2.5)
        .battery_soc(85.0)
        .send()
        .await?;

    agent.stop().await?;
    Ok(())
}

Step 3: Configuration from YAML

use phytrace_sdk::PhyTraceAgent;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let agent = PhyTraceAgent::from_config_file("phytrace-agent.yaml").await?;
    agent.start().await?;
    // ...
    Ok(())
}

Step 4: Validation

use phytrace_sdk::core::ValidationEngine;

let engine = ValidationEngine::new();
let result = engine.validate(&event);

if result.is_valid() {
    println!("Event is valid!");
} else {
    for error in result.errors() {
        eprintln!("Validation error: {} - {}", error.field, error.message);
    }
}

Next Steps