Usage Guide
This guide provides detailed instructions for using datamorph-rs in your projects.
Table of Contents
- Installation
- Basic Usage
- Transformation Specifications
- Built-in Functions
- Error Handling
- Best Practices
Installation
Add datamorph-rs to your project:
[dependencies]
datamorph-rs = "0.1.0"
Basic Usage
Simple Field Transformation
#![allow(unused)] fn main() { use datamorph_rs::Datamorph; use serde_json::json; let spec = r#"{ "mappings": { "name": { "target": "fullName", "transform": "uppercase" } } }"#; let input = json!({ "name": "john doe" }); let transformer = Datamorph::from_json(spec)?; let result: serde_json::Value = transformer.transform(input)?; }
Multiple Transformations
#![allow(unused)] fn main() { let spec = r#"{ "mappings": { "description": { "target": "summary", "transform": ["trim", "uppercase"] } } }"#; }
Transformation Specifications
Basic Structure
{
"mappings": {
"sourceField": {
"target": "targetField",
"transform": "transformationFunction"
}
}
}
Multiple Transformations
{
"mappings": {
"sourceField": {
"target": "targetField",
"transform": ["function1", "function2"]
}
}
}
Built-in Functions
String Transformations
uppercase
: Convert string to uppercaselowercase
: Convert string to lowercasetoString
: Convert value to string
Examples
{
"mappings": {
"name": {
"target": "upperName",
"transform": "uppercase"
},
"age": {
"target": "ageString",
"transform": "toString"
}
}
}
Error Handling
#![allow(unused)] fn main() { use datamorph_rs::{Datamorph, Error}; match Datamorph::from_json(spec) { Ok(transformer) => { match transformer.transform(input) { Ok(result) => println!("Success: {}", result), Err(Error::TransformError(msg)) => eprintln!("Transform failed: {}", msg), Err(e) => eprintln!("Other error: {}", e), } }, Err(e) => eprintln!("Failed to parse spec: {}", e), } }
Best Practices
-
Specification Organization
- Keep specifications simple and focused
- Use meaningful field names
- Document complex transformations
-
Error Handling
- Always handle potential errors
- Validate specifications before use
- Log transformation errors
-
Performance
- Minimize number of transformations
- Use appropriate data types
- Consider batch processing for large datasets