Skip to main content

lora_server/config/
errors.rs

1//! [`ConfigError`] and its conversions.
2
3use std::fmt;
4use std::num::ParseIntError;
5
6#[derive(Debug, PartialEq, Eq)]
7pub enum ConfigError {
8    UnknownArg(String),
9    MissingValue(&'static str),
10    EmptyValue(&'static str),
11    InvalidPort { value: String, reason: String },
12    InvalidSyncMode(String),
13    UnexpectedPositional(String),
14}
15
16impl fmt::Display for ConfigError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            ConfigError::UnknownArg(a) => write!(f, "unknown argument `{a}`"),
20            ConfigError::MissingValue(flag) => write!(f, "missing value for `{flag}`"),
21            ConfigError::EmptyValue(flag) => write!(f, "`{flag}` value must not be empty"),
22            ConfigError::InvalidPort { value, reason } => {
23                write!(f, "invalid port `{value}`: {reason}")
24            }
25            ConfigError::InvalidSyncMode(value) => {
26                write!(
27                    f,
28                    "invalid `--wal-sync-mode` `{value}`: expected `per-commit`, `group`, or `none`"
29                )
30            }
31            ConfigError::UnexpectedPositional(a) => {
32                write!(f, "unexpected positional argument `{a}`")
33            }
34        }
35    }
36}
37
38impl std::error::Error for ConfigError {}
39
40impl From<ParseIntError> for ConfigError {
41    fn from(_: ParseIntError) -> Self {
42        // Placeholder; we always build InvalidPort manually with the offending
43        // string so the user sees what they typed.
44        ConfigError::InvalidPort {
45            value: String::new(),
46            reason: "not a valid u16".into(),
47        }
48    }
49}