canarytrap 0.2.0

Rust-native canary tokens and honeytokens for early detection of suspicious access to secrets and configuration.
Documentation
use thiserror::Error;

/// Errors returned by CanaryTrap operations.
///
/// # Example
///
/// ```
/// use canarytrap::Error;
///
/// let error = Error::NotFound("prod.db.password".to_owned());
/// assert_eq!(
///     error.to_string(),
///     "canary token not found: prod.db.password"
/// );
/// ```
#[derive(Debug, PartialEq, Eq, Error)]
#[non_exhaustive]
pub enum Error {
    /// A canary token was not found by id or name.
    #[error("canary token not found: {0}")]
    NotFound(String),

    /// A canary token with the same name is already registered.
    #[error("canary token name already registered: {0}")]
    DuplicateName(String),

    /// A canary token with the same id is already registered.
    ///
    /// This should be extremely unlikely when ids are generated using UUIDv4.
    #[error("canary token id already registered: {0}")]
    DuplicateId(String),

    /// A generator received an invalid character length.
    ///
    /// # Example
    ///
    /// ```
    /// use canarytrap::Error;
    ///
    /// let error = Error::InvalidLength(7);
    /// assert_eq!(error.to_string(), "invalid generator length: 7");
    /// ```
    #[error("invalid generator length: {0}")]
    InvalidLength(usize),

    /// A generator received an invalid random byte count.
    ///
    /// # Example
    ///
    /// ```
    /// use canarytrap::Error;
    ///
    /// let error = Error::InvalidByteCount(4);
    /// assert_eq!(error.to_string(), "invalid generator byte count: 4");
    /// ```
    #[error("invalid generator byte count: {0}")]
    InvalidByteCount(usize),

    /// A canary token builder was used without specifying a kind.
    ///
    /// # Example
    ///
    /// ```
    /// use canarytrap::Error;
    ///
    /// let error = Error::MissingKind;
    /// assert_eq!(
    ///     error.to_string(),
    ///     "canary token builder requires a kind"
    /// );
    /// ```
    #[error("canary token builder requires a kind")]
    MissingKind,

    /// A canary token builder was used without specifying a value.
    ///
    /// # Example
    ///
    /// ```
    /// use canarytrap::Error;
    ///
    /// let error = Error::MissingValue;
    /// assert_eq!(
    ///     error.to_string(),
    ///     "canary token builder requires a value or generate_value()"
    /// );
    /// ```
    #[error("canary token builder requires a value or generate_value()")]
    MissingValue,
}

/// A specialized `Result` type for CanaryTrap operations.
pub type Result<T> = std::result::Result<T, Error>;