Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/open-payments/enrichr-rs
A derive macro to enrich Rust structs using declarative transformation specs
https://github.com/open-payments/enrichr-rs
Last synced: about 1 month ago
JSON representation
A derive macro to enrich Rust structs using declarative transformation specs
- Host: GitHub
- URL: https://github.com/open-payments/enrichr-rs
- Owner: Open-Payments
- License: apache-2.0
- Created: 2024-10-31T11:35:32.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-04T04:27:40.000Z (about 2 months ago)
- Last Synced: 2024-11-04T05:23:40.083Z (about 2 months ago)
- Language: Rust
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# struct-enrichment
A Rust library for enriching structs using JSONPath-based mapping rules with support for complex transformations and conditional logic.
## Features
- β¨ Derive macro for automatic implementation
- πΊοΈ JSONPath-based field mapping
- π Rich set of transformations
- π― Multiple source and target paths
- π Conditional mapping using JSONLogic
- π¨ Template-based formatting## Installation
Add to your `Cargo.toml`:
```toml
[dependencies]
struct_enrichment = "0.1.0"
```## Quick Start
```rust
use struct_enrichment::{Enrichable, MappingRule, JsonPath, Transform};
use serde::{Serialize, Deserialize};
use std::collections::HashMap;#[derive(Debug, Serialize, Deserialize, Enrichable)]
struct User {
first_name: String,
last_name: String,
email: Option,
tags: Vec,
}fn main() -> Result<(), Box> {
let mut user = User {
first_name: String::new(),
last_name: String::new(),
email: None,
tags: vec![],
};// Input data in JSON format for readability
let data_json = r#"{
"user_data": {
"full_name": "John Doe",
"email": "[email protected]",
"tags": "admin,user"
}
}"#;// Convert JSON to HashMap
let data: HashMap = serde_json::from_str(data_json)?;// Mapping specification in JSON format
let spec_json = r#"[
{
"source": "$.user_data.full_name",
"target": ["$.first_name", "$.last_name"],
"transform": {
"type": "Split",
"params": {
"delimiter": " "
}
}
},
{
"source": "$.user_data.email",
"target": "$.email"
},
{
"source": "$.user_data.tags",
"target": "$.tags",
"transform": {
"type": "Split",
"params": {
"delimiter": ","
}
}
}
]"#;// Convert JSON spec to Vec
let spec: Vec = serde_json::from_str(spec_json)?;// Perform enrichment
user.enrich(&data, &spec)?;assert_eq!(user.first_name, "John");
assert_eq!(user.last_name, "Doe");
assert_eq!(user.email, Some("[email protected]".to_string()));
assert_eq!(user.tags, vec!["admin", "user"]);Ok(())
}
```## Available Transformations
- `ToString`: Convert any value to string
- `ToUpperCase`: Convert string to uppercase
- `ToLowerCase`: Convert string to lowercase
- `Split`: Split string into array using delimiter
- `Concat`: Join array values with delimiter
- `Replace`: Replace substrings
- `Substring`: Extract substring
- `Template`: Format string using placeholders## Documentation
- [Usage Guide](./docs/usage.md) - Detailed examples and patterns
- [Error Handling](./docs/error_handling.md) - Error handling guide