Skip to content

Commit

Permalink
use NoteError in config and remove ConfgError
Browse files Browse the repository at this point in the history
  • Loading branch information
falk-werner committed Jan 28, 2024
1 parent 42bed4a commit d52fe2e
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use home::{home_dir};

use std::fs;
use std::path::{Path, PathBuf};
use core::fmt::Display;

use crate::noteerror::{NoteError,NoteResult};

const DEFAULT_CONFIG_FILENAME: &str = ".noters.yaml";

Expand Down Expand Up @@ -32,23 +33,6 @@ fn get_home_dir() -> PathBuf {
home_dir().unwrap_or(PathBuf::from("."))
}

#[derive(Debug)]
struct ConfigError {
message: String
}

impl ConfigError {
fn new(message: &str) -> Self {
ConfigError { message: message.into() }
}
}

impl<E: Display> From<E> for ConfigError {
fn from(value: E) -> Self {
ConfigError { message: value.to_string() }
}
}

impl Config {

/// Creates a new config with default values.
Expand Down Expand Up @@ -83,7 +67,7 @@ impl Config {
match Config::from_file(filename.as_path()) {
Ok(config) => config,
Err(error) => {
eprintln!("warning: failed to load config {:?}: {}", filename, error.message);
eprintln!("warning: failed to load config {:?}: {}", filename, error.to_string());
let config = Config::new(filename.as_path());
config.save();
config
Expand All @@ -92,12 +76,12 @@ impl Config {
}

/// Try to load the config from a given path.
fn from_file(filename: &Path) -> Result<Self, ConfigError> {
fn from_file(filename: &Path) -> NoteResult<Self> {
let text = fs::read_to_string(filename)?;
let config_file = serde_yaml::from_str::<ConfigFile>(&text)?;

if config_file.meta.version != 1 {
return Err(ConfigError::new("unknown file format (version mismatch)"));
return Err(NoteError::new("unknown file format (version mismatch)"));
}

Ok(Config {
Expand Down

0 comments on commit d52fe2e

Please sign in to comment.