diff --git a/Cargo.lock b/Cargo.lock index e5b651a..9449ce1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -127,6 +127,9 @@ name = "bitflags" version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +dependencies = [ + "serde", +] [[package]] name = "block-buffer" @@ -1102,6 +1105,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "ron" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94" +dependencies = [ + "base64", + "bitflags 2.4.2", + "serde", + "serde_derive", +] + [[package]] name = "rustc-demangle" version = "0.1.23" @@ -1502,10 +1517,12 @@ dependencies = [ [[package]] name = "tesla-common" -version = "0.1.1" +version = "0.2.0" dependencies = [ + "ron", "serde", "teslatte", + "thiserror", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 350306d..797247f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tesla-common" -version = "0.1.1" +version = "0.2.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -8,3 +8,5 @@ edition = "2021" [dependencies] serde = { version = "1.0", features = ["derive"] } teslatte = { git = "https://git.alexjanka.com/alex/teslatte" } +ron = "0.8" +thiserror = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 1a0ba8a..70b7079 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,25 @@ use serde::{Deserialize, Serialize}; +use std::path::Path; use teslatte::auth::{AccessToken, RefreshToken}; +use thiserror::Error; #[derive(Serialize, Deserialize, Clone)] pub struct AuthInfo { pub access_token: AccessToken, pub refresh_token: Option, } + +impl AuthInfo { + pub fn save(&self, path: &Path) -> Result<(), SaveError> { + std::fs::write(path, ron::ser::to_string(self)?)?; + Ok(()) + } +} + +#[derive(Error, Debug)] +pub enum SaveError { + #[error("stdio error")] + StdIo(#[from] std::io::Error), + #[error("ron")] + RonSpanned(#[from] ron::Error), +}