lib-humus-configuration 0.2.0

Helper crate for reading configuration files into data structures using serde.
Documentation
// SPDX-FileCopyrightText: 2025 Slatian
//
// SPDX-License-Identifier: LGPL-3.0-or-later

use serde::Deserialize;

use std::fmt;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::error::Error;

/// Reads from a specified filepath, parses as toml and returns as the
/// desired deserialized type.
///
/// Example:
/// ```
/// use serde::Deserialize;
/// use lib_humus::read_toml_from_file;
///
/// #[derive(Deserialize)]
/// struct Config {
///		name: String,
/// }
///
///	let cfg: Config = read_toml_from_file("example_config.toml").unwrap();
///	println!("Hello {}!", cfg.name);
/// ```
///
pub fn read_toml_from_file<T>(path: impl AsRef<Path>) -> Result<T,TomlError>
where T: for<'de> Deserialize<'de> {
	let text = match fs::read_to_string(&path) {
		Ok(t) => t,
		Err(e) => {
			return Err(TomlError::FileError{
				path: path.as_ref().into(),
				io_error: e
			});
		}
	};
	match toml::from_str(&text) {
		Ok(t) => Ok(t),
		Err(e) => {
			return Err(TomlError::ParseError{
				path: path.as_ref().into(),
				toml_error: e
			});
		}
	}
}

/// Returned when [read_toml_from_file()] fails.
///
/// [read_toml_from_file]: ./fn.read_toml_from_file.html
#[derive(Debug)]
pub enum TomlError {
	
	/// There was an error reading the file from the filesystem.
	FileError{
		/// The path the file is supposed to be at
		path: PathBuf,
		/// What went wrong
		io_error: std::io::Error
	},
	
	/// There was an error parsing the file content as toml.
	ParseError{
		/// The path the file was read from
		path: PathBuf,
		/// What went wrong
		toml_error: toml::de::Error
	},
}

impl fmt::Display for TomlError {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		match self {
			Self::FileError{path, io_error} =>
				write!(f,"Error while reading file '{path:?}': {io_error}"),
			Self::ParseError{path, toml_error} =>
				write!(f,"Unable to parse file as toml '{path:?}':\n{toml_error}"),
		}
	}
}

impl Error for TomlError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
			Self::FileError{io_error, ..} =>
				Some(io_error),
			Self::ParseError{toml_error, ..} =>
				Some(toml_error),
        }
    }
}