Skip to content

Commit

Permalink
feat(cli): error if path in config is not absolute
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-w committed Nov 22, 2024
1 parent 47ac314 commit 43bfefb
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions edu-sync/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use serde::{Deserialize, Deserializer};
use std::path::{Path, PathBuf};
use std::{
borrow::Cow,
collections::BTreeMap,
convert::Infallible,
fmt::{self, Display},
io::{self, ErrorKind},
path::{Path, PathBuf},
sync::OnceLock,
};

Expand All @@ -14,7 +15,7 @@ use edu_ws::{
ws,
};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde::Serialize;
use serde_with::{serde_as, serde_conv, DisplayFromStr};
use thiserror::Error;
use tokio::{
Expand Down Expand Up @@ -105,6 +106,21 @@ impl CourseConfigs {
}
}

// Custom deserializer function
fn deserialize_absolute_path<'de, D>(deserializer: D) -> Result<PathBuf, D::Error>
where
D: Deserializer<'de>,
{
let path_str = String::deserialize(deserializer)?;
let path = PathBuf::from(path_str);

if !path.is_absolute() {
return Err(serde::de::Error::custom("Path must be absolute"));
}

Ok(path)
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct AccountConfig {
Expand All @@ -113,6 +129,7 @@ pub struct AccountConfig {
#[serde(flatten)]
pub id: Id,
pub token: Token,
#[serde(deserialize_with = "deserialize_absolute_path")]
pub path: PathBuf,
#[serde(default)]
pub courses: CourseConfigs,
Expand Down

0 comments on commit 43bfefb

Please sign in to comment.