Usage Guide
This guide covers various use cases and patterns for using enrichr-rs
.
Table of Contents
Basic Usage
The simplest use case involves direct field mapping:
#![allow(unused)] fn main() { #[derive(Enrichable, Default)] struct Person { name: String, age: u32, } let spec = r#"{ "name": "$.person.name", "age": "$.person.age" }"#; }
Complex Mappings
Nested Objects
#![allow(unused)] fn main() { #[derive(Enrichable, Default)] struct Employee { name: String, department: Department, } #[derive(Enrichable, Default)] struct Department { id: String, name: String, } let spec = r#"{ "name": "$.employee.personal.full_name", "department.id": "$.employee.dept.dept_id", "department.name": "$.employee.dept.dept_name" }"#; }
Array Handling
#![allow(unused)] fn main() { #[derive(Enrichable, Default)] struct Team { name: String, members: Vec<String>, } let spec = r#"{ "name": "$.team.name", "members": "$.team.member_list[*].name" }"#; }
Optional Fields
#![allow(unused)] fn main() { #[derive(Enrichable, Default)] struct User { name: String, email: Option<String>, } let spec = r#"{ "name": "$.user.name", "email": "$.user.contact.email" }"#; }
Type Conversions
The library handles various type conversions automatically:
#![allow(unused)] fn main() { #[derive(Enrichable, Default)] struct Metrics { count: u32, // From number enabled: bool, // From boolean ratio: f64, // From number tags: Vec<String>, // From array } }
Custom Types
For custom types that implement Deserialize
:
#![allow(unused)] fn main() { #[derive(Deserialize)] enum Status { Active, Inactive, } #[derive(Enrichable, Default)] struct Account { id: String, status: Status, } }