Skip to main content

ical/value/
integer.rs

1//! # Integer value
2//!
3//! The decoded integer value kind.
4//!
5//! Backs the integer-valued properties and parameters (RFC 5545 3.3.8): a
6//! signed decimal integer such as `1234` or `-9`. The value is kept as its raw
7//! text so the original lexical form round-trips; use [`IcalInteger::get`] to
8//! parse it into an [`i64`]. Pure data, no escaping; the owning property's wire
9//! name lives on [`crate::prop::IcalProp::name`].
10
11use alloc::{borrow::Cow, string::String};
12
13/// A decoded integer value (signed), kept as its raw text.
14#[derive(Clone, Debug, Default, PartialEq, Eq)]
15pub struct IcalInteger<'a>(pub Cow<'a, str>);
16
17impl IcalInteger<'_> {
18    /// Parse the raw text into an [`i64`]; `None` if it is not a valid integer.
19    pub fn get(&self) -> Option<i64> {
20        self.0.parse().ok()
21    }
22}
23
24impl<'a> From<&'a str> for IcalInteger<'a> {
25    fn from(value: &'a str) -> Self {
26        Self(Cow::Borrowed(value))
27    }
28}
29
30impl From<String> for IcalInteger<'_> {
31    fn from(value: String) -> Self {
32        Self(Cow::Owned(value))
33    }
34}
35
36impl<'a> From<Cow<'a, str>> for IcalInteger<'a> {
37    fn from(value: Cow<'a, str>) -> Self {
38        Self(value)
39    }
40}
41
42#[cfg(test)]
43mod tests {
44    use crate::value::integer::IcalInteger;
45
46    #[test]
47    fn get_parses_signed_integer() {
48        assert_eq!(IcalInteger::from("-9").get(), Some(-9));
49        assert_eq!(IcalInteger::from("abc").get(), None);
50    }
51}