Skip to main content

uselesskey_core/
error.rs

1use alloc::string::String;
2
3use thiserror::Error;
4
5/// Errors for `uselesskey-core`.
6///
7/// This crate is deliberately “test-first”: many operations are infallible by design.
8/// We still surface IO and environment errors because those are common in test harnesses.
9#[derive(Debug, Error)]
10pub enum Error {
11    #[error("environment variable `{var}` is not set")]
12    MissingEnvVar { var: String },
13
14    #[error("failed to parse seed from environment variable `{var}`: {message}")]
15    InvalidSeed { var: String, message: String },
16
17    #[cfg(feature = "std")]
18    #[error(transparent)]
19    Io(#[from] std::io::Error),
20}
21
22#[cfg(all(test, feature = "std"))]
23mod tests {
24    use super::Error;
25
26    #[test]
27    fn error_messages_are_readable() {
28        let missing = Error::MissingEnvVar {
29            var: "MY_VAR".to_string(),
30        };
31        assert_eq!(
32            missing.to_string(),
33            "environment variable `MY_VAR` is not set"
34        );
35
36        let invalid = Error::InvalidSeed {
37            var: "MY_VAR".to_string(),
38            message: "bad seed".to_string(),
39        };
40        assert_eq!(
41            invalid.to_string(),
42            "failed to parse seed from environment variable `MY_VAR`: bad seed"
43        );
44
45        #[cfg(feature = "std")]
46        {
47            let io_err: Error = std::io::Error::other("io-fail").into();
48            assert_eq!(io_err.to_string(), "io-fail");
49        }
50    }
51}