Skip to content

Commit

Permalink
fix(account_config): check if path is absolute and canonicalize
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-w committed Nov 22, 2024
1 parent a28d30f commit d86f217
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion edu-sync/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use edu_ws::{
ws,
};
use reqwest::Url;
use serde::{Deserialize, Serialize};
use serde::{Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, serde_conv, DisplayFromStr};
use thiserror::Error;
use tokio::{
Expand Down Expand Up @@ -115,6 +115,22 @@ pub fn check_dir(path: &PathBuf) -> Result<PathBuf, std::io::Error> {
std::fs::canonicalize(expanded_path)
}

// Custom deserializer function to check if the path is absolute and to
// canonicalize the path
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"));
}

check_dir(&path).map_err(|io_err| serde::de::Error::custom(format!("{io_err}")))
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct AccountConfig {
Expand All @@ -123,6 +139,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 d86f217

Please sign in to comment.