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; [`IcalBoolean::is_true`] reads it
8//! as a `bool`.
9
10use alloc::{borrow::Cow, string::String};
11
12/// A decoded boolean value (`TRUE` / `FALSE`), kept as its raw text.
13#[derive(Clone, Debug, Default, PartialEq, Eq)]
14pub struct IcalBoolean<'a>(pub Cow<'a, str>);
15
16impl IcalBoolean<'_> {
17    /// Whether the value is the (case-insensitive) token `TRUE`.
18    pub fn is_true(&self) -> bool {
19        self.0.eq_ignore_ascii_case("TRUE")
20    }
21}
22
23impl<'a> From<&'a str> for IcalBoolean<'a> {
24    fn from(value: &'a str) -> Self {
25        Self(Cow::Borrowed(value))
26    }
27}
28
29impl From<String> for IcalBoolean<'_> {
30    fn from(value: String) -> Self {
31        Self(Cow::Owned(value))
32    }
33}
34
35impl<'a> From<Cow<'a, str>> for IcalBoolean<'a> {
36    fn from(value: Cow<'a, str>) -> Self {
37        Self(value)
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use crate::value::boolean::IcalBoolean;
44
45    #[test]
46    fn is_true_reads_both_cases() {
47        assert!(IcalBoolean::from("TRUE").is_true());
48        assert!(IcalBoolean::from("true").is_true());
49        assert!(!IcalBoolean::from("FALSE").is_true());
50        assert!(!IcalBoolean::from("false").is_true());
51    }
52}