Skip to main content

tokenless_core/
config.rs

1/// Validated configuration for tokenless components.
2///
3/// The `name` field must be non-empty.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct Config {
6    name: String,
7    description: Option<String>,
8}
9
10impl Config {
11    /// Create a new `Config` with a non-empty name.
12    ///
13    /// # Errors
14    ///
15    /// Returns [`crate::CoreError::App`] if `name` is empty or whitespace-only.
16    pub fn new(name: impl Into<String>) -> crate::Result<Self> {
17        let name = name.into();
18        if name.trim().is_empty() {
19            return Err(crate::CoreError::App(
20                "config name must not be empty".into(),
21            ));
22        }
23        Ok(Self {
24            name,
25            description: None,
26        })
27    }
28
29    /// Set an optional description.
30    #[must_use]
31    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
32        self.description = Some(desc.into());
33        self
34    }
35
36    /// Return the configuration name.
37    #[must_use]
38    pub fn name(&self) -> &str {
39        &self.name
40    }
41
42    /// Return the optional description.
43    #[must_use]
44    pub fn description(&self) -> Option<&str> {
45        self.description.as_deref()
46    }
47}