1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#![allow(clippy::multiple_crate_versions)]

use std::{marker::Sized, path::PathBuf};

pub use derive_macros::*;
#[cfg(feature = "dirs")]
pub use dirs::{self};
use duplicate::duplicate_item;
#[cfg(feature = "json")]
pub use json::{self};
use thiserror::Error;
#[cfg(feature = "toml")]
pub use toml::{self};
#[cfg(feature = "yaml")]
pub use yaml::{self};

#[derive(Debug, Error)]
pub enum ConfigError {
    #[error("None")]
    None,

    #[error("{0}")]
    Io(#[from] std::io::Error),

    #[cfg(feature = "json")]
    #[error("{0}")]
    Json(#[from] json::Error),

    #[cfg(feature = "toml")]
    #[error("{0}")]
    TomlDe(#[from] toml::de::Error),

    #[cfg(feature = "toml")]
    #[error("{0}")]
    TomlSer(#[from] toml::ser::Error),

    #[cfg(feature = "yaml")]
    #[error("{0}")]
    Yaml(#[from] yaml::Error),
}

#[duplicate_item(
    language_struct_name;
    [ DeriveJsonConfig ];
    [ DeriveTomlConfig ];
    [ DeriveYamlConfig ];
)]
pub trait language_struct_name {
    /// # Errors
    ///
    /// Will return `Err` if `dirs::config_dir` returns err.
    fn path() -> Result<PathBuf, ConfigError>;

    /// # Errors
    fn save(&self) -> Result<(), ConfigError>;

    /// # Errors
    fn load() -> Result<Self, ConfigError>
    where
        Self: Sized;
}