Skip to main content

derive_config/
lib.rs

1#![allow(clippy::multiple_crate_versions)]
2
3use std::{marker::Sized, path::PathBuf};
4
5pub use derive_macros::*;
6#[cfg(feature = "dirs")]
7pub use dirs::{self};
8use duplicate::duplicate_item;
9#[cfg(feature = "json")]
10pub use json::{self};
11use thiserror::Error;
12#[cfg(feature = "toml")]
13pub use toml::{self};
14#[cfg(feature = "yaml")]
15pub use yaml::{self};
16
17#[derive(Debug, Error)]
18pub enum ConfigError {
19    #[error("None")]
20    None,
21
22    #[error("{0}")]
23    Io(#[from] std::io::Error),
24
25    #[cfg(feature = "json")]
26    #[error("{0}")]
27    Json(#[from] json::Error),
28
29    #[cfg(feature = "toml")]
30    #[error("{0}")]
31    TomlDe(#[from] toml::de::Error),
32
33    #[cfg(feature = "toml")]
34    #[error("{0}")]
35    TomlSer(#[from] toml::ser::Error),
36
37    #[cfg(feature = "yaml")]
38    #[error("{0}")]
39    Yaml(#[from] yaml::Error),
40}
41
42#[duplicate_item(
43    language_struct_name;
44    [ DeriveJsonConfig ];
45    [ DeriveTomlConfig ];
46    [ DeriveYamlConfig ];
47)]
48pub trait language_struct_name {
49    /// # Errors
50    /// Will return `Err` if `dirs::config_dir` fails.
51    fn path() -> Result<PathBuf, ConfigError>;
52
53    /// # Errors
54    /// Will return `Err` if `Self::path`, `File::open`, `format::to_string`, or `File::write_all` fails.
55    fn save(&self) -> Result<(), ConfigError>;
56
57    /// # Errors
58    /// Will return `Err` if `Self::path`, `File::open`, `format::to_string`, or `File::write_all` fails.
59    fn and_save(self) -> Result<Self, ConfigError>
60    where
61        Self: Sized;
62
63    /// # Errors
64    /// Will return `Err` if `Self::path`, `File::open`, `File::read_to_string`, `File::rewind`, or `format::from_str` fails.
65    fn load() -> Result<Self, ConfigError>
66    where
67        Self: Sized;
68}