Repository navigation
Expand file tree
/
Copy pathnested_enum_example.rs
More file actions
148 lines (130 loc) · 4.32 KB
/
Copy pathnested_enum_example.rs
File metadata and controls
148 lines (130 loc) · 4.32 KB
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
//! Example demonstrating nested enum support
//!
//! This example shows that nested enums now work automatically with #[derive(Instructor)]
//! without requiring manual SchemaType implementations.
//!
//! Previously, users had to manually implement SchemaType for nested enums:
//! ```rust
//! impl SchemaType for NestedEnumType {
//! fn schema() -> Schema { ... }
//! }
//! ```
//!
//! Now, nested enums work automatically with #[derive(Instructor)]!
use rstructor::{Instructor, SchemaType};
use serde::{Deserialize, Serialize};
// Define a simple enum
#[derive(Instructor, Serialize, Deserialize, Debug, PartialEq)]
enum Priority {
Low,
Medium,
High,
Critical,
}
// Define another enum
#[derive(Instructor, Serialize, Deserialize, Debug, PartialEq)]
enum Status {
Pending,
InProgress,
Completed,
Cancelled,
}
// Define a nested enum - enum variant containing another enum
// This now works automatically with #[derive(Instructor)]!
#[derive(Instructor, Serialize, Deserialize, Debug, PartialEq)]
enum TaskState {
#[llm(description = "Task is pending with a priority level")]
Pending {
#[llm(description = "Priority level")]
priority: Priority,
},
#[llm(description = "Task is in progress")]
InProgress {
#[llm(description = "Priority level")]
priority: Priority,
#[llm(description = "Current status")]
status: Status,
},
#[llm(description = "Task is completed")]
Completed {
#[llm(description = "Completion status")]
status: Status,
},
}
// Define a struct that uses the nested enum
#[derive(Instructor, Serialize, Deserialize, Debug, PartialEq)]
struct Task {
#[llm(description = "Task title")]
title: String,
#[llm(description = "Current task state with nested enums")]
state: TaskState,
}
fn main() {
// Demonstrate that schema generation works automatically
println!("=== Nested Enum Schema Generation ===\n");
// Generate schema for the nested enum - works automatically!
let task_state_schema = TaskState::schema();
let schema_json = task_state_schema.to_json();
println!("TaskState schema (automatically generated):");
println!("{}", serde_json::to_string_pretty(&schema_json).unwrap());
println!();
// Demonstrate deserialization works correctly
println!("=== Deserialization Test ===\n");
let json1 = serde_json::json!({
"Pending": {
"priority": "High"
}
});
let task_state1: TaskState = serde_json::from_value(json1).unwrap();
match task_state1 {
TaskState::Pending { priority } => {
println!(
"✓ Deserialized Pending variant with priority: {:?}",
priority
);
assert_eq!(priority, Priority::High);
}
_ => panic!("Expected Pending variant"),
}
let json2 = serde_json::json!({
"InProgress": {
"priority": "Critical",
"status": "InProgress"
}
});
let task_state2: TaskState = serde_json::from_value(json2).unwrap();
match task_state2 {
TaskState::InProgress { priority, status } => {
println!(
"✓ Deserialized InProgress variant with priority: {:?}, status: {:?}",
priority, status
);
assert_eq!(priority, Priority::Critical);
assert_eq!(status, Status::InProgress);
}
_ => panic!("Expected InProgress variant"),
}
// Demonstrate it works in structs too
println!("\n=== Nested Enum in Struct ===\n");
let task_json = serde_json::json!({
"title": "Fix bug",
"state": {
"Pending": {
"priority": "High"
}
}
});
let task: Task = serde_json::from_value(task_json).unwrap();
println!("✓ Deserialized Task with nested enum state");
assert_eq!(task.title, "Fix bug");
match task.state {
TaskState::Pending { priority } => {
assert_eq!(priority, Priority::High);
println!(" Task state: Pending with {:?} priority", priority);
}
_ => panic!("Expected Pending variant"),
}
println!("\n=== Success! ===");
println!("Nested enums now work automatically with #[derive(Instructor)]!");
println!("No manual SchemaType implementation needed!");
}