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