#![allow(clippy::unwrap_used, dead_code, missing_docs)]
use specta::{Type, Types};
use specta_swift::Swift;
#[derive(Type)]
struct Primitives {
small_int: i8,
unsigned_small: u8,
short_int: i16,
unsigned_short: u16,
regular_int: i32,
unsigned_int: u32,
long_int: i64,
unsigned_long: u64,
single_precision: f32,
double_precision: f64,
is_active: bool,
single_char: char,
name: String,
optional_name: Option<String>,
tags: Vec<String>,
scores: Vec<f64>,
user_ids: Vec<u32>,
matrix: Vec<Vec<f64>>,
string_pairs: Vec<(String, String)>,
}
#[derive(Type)]
enum Status {
Active,
Pending(String),
Error(String, u32),
Loading {
progress: f32,
message: Option<String>,
},
}
#[derive(Type)]
struct ApiResponse<T, E> {
data: Option<T>,
error: Option<E>,
status_code: u16,
headers: Vec<(String, String)>,
}
#[derive(Type)]
struct User {
id: u32,
username: String,
email: String,
profile: UserProfile,
preferences: UserPreferences,
status: Status,
metadata: Option<UserMetadata>,
}
#[derive(Type)]
struct UserProfile {
first_name: String,
last_name: String,
bio: Option<String>,
avatar_url: Option<String>,
birth_date: Option<String>,
}
#[derive(Type)]
struct UserPreferences {
theme: String,
language: String,
notifications_enabled: bool,
privacy_level: u8,
}
#[derive(Type)]
struct UserMetadata {
created_at: String,
last_login: Option<String>,
login_count: u32,
is_verified: bool,
}
fn main() {
println!("🚀 Basic Types Example - Generating Swift from Rust types");
println!("{}", "=".repeat(60));
let types = Types::default()
.register::<Primitives>()
.register::<Status>()
.register::<ApiResponse<String, String>>()
.register::<User>()
.register::<UserProfile>()
.register::<UserPreferences>()
.register::<UserMetadata>();
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/BasicTypes.swift",
&types,
specta_serde::Format,
)
.unwrap();
println!("✅ Basic types exported to BasicTypes.swift");
println!("\n🔍 Key Features Demonstrated:");
println!("• Primitive type mappings (i32 → Int32, f64 → Double, etc.)");
println!("• Optional types (Option<String> → String?)");
println!("• Collections (Vec<T> → [T])");
println!("• Nested collections (Vec<Vec<f64>> → [[Double]])");
println!("• Enum variants (unit, tuple, named fields)");
println!("• Generic types with type parameters");
println!("• Complex nested struct relationships");
println!("• Tuple types ((String, String) → (String, String))");
}