use serde::{Deserialize, Serialize};
+use xdg::BaseDirectories;
+
+use super::OurError;
#[derive(Serialize, Deserialize, Debug)]
pub struct AuthConfig {
}
impl AuthConfig {
- pub fn load() -> Self {
- let xdg_dirs = xdg::BaseDirectories::with_prefix("mastodonochrome")
- .unwrap();
+ pub fn load() -> Result<Self, OurError> {
+ let xdg_dirs = match BaseDirectories::with_prefix("mastodonochrome") {
+ Err(e) => Err(OurError::Fatal(
+ format!("unable to get config directory: {}", e))),
+ Ok(d) => Ok(d),
+ }?;
+
let authfile = xdg_dirs.get_config_file("auth");
- let auth = std::fs::read_to_string(authfile).unwrap();
- let auth: Self = serde_json::from_str(&auth).unwrap();
- auth
+ let authdata = match std::fs::read_to_string(&authfile) {
+ Err(e) => Err(OurError::Fatal(
+ format!("unable to read config file '{}': {}",
+ authfile.display(), e))),
+ Ok(d) => Ok(d),
+ }?;
+ let auth: Self = match serde_json::from_str(&authdata) {
+ Err(e) => Err(OurError::Fatal(
+ format!("unable to parse config file '{}': {}",
+ authfile.display(), e))),
+ Ok(d) => Ok(d),
+ }?;
+ Ok(auth)
}
}
use mastodonochrome::auth::AuthConfig;
use std::io::Read;
-fn main() {
- let auth = AuthConfig::load();
+fn main() -> Result<(), mastodonochrome::OurError> {
+ let auth = AuthConfig::load()?;
let client = reqwest::blocking::Client::new();
let mut req = client.get(auth.instance_url + "/api/v1/streaming/user")
let read = &buf[..sz];
dbg!(read);
}
+
+ Ok(())
}