Skip to main content

shep_core/config/daemon/
error.rs

1use crate::secrets;
2use crate::values::UpDuration;
3use core::fmt;
4
5/// Error type returned from [`DaemonConfig::load`](crate::config::daemon::DaemonConfig::load).
6///
7/// `#[non_exhaustive]`: every `[daemon]` key this crate learns to validate
8/// brings its own rejection reason, and `deferred.md`'s daemon-config
9/// flags layer is a whole set of them at once.
10#[non_exhaustive]
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum DaemonConfigError {
13    /// `shep.toml` is invalid TOML (carries the parser message)
14    Toml(String),
15    /// A `SHEP_*` env var held an unparseable value (var name, value)
16    BadEnvValue(&'static str, String),
17    /// A `[daemon]` duration is below the floor that keeps the daemon from
18    /// spinning. Carries the key the user actually set: the TOML key or
19    /// the environment variable, whichever supplied the winning value.
20    BelowMinimum {
21        /// `max_cron_sleep` or `SHEP_MAX_CRON_SLEEP`.
22        key: &'static str,
23        /// The value as the user wrote it.
24        value: UpDuration,
25        /// The floor it failed.
26        min: UpDuration,
27    },
28    /// `[daemon] environment` is [`crate::secrets::ALL_ENVIRONMENTS`], the
29    /// secrets store's every-environment slot, or falls outside the grammar
30    /// [`crate::secrets`] keys and environment names share. Carries the
31    /// value as written.
32    InvalidEnvironment(String),
33}
34
35impl core::error::Error for DaemonConfigError {}
36
37impl fmt::Display for DaemonConfigError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Self::Toml(m) => write!(f, "invalid shep.toml: {m}"),
41            Self::BadEnvValue(var, v) => write!(f, "invalid value `{v}` for {var}"),
42            Self::BelowMinimum { key, value, min } => {
43                write!(
44                    f,
45                    "invalid value `{value}` for {key}: must be at least {min}"
46                )
47            }
48            Self::InvalidEnvironment(value) => write!(
49                f,
50                "invalid value `{value}` for environment: must be 1-{} bytes of \
51                     `[A-Za-z0-9._-]` not starting with `.`, and not `{}` (the secrets \
52                     store's every-environment slot)",
53                secrets::MAX_KEY_BYTES,
54                secrets::ALL_ENVIRONMENTS
55            ),
56        }
57    }
58}