Skip to content

Commit

Permalink
Some nicety stuff, cleans up weird erorr work
Browse files Browse the repository at this point in the history
We were defining a new error type and then casting it to another,
different error type immediately.

There's no reason to do this because we have an existing error type
system that handles this kind of casting better. Should just be usin
that
  • Loading branch information
LemonInTheDark committed Aug 30, 2024
1 parent 494b083 commit 1d6ce51
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ rustflags = ["-C", "target-feature=+crt-static"]

[target.x86_64-unknown-linux-musl]
rustflags = ["-C", "target-feature=+crt-static"]

[profile.profiling]
inherits = "release"
debug = true
3 changes: 3 additions & 0 deletions hypnagogic_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@ fn process_icon(
match err {
ConfigError::Template(template_err) => {
match template_err {
TemplateError::NoTemplateDir(dir_path) => {
Error::NoTemplateFolder(dir_path)
}
TemplateError::FailedToFindTemplate(template_string, expected_path) => {
Error::TemplateNotFound {
source_config,
Expand Down
2 changes: 2 additions & 0 deletions hypnagogic_core/src/config/template_resolver/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use toml::Value;

#[derive(Debug, Error)]
pub enum TemplateError {
#[error("Template dir not found while creating FileResolver {0}")]
NoTemplateDir(PathBuf),
#[error("Failed to find template: `{0}`, expected `{1}`")]
FailedToFindTemplate(String, PathBuf),
#[error("Generic toml parse error while resolving template: {0}")]
Expand Down
20 changes: 2 additions & 18 deletions hypnagogic_core/src/config/template_resolver/file_resolver.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use core::default::Default;
use core::result::Result::{Err, Ok};
use std::fmt::Formatter;
use std::fs;
use std::path::{Path, PathBuf};

Expand All @@ -16,28 +15,13 @@ pub struct FileResolver {
path: PathBuf,
}

#[derive(Debug)]
pub struct NoTemplateDirError(PathBuf);

impl std::fmt::Display for NoTemplateDirError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Template dir not found while creating FileResolver: {:?}",
self.0
)
}
}

impl std::error::Error for NoTemplateDirError {}

impl FileResolver {
/// Creates a new `FileResolver` with the given path
/// # Errors
/// Returns an error if `path` does not exist.
pub fn new(path: &Path) -> Result<Self, NoTemplateDirError> {
pub fn new(path: &Path) -> Result<Self, TemplateError> {
let pathbuf =
fs::canonicalize(path).map_err(|_e| NoTemplateDirError(path.to_path_buf()))?;
fs::canonicalize(path).map_err(|_e| TemplateError::NoTemplateDir(path.to_path_buf()))?;
Ok(FileResolver { path: pathbuf })
}
}
Expand Down

0 comments on commit 1d6ce51

Please sign in to comment.