Skip to content

Commit

Permalink
Update fs error handling to log file path
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Aug 3, 2024
1 parent 0ad47cf commit eca5728
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
25 changes: 22 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ impl Config {
}
}

mod fs {
use super::*;
use std::fs;

pub use fs::create_dir_all;

pub fn read_to_string<P: AsRef<Path>>(path: P) -> Result<String> {
fs::read_to_string(path.as_ref()).map_err(|err| Error::file(path, err))
}

pub fn read<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
fs::read(path.as_ref()).map_err(|err| Error::file(path, err))
}

pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, content: C) -> Result<()> {
fs::write(path.as_ref(), content).map_err(|err| Error::file(path, err))
}
}

static USER_CONFIG: LazyLock<Mutex<Option<Vec<Arc<Node>>>>> = LazyLock::new(|| Mutex::new(None));

pub fn user_config() -> Option<Vec<Arc<Node>>> {
Expand Down Expand Up @@ -180,7 +199,7 @@ pub fn load_key() -> Result<Secret> {
if !key_path.exists() {
return Err(Error::KeyNotFound);
}
Ok(Secret::from(fs::read(key_path)?))
Ok(Secret::from(fs::read(&key_path)?))
}

pub fn load_key64() -> Result<u64> {
Expand All @@ -189,7 +208,7 @@ pub fn load_key64() -> Result<u64> {
return Err(Error::KeyNotFound);
}
Ok(u64::from_be_bytes(
fs::read(key64_path)?.try_into().unwrap(),
fs::read(&key64_path)?.try_into().unwrap(),
))
}

Expand All @@ -210,7 +229,7 @@ pub fn locate_local_config() -> Option<PathBuf> {

pub fn test_config() -> Result<Vec<Arc<Node>>> {
let local_config = locate_local_config().ok_or(Error::LocalConfigNotFound)?;
let toml = fs::read_to_string(local_config)?;
let toml = fs::read_to_string(&local_config)?;
// let local = include_str!("../Resolver.toml");
Config::try_parse(toml.as_str())
}
Expand Down
9 changes: 9 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum Error {
#[error("IO Error: {0}")]
Io(#[from] std::io::Error),

#[error("Unable to read file: `{0}`, {1}")]
File(String, std::io::Error),

#[error(transparent)]
Serde(#[from] serde_json::Error),

Expand Down Expand Up @@ -77,6 +80,12 @@ impl Error {
}
}

impl Error {
pub fn file<P: AsRef<std::path::Path>>(path: P, err: std::io::Error) -> Self {
Error::File(path.as_ref().display().to_string(), err)
}
}

impl Error {
pub fn config<T: std::fmt::Display>(msg: T) -> Self {
Error::Config(msg.to_string())
Expand Down
4 changes: 2 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub use serde::{de::DeserializeOwned, Deserialize, Serialize};
pub use serde_hex::{SerHex, Strict};
pub use std::collections::{HashMap, HashSet};
pub use std::fmt::{self, Display, Formatter};
pub use std::fs;
pub use std::path::PathBuf;
// pub use std::fs;
pub use std::path::{Path, PathBuf};
pub use std::str::FromStr;
pub use std::sync::atomic::AtomicBool;
pub use std::sync::atomic::{AtomicU64, Ordering};
Expand Down

0 comments on commit eca5728

Please sign in to comment.