use core::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum TriggerSource {
Reveal,
WithValue,
Registry,
Serialize,
Custom(&'static str),
}
impl TriggerSource {
pub const BUILT_IN: &'static [TriggerSource] = &[
Self::Reveal,
Self::WithValue,
Self::Registry,
Self::Serialize,
];
#[must_use]
pub fn custom(name: &'static str) -> Self {
Self::Custom(name.trim())
}
pub fn from_static(input: &'static str) -> Result<Self, ParseTriggerSourceError> {
const CUSTOM_PREFIX: &str = "custom:";
let input = input.trim();
if input.is_empty() {
return Err(ParseTriggerSourceError::new());
}
let has_custom_prefix = input
.get(..CUSTOM_PREFIX.len())
.is_some_and(|prefix| prefix.eq_ignore_ascii_case(CUSTOM_PREFIX));
if has_custom_prefix {
let name = input[CUSTOM_PREFIX.len()..].trim();
if name.is_empty() {
return Err(ParseTriggerSourceError::new());
}
return Ok(Self::Custom(name));
}
if input.eq_ignore_ascii_case("reveal") || input.eq_ignore_ascii_case("revealed") {
return Ok(Self::Reveal);
}
if input.eq_ignore_ascii_case("with-value")
|| input.eq_ignore_ascii_case("with_value")
|| input.eq_ignore_ascii_case("withvalue")
|| input.eq_ignore_ascii_case("value")
{
return Ok(Self::WithValue);
}
if input.eq_ignore_ascii_case("registry") {
return Ok(Self::Registry);
}
if input.eq_ignore_ascii_case("serialize")
|| input.eq_ignore_ascii_case("serialization")
|| input.eq_ignore_ascii_case("serde")
{
return Ok(Self::Serialize);
}
Err(ParseTriggerSourceError::new())
}
#[must_use]
pub fn as_builtin_str(&self) -> Option<&'static str> {
match self {
Self::Reveal => Some("reveal"),
Self::WithValue => Some("with-value"),
Self::Registry => Some("registry"),
Self::Serialize => Some("serialize"),
Self::Custom(_) => None,
}
}
#[must_use]
pub fn as_custom_str(&self) -> Option<&'static str> {
match self {
Self::Custom(name) => Some(name),
_ => None,
}
}
#[must_use]
pub fn is_builtin(&self) -> bool {
self.as_builtin_str().is_some()
}
#[must_use]
pub fn is_custom(&self) -> bool {
matches!(self, Self::Custom(_))
}
}
impl fmt::Display for TriggerSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Reveal => f.write_str("reveal"),
Self::WithValue => f.write_str("with-value"),
Self::Registry => f.write_str("registry"),
Self::Serialize => f.write_str("serialize"),
Self::Custom(name) => write!(f, "custom:{name}"),
}
}
}
impl TryFrom<&'static str> for TriggerSource {
type Error = ParseTriggerSourceError;
fn try_from(value: &'static str) -> Result<Self, Self::Error> {
Self::from_static(value)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ParseTriggerSourceError {
_private: (),
}
impl ParseTriggerSourceError {
const fn new() -> Self {
Self { _private: () }
}
}
impl fmt::Display for ParseTriggerSourceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("invalid trigger source")
}
}
impl core::error::Error for ParseTriggerSourceError {}
#[cfg(test)]
mod tests {
use super::ParseTriggerSourceError;
use super::TriggerSource;
#[test]
fn display_uses_canonical_builtin_values() {
assert_eq!(TriggerSource::Reveal.to_string(), "reveal");
assert_eq!(TriggerSource::WithValue.to_string(), "with-value");
assert_eq!(TriggerSource::Registry.to_string(), "registry");
assert_eq!(TriggerSource::Serialize.to_string(), "serialize");
}
#[test]
fn display_uses_custom_prefix() {
assert_eq!(TriggerSource::custom("serde").to_string(), "custom:serde");
assert_eq!(
TriggerSource::Custom("manual-check").to_string(),
"custom:manual-check"
);
}
#[test]
fn custom_constructor_trims_name() {
assert_eq!(
TriggerSource::custom(" serde "),
TriggerSource::Custom("serde")
);
}
#[test]
fn as_builtin_str_returns_canonical_values_for_builtin_sources() {
assert_eq!(TriggerSource::Reveal.as_builtin_str(), Some("reveal"));
assert_eq!(
TriggerSource::WithValue.as_builtin_str(),
Some("with-value")
);
assert_eq!(TriggerSource::Registry.as_builtin_str(), Some("registry"));
assert_eq!(TriggerSource::Serialize.as_builtin_str(), Some("serialize"));
}
#[test]
fn as_builtin_str_returns_none_for_custom() {
assert_eq!(TriggerSource::custom("serde").as_builtin_str(), None);
}
#[test]
fn as_custom_str_returns_name_only_for_custom() {
assert_eq!(
TriggerSource::custom("serde").as_custom_str(),
Some("serde")
);
assert_eq!(TriggerSource::Reveal.as_custom_str(), None);
}
#[test]
fn is_builtin_and_is_custom_are_complementary() {
for source in TriggerSource::BUILT_IN {
assert!(source.is_builtin());
assert!(!source.is_custom());
}
let custom = TriggerSource::custom("serde");
assert!(custom.is_custom());
assert!(!custom.is_builtin());
}
#[test]
fn built_in_contains_only_builtin_sources() {
assert_eq!(TriggerSource::BUILT_IN.len(), 4);
assert!(
TriggerSource::BUILT_IN
.iter()
.all(TriggerSource::is_builtin)
);
}
#[test]
fn from_static_accepts_canonical_builtin_values() {
for source in TriggerSource::BUILT_IN {
let canonical = source
.as_builtin_str()
.expect("built-in source must have canonical string");
assert_eq!(TriggerSource::from_static(canonical), Ok(*source));
}
}
#[test]
fn from_static_accepts_common_aliases() {
assert_eq!(
TriggerSource::from_static("revealed"),
Ok(TriggerSource::Reveal)
);
assert_eq!(
TriggerSource::from_static("with_value"),
Ok(TriggerSource::WithValue)
);
assert_eq!(
TriggerSource::from_static("withvalue"),
Ok(TriggerSource::WithValue)
);
assert_eq!(
TriggerSource::from_static("value"),
Ok(TriggerSource::WithValue)
);
assert_eq!(
TriggerSource::from_static("serialization"),
Ok(TriggerSource::Serialize)
);
assert_eq!(
TriggerSource::from_static("serde"),
Ok(TriggerSource::Serialize)
);
}
#[test]
fn from_static_trims_whitespace_and_ignores_ascii_case() {
assert_eq!(
TriggerSource::from_static(" REVEAL "),
Ok(TriggerSource::Reveal)
);
assert_eq!(
TriggerSource::from_static("With-Value"),
Ok(TriggerSource::WithValue)
);
assert_eq!(
TriggerSource::from_static("Registry"),
Ok(TriggerSource::Registry)
);
assert_eq!(
TriggerSource::from_static("SERIALIZE"),
Ok(TriggerSource::Serialize)
);
}
#[test]
fn from_static_accepts_custom_sources_with_prefix() {
assert_eq!(
TriggerSource::from_static("custom:serde"),
Ok(TriggerSource::Custom("serde"))
);
assert_eq!(
TriggerSource::from_static("CUSTOM:serde"),
Ok(TriggerSource::Custom("serde"))
);
assert_eq!(
TriggerSource::from_static(" custom: manual-check "),
Ok(TriggerSource::Custom("manual-check"))
);
}
#[test]
fn try_from_static_str_matches_from_static() {
assert_eq!(
TriggerSource::try_from("serialize"),
Ok(TriggerSource::Serialize)
);
assert_eq!(
TriggerSource::try_from("custom:serde"),
Ok(TriggerSource::Custom("serde"))
);
}
#[test]
fn from_static_rejects_empty_custom_name() {
assert_eq!(
TriggerSource::from_static("custom:"),
Err(ParseTriggerSourceError::new())
);
assert_eq!(
TriggerSource::from_static("custom: "),
Err(ParseTriggerSourceError::new())
);
}
#[test]
fn from_static_rejects_unknown_values_without_custom_prefix() {
assert_eq!(
TriggerSource::from_static(""),
Err(ParseTriggerSourceError::new())
);
assert_eq!(
TriggerSource::from_static(" "),
Err(ParseTriggerSourceError::new())
);
assert_eq!(
TriggerSource::from_static("drop"),
Err(ParseTriggerSourceError::new())
);
assert_eq!(
TriggerSource::from_static("reveal "),
Ok(TriggerSource::Reveal)
);
assert_eq!(
TriggerSource::from_static("reveal-with-suffix"),
Err(ParseTriggerSourceError::new())
);
}
#[test]
fn error_display_is_stable() {
assert_eq!(
ParseTriggerSourceError::new().to_string(),
"invalid trigger source"
);
}
}