supabase_rust_client/models.rs
1// src/models.rs
2
3// Define Rust structs corresponding to the 'models' section in supabase_interaction.ssot
4// Example based on the 'item' model in the SSOT:
5
6// Potentially use crates like serde for serialization/deserialization
7// and uuid for the ID type.
8
9use chrono::{DateTime, Utc};
10use serde::{Deserialize, Serialize};
11use uuid::Uuid;
12
13/// Represents the 'item' model structure defined in the SSOT.
14/// Corresponds to the 'items' table in Supabase.
15#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
16pub struct Item {
17 #[serde(default = "Uuid::new_v4")] // Generate UUID if missing, useful for inserts
18 pub id: Uuid,
19 pub user_id: Uuid, // Assuming user_id is known when creating/fetching
20 pub name: String,
21 #[serde(skip_serializing_if = "Option::is_none")] // Don't include in JSON if None
22 pub description: Option<String>,
23 #[serde(default = "Utc::now")]
24 pub created_at: DateTime<Utc>,
25 #[serde(default = "Utc::now")]
26 pub updated_at: DateTime<Utc>,
27}
28
29/// Represents authentication credentials based on SSOT validation rules.
30#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
31pub struct AuthCredentials {
32 // Add validation attributes if using a validation library
33 pub email: String,
34 pub password: String,
35}
36
37/// Represents a Supabase User, typically returned after authentication.
38#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
39pub struct User {
40 pub id: Uuid,
41 pub aud: String, // Audience
42 pub role: Option<String>,
43 pub email: Option<String>,
44 pub phone: Option<String>,
45 pub confirmation_sent_at: Option<DateTime<Utc>>,
46 pub confirmed_at: Option<DateTime<Utc>>,
47 pub email_confirmed_at: Option<DateTime<Utc>>,
48 pub phone_confirmed_at: Option<DateTime<Utc>>,
49 pub recovery_sent_at: Option<DateTime<Utc>>,
50 pub last_sign_in_at: Option<DateTime<Utc>>,
51 // pub app_metadata: serde_json::Value, // Use Value for flexible metadata
52 // pub user_metadata: serde_json::Value,
53 pub created_at: DateTime<Utc>,
54 pub updated_at: DateTime<Utc>,
55 // Add other relevant fields from Supabase Auth user object as needed
56}
57
58// --- From Trait Implementation ---
59
60impl From<supabase_rust_auth::User> for User {
61 fn from(auth_user: supabase_rust_auth::User) -> Self {
62 // Attempt to parse UUID and timestamps, providing defaults on failure
63 let parsed_id = Uuid::parse_str(&auth_user.id).unwrap_or_else(|e| {
64 eprintln!(
65 "Warning: Failed to parse user ID '{}' as UUID: {}. Using default UUID.",
66 auth_user.id, e
67 );
68 Uuid::nil() // Or Uuid::new_v4() if a default is not appropriate
69 });
70
71 let parsed_created_at = DateTime::parse_from_rfc3339(&auth_user.created_at)
72 .map(|dt| dt.with_timezone(&Utc))
73 .unwrap_or_else(|e| {
74 eprintln!(
75 "Warning: Failed to parse created_at '{}': {}. Using current time.",
76 auth_user.created_at, e
77 );
78 Utc::now()
79 });
80
81 let parsed_updated_at = DateTime::parse_from_rfc3339(&auth_user.updated_at)
82 .map(|dt| dt.with_timezone(&Utc))
83 .unwrap_or_else(|e| {
84 eprintln!(
85 "Warning: Failed to parse updated_at '{}': {}. Using current time.",
86 auth_user.updated_at, e
87 );
88 Utc::now()
89 });
90
91 User {
92 id: parsed_id,
93 email: auth_user.email,
94 phone: auth_user.phone,
95 created_at: parsed_created_at,
96 updated_at: parsed_updated_at,
97 // Set missing fields to defaults
98 aud: String::new(), // Or some default? Check Supabase docs
99 role: None,
100 confirmation_sent_at: None,
101 confirmed_at: None,
102 email_confirmed_at: None,
103 phone_confirmed_at: None,
104 recovery_sent_at: None,
105 last_sign_in_at: None,
106 // app_metadata: Default::default(), // If added back as serde_json::Value
107 // user_metadata: Default::default(), // If added back as serde_json::Value
108 }
109 }
110}
111
112// Add other models defined in SSOT here.
113
114// Define AuthCredentials struct based on SSOT validation rules
115// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
116// pub struct AuthCredentials {
117// pub email: String,
118// pub password: String,
119// }
120
121// Define User struct based on Supabase response
122// #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
123// pub struct User {
124// pub id: Uuid,
125// pub email: Option<String>,
126// // Add other relevant user fields
127// }