tesla-common/src/lib.rs

40 lines
962 B
Rust
Raw Normal View History

2024-02-12 11:11:38 +11:00
use serde::{Deserialize, Serialize};
2024-02-12 11:26:01 +11:00
use std::path::Path;
use thiserror::Error;
2024-02-12 11:11:38 +11:00
2024-02-12 11:31:15 +11:00
pub use teslatte::auth::{AccessToken, RefreshToken};
2024-05-25 13:54:32 +10:00
#[derive(Serialize, Deserialize, Clone, Debug)]
2024-02-12 11:11:38 +11:00
pub struct AuthInfo {
2024-02-12 11:16:19 +11:00
pub access_token: AccessToken,
pub refresh_token: Option<RefreshToken>,
2024-02-12 11:11:38 +11:00
}
2024-02-12 11:26:01 +11:00
impl AuthInfo {
2024-02-12 11:38:19 +11:00
pub fn save(&self, path: Option<&Path>) -> Result<Option<String>, ron::Error> {
let ser = ron::ser::to_string(self)?;
2024-02-12 11:38:19 +11:00
Ok(match path {
Some(path) => std::fs::write(path, ser.clone())
.map(|_v| None)
.unwrap_or(Some(ser)),
None => Some(ser),
})
2024-02-12 11:26:01 +11:00
}
}
2024-05-25 14:22:03 +10:00
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SuccessfulAuth {
locale: String,
code: String,
state: String,
issuer: String,
}
2024-02-12 11:26:01 +11:00
#[derive(Error, Debug)]
pub enum SaveError {
#[error("stdio error")]
StdIo(#[from] std::io::Error),
#[error("ron")]
RonSpanned(#[from] ron::Error),
}