1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Procedural macro for AI-powered data transformations between JSON-serializable types.
//!
//! Transforms data from one JSON-serializable type to another using `OpenAI`'s
//! language models. The idea is to enable semantic understanding when that is
//! useful and easier than writing deterministic parser by hand.
//!
//! **Important**: This crate requires [`ai_transform_runtime`] to run properly.
//!
//! ```toml
//! [dependencies]
//! ai-transform = "0.1.0"
//! ai-transform-runtime = "0.1.0" # Required!
//! ```
//!
//! # How It Works
//!
//! 1. **Macro Expansion**: Generates `ai_transform_runtime::transform::<S, T>(value)`
//! 2. **Serialization**: Converts source value to JSON
//! 3. **Schema Generation**: Creates example JSON for both types using `Default`
//! 4. **AI Request**: Sends transformation prompt to `OpenAI` with context
//! 5. **Response Processing**: Extracts and validates JSON from AI response
//! 6. **Deserialization**: Converts result to target type
//!
//! # Considerations
//!
//! - Each call makes an HTTP request to `OpenAI`'s API
//! - Consider caching for repeated transformations
//! - Response time: ~1-5 seconds depending on complexity
//!
//! # Configuration
//!
//! Environment variables:
//! - `OPENAI_API_KEY`: Your API key (required)
//! - `OPENAI_MODEL`: Model to use (default: `"gpt-4o"`)
//! - `OPENAI_BASE_URL`: API endpoint (default: `OpenAI`'s URL)
//!
//! See [`transform!`] for usage info.
//!
//! [`ai_transform_runtime`]: https://docs.rs/ai-transform-runtime
use TokenStream;
use quote;
use ;
/// Type representing macro input for parsing.
/// 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
///
/// ```rust,ignore
/// transform!(SourceType, TargetType, source_value)
/// ```
///
/// # Requirements
///
/// **Dependencies**: Add both this and runtime to your `Cargo.toml`:
/// ```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:
/// ```bash
/// export OPENAI_API_KEY="your-api-key-here"
/// ```
///
/// **Type Requirements**: Both types must implement:
/// - `serde::Serialize` + `serde::Deserialize` + `Default`
///
/// # Arguments
///
/// * `SourceType` - Input data type
/// * `TargetType` - Output data type
/// * `source_value` - Expression evaluating to a `SourceType` value
///
/// # Returns
///
/// `Future<Result<TargetType, ai_transform_runtime::error::TransformError>>`
///
/// # Examples
///
/// ## Basic Field Mapping
///
/// ```rust,ignore
/// 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 }
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// 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?;
/// # Ok(())
/// # }
/// ```
///
/// ## Error Handling
///
/// ```rust,ignore
/// # use ai_transform::transform;
/// # use serde::{Deserialize, Serialize};
/// use ai_transform_runtime::error::TransformError;
///
/// # #[derive(Serialize, Deserialize, Default, Debug)]
/// # struct Source { data: String }
/// # #[derive(Serialize, Deserialize, Default, Debug)]
/// # struct Target { result: String }
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// # let source = Source::default();
/// 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),
/// }
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// See [`ai_transform_runtime::error::TransformError`] for details and all error variants.