use alloc::{borrow::Cow, string::String};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct IcalInteger<'a>(pub Cow<'a, str>);
impl IcalInteger<'_> {
pub fn get(&self) -> Option<i64> {
self.0.parse().ok()
}
}
impl<'a> From<&'a str> for IcalInteger<'a> {
fn from(value: &'a str) -> Self {
Self(Cow::Borrowed(value))
}
}
impl From<String> for IcalInteger<'_> {
fn from(value: String) -> Self {
Self(Cow::Owned(value))
}
}
impl<'a> From<Cow<'a, str>> for IcalInteger<'a> {
fn from(value: Cow<'a, str>) -> Self {
Self(value)
}
}
#[cfg(test)]
mod tests {
use crate::value::integer::IcalInteger;
#[test]
fn get_parses_signed_integer() {
assert_eq!(IcalInteger::from("-9").get(), Some(-9));
assert_eq!(IcalInteger::from("abc").get(), None);
}
}