improved some errors
This commit is contained in:
23
src/fomod.rs
23
src/fomod.rs
@@ -2,9 +2,10 @@
|
||||
// https://github.com/luctius/fomod/
|
||||
// Original license: MIT / Apache-2.0
|
||||
|
||||
use std::{error::Error, fs, path::Path};
|
||||
use std::{fs, io, path::Path};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
pub struct Info {
|
||||
@@ -22,6 +23,15 @@ pub struct Info {
|
||||
pub category_id: Option<usize>,
|
||||
}
|
||||
|
||||
impl Info {
|
||||
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, FOModError> {
|
||||
let data = fs::read_to_string(path)?;
|
||||
let config = quick_xml::de::from_str(&data)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq)]
|
||||
pub struct Config {
|
||||
#[serde(rename = "moduleName")]
|
||||
@@ -44,7 +54,7 @@ pub struct Config {
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn Error>> {
|
||||
pub fn load_from_file(path: impl AsRef<Path>) -> Result<Self, FOModError> {
|
||||
let data = fs::read_to_string(path)?;
|
||||
let config = quick_xml::de::from_str(&data)?;
|
||||
|
||||
@@ -52,6 +62,15 @@ impl Config {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Error, Debug)]
|
||||
pub enum FOModError {
|
||||
#[error("Failed to read file")]
|
||||
Io(#[from] io::Error),
|
||||
|
||||
#[error("Failed to parse config")]
|
||||
Parse(#[from] quick_xml::de::DeError),
|
||||
}
|
||||
|
||||
#[derive(
|
||||
Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, PartialOrd, Ord, Hash, Default,
|
||||
)]
|
||||
|
||||
Reference in New Issue
Block a user