haz-domain 0.2.0

Shared domain types for the haz task runner (identifiers, paths, errors).
Documentation
//! `MutexName`: an arbitrary name used to coordinate task mutual exclusion.

use nutype::nutype;

use crate::name::validation::{NameError, validate};

/// A validated mutex name.
///
/// Two tasks declaring the same `MutexName` MUST NOT run
/// concurrently. Construction is via [`MutexName::try_new`] (or
/// the standard [`std::str::FromStr`] interface).
///
/// # Examples
///
/// ```
/// use haz_domain::name::MutexName;
///
/// let m = MutexName::try_new("native_build").unwrap();
/// assert_eq!(m.to_string(), "native_build");
/// ```
#[nutype(
    validate(with = validate, error = NameError),
    derive(
        Debug,
        Clone,
        PartialEq,
        Eq,
        PartialOrd,
        Ord,
        Hash,
        Display,
        FromStr,
        AsRef,
    ),
)]
pub struct MutexName(String);

#[cfg(test)]
mod tests {
    use crate::name::MutexName;

    #[test]
    fn id_004_case_sensitive_equality() {
        let upper = MutexName::try_new("Native").unwrap();
        let lower = MutexName::try_new("native").unwrap();
        assert_ne!(upper, lower);
    }

    #[test]
    fn round_trip_via_try_new_and_display() {
        let n = MutexName::try_new("native_build").unwrap();
        assert_eq!(n.to_string(), "native_build");
    }
}