From b1a49d1dbb2da84b34008ad70b6683134ffc8648 Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Sun, 10 May 2026 10:14:58 -0400 Subject: [PATCH] handle loading --- .../src/core/providers/payload/web_login.rs | 70 +++++++++++++++---- 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/crates/desktop/artcraft/src/core/providers/payload/web_login.rs b/crates/desktop/artcraft/src/core/providers/payload/web_login.rs index c9180c755c..a874c2a7cc 100644 --- a/crates/desktop/artcraft/src/core/providers/payload/web_login.rs +++ b/crates/desktop/artcraft/src/core/providers/payload/web_login.rs @@ -11,10 +11,10 @@ const CURRENT_VERSION: u8 = 1; pub struct WebLoginData { pub cookies_header: Option, pub additional_headers: Option>, - + pub username: Option, pub email_address: Option, - + pub created_at: Option>, pub updated_at: Option>, } @@ -30,53 +30,93 @@ impl WebLoginData { updated_at: None, } } - - pub fn load_from_file>(file_path: P) -> Result { - unimplemented!() + + pub fn load_from_file>(file_path: P) -> Result { + let contents = std::fs::read_to_string(file_path) + .map_err(WebLoginDataError::IoError)?; + + let serializable: WebLoginDataSerializable = serde_json::from_str(&contents) + .map_err(WebLoginDataError::DeserializeError)?; + + Ok(Self { + cookies_header: serializable.cookies_header, + additional_headers: serializable.additional_headers, + username: serializable.username, + email_address: serializable.email_address, + created_at: serializable.created_at, + updated_at: serializable.updated_at, + }) } - pub fn save_to_file>(&self, file_path: P) -> Result<(), ArtcraftError> { - unimplemented!() + pub fn save_to_file>(&self, file_path: P) -> Result<(), WebLoginDataError> { + let serializable = WebLoginDataSerializable { + version: CURRENT_VERSION, + cookies_header: self.cookies_header.clone(), + additional_headers: self.additional_headers.clone(), + username: self.username.clone(), + email_address: self.email_address.clone(), + created_at: self.created_at, + updated_at: self.updated_at, + }; + + let contents = serde_json::to_string_pretty(&serializable) + .map_err(WebLoginDataError::SerializeError)?; + + std::fs::write(file_path, contents) + .map_err(WebLoginDataError::IoError)?; + + Ok(()) } } #[derive(Debug)] pub enum WebLoginDataError { - InvalidVersion, IoError(std::io::Error), + DeserializeError(serde_json::Error), + SerializeError(serde_json::Error), } -impl Error for WebLoginDataError {} +impl Error for WebLoginDataError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self { + WebLoginDataError::IoError(e) => Some(e), + WebLoginDataError::DeserializeError(e) => Some(e), + WebLoginDataError::SerializeError(e) => Some(e), + _ => None, + } + } +} impl Display for WebLoginDataError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - WebLoginDataError::InvalidVersion => write!(f, "Invalid version"), WebLoginDataError::IoError(e) => write!(f, "IO error: {}", e), + WebLoginDataError::DeserializeError(e) => write!(f, "Deserialization error: {}", e), + WebLoginDataError::SerializeError(e) => write!(f, "Serialization error: {}", e), } - } + } } #[derive(Serialize, Deserialize)] #[serde(rename_all = "snake_case")] struct WebLoginDataSerializable { version: u8, - + #[serde(skip_serializing_if = "Option::is_none")] cookies_header: Option, - + #[serde(skip_serializing_if = "Option::is_none")] additional_headers: Option>, #[serde(skip_serializing_if = "Option::is_none")] username: Option, - + #[serde(skip_serializing_if = "Option::is_none")] email_address: Option, #[serde(skip_serializing_if = "Option::is_none")] created_at: Option>, - + #[serde(skip_serializing_if = "Option::is_none")] updated_at: Option>, }