Skip to main content

ical/value/
boolean.rs

1//! # Boolean value
2//!
3//! The decoded boolean value kind.
4//!
5//! Backs the boolean-valued properties and parameters (RFC 5545 3.3.2): the
6//! case-insensitive tokens `TRUE` and `FALSE`. The value is kept as its raw
7//! text so the original casing round-trips; use [`IcalBoolean::is_true`] to
8//! read it as a `bool`. Pure data, no escaping; the owning property's wire name
9//! lives on [`crate::prop::IcalProp::name`].
10
11use alloc::{borrow::Cow, string::String};
12
13/// A decoded boolean value (`TRUE` / `FALSE`), kept as its raw text.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15pub struct IcalBoolean<'a>(pub Cow<'a, str>);
16
17impl IcalBoolean<'_> {
18    /// Whether the value is the (case-insensitive) token `TRUE`.
19    pub fn is_true(&self) -> bool {
20        self.0.eq_ignore_ascii_case("TRUE")
21    }
22}
23
24impl<'a> From<&'a str> for IcalBoolean<'a> {
25    fn from(value: &'a str) -> Self {
26        Self(Cow::Borrowed(value))
27    }
28}
29
30impl From<String> for IcalBoolean<'_> {
31    fn from(value: String) -> Self {
32        Self(Cow::Owned(value))
33    }
34}
35
36impl<'a> From<Cow<'a, str>> for IcalBoolean<'a> {
37    fn from(value: Cow<'a, str>) -> Self {
38        Self(value)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::value::boolean::IcalBoolean;
45
46    #[test]
47    fn is_true_reads_both_cases() {
48        assert!(IcalBoolean::from("TRUE").is_true());
49        assert!(IcalBoolean::from("true").is_true());
50        assert!(!IcalBoolean::from("FALSE").is_true());
51        assert!(!IcalBoolean::from("false").is_true());
52    }
53}