#![allow(clippy::unwrap_used, dead_code, missing_docs)]
use specta::{Type, Types};
use specta_swift::Swift;
#[derive(Type)]
enum HttpStatus {
Ok,
Created,
Accepted,
NoContent,
BadRequest,
Unauthorized,
NotFound,
InternalServerError,
}
#[derive(Type)]
enum Environment {
Development,
Staging,
Production,
Testing,
}
#[derive(Type)]
enum ApiResult {
Success,
SuccessWithData { data: String, status_code: u16 },
Error { message: String, code: u32 },
Loading,
}
#[derive(Type)]
enum UserAction {
Login,
Logout,
UpdateProfile {
name: String,
email: String,
avatar_url: Option<String>,
},
ChangePassword {
old_password: String,
new_password: String,
},
DeleteAccount,
}
#[derive(Type)]
enum JobState {
Queued,
Running,
Paused,
Completed,
Failed,
Cancelled,
}
#[derive(Type)]
enum NotificationType {
Email,
Push,
Sms,
Webhook {
url: String,
headers: Vec<(String, String)>,
retry_count: u32,
},
InApp {
title: String,
message: String,
priority: String,
},
}
#[derive(Type)]
enum Result<T, E> {
Ok(T),
Err(E),
}
#[derive(Type)]
enum EventType {
UserCreated,
UserUpdated {
user_id: u32,
changes: Vec<(String, String)>,
},
UserDeleted { user_id: u32, reason: String },
SystemEvent {
component: String,
level: String,
message: String,
},
}
#[derive(Type)]
enum FileType {
Image,
Video,
Audio,
Document,
Archive,
Unknown,
}
fn main() {
println!("🚀 String Enums Example - String enums and custom Codable");
println!("{}", "=".repeat(60));
let types = Types::default()
.register::<HttpStatus>()
.register::<Environment>()
.register::<ApiResult>()
.register::<UserAction>()
.register::<JobState>()
.register::<NotificationType>()
.register::<Result<String, String>>()
.register::<EventType>()
.register::<FileType>();
let swift = Swift::default();
let output = swift.export(&types, specta_serde::Format).unwrap();
println!("📝 Generated Swift code:\n");
println!("{}", output);
swift
.export_to(
"./examples/generated/StringEnums.swift",
&types,
specta_serde::Format,
)
.unwrap();
println!("✅ String enums exported to StringEnums.swift");
println!("\n🔍 Key Features Demonstrated:");
println!("• Pure string enums (String, Codable)");
println!("• Mixed enums with both simple and complex variants");
println!("• Custom Codable implementations for complex enums");
println!("• Struct generation for named field variants");
println!("• Generic enum support");
println!("• Proper Swift enum case naming");
println!("• Automatic protocol conformance");
println!("\n📋 String Enum Features:");
println!("• Automatic String and Codable conformance");
println!("• Simple enum cases without associated values");
println!("• Clean Swift enum representation");
println!("\n📋 Mixed Enum Features:");
println!("• Custom Codable implementation generation");
println!("• Struct generation for named field variants");
println!("• Proper key mapping (Rust → Swift naming)");
println!("• Error handling in Codable implementations");
println!("• Support for both simple and complex variants");
println!("\n💡 Generated Codable Features:");
println!("• CodingKeys enum for key mapping");
println!("• Custom init(from decoder:) implementation");
println!("• Custom encode(to encoder:) implementation");
println!("• Error handling for invalid data");
println!("• Support for nested data structures");
}