transform!() { /* proc-macro */ }
Expand description
AI-powered data transformation macro.
Important: It is intended to be used when you understand that such a transformation from source type to target type makes sense.
§Syntax
ⓘ
transform!(SourceType, TargetType, source_value)
§Requirements
Dependencies: Add both this and runtime to your Cargo.toml
:
[dependencies]
ai-transform = "0.1.0"
ai-transform-runtime = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["macros"] }
Environment: Set your OpenAI
API key:
export OPENAI_API_KEY="your-api-key-here"
Type Requirements: Both types must implement:
serde::Serialize
+serde::Deserialize
+Default
§Arguments
SourceType
- Input data typeTargetType
- Output data typesource_value
- Expression evaluating to aSourceType
value
§Returns
Future<Result<TargetType, ai_transform_runtime::error::TransformError>>
§Examples
§Basic Field Mapping
ⓘ
use ai_transform::transform;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Default)]
struct User { name: String, age: u32 }
#[derive(Serialize, Deserialize, Default)]
struct Profile { full_name: String, years_old: u32, is_adult: bool }
let user = User { name: "Alice".into(), age: 28 };
// AI automatically maps: name → full_name, age → years_old
// and computes: is_adult from age
let profile: Profile = transform!(User, Profile, user).await?;
§Error Handling
ⓘ
use ai_transform_runtime::error::TransformError;
match transform!(Source, Target, source).await {
Ok(result) => println!("Success: {:?}", result),
Err(TransformError::EnvVarError(var)) => {
eprintln!("Missing environment variable: {}", var);
}
Err(TransformError::ApiError { status, body }) => {
eprintln!("OpenAI API error {}: {}", status, body);
}
Err(e) => eprintln!("Other error: {}", e),
}
§Errors
See ai_transform_runtime::error::TransformError
for details and all error variants.