Skip to main content

ical/tree/param/
altrep.rs

1//! # ALTREP parameter lens
2//!
3//! The `ALTREP` parameter lens: the alternate text representation URI (RFC 5545
4//! 3.2.1).
5
6use alloc::{borrow::Cow, string::ToString, vec};
7
8use crate::{
9    param::IcalParamKind,
10    tree::{
11        codec::unescape::unescape,
12        leaf::IcalLeaf,
13        param::{IcalParamLens, IcalParamNode},
14    },
15};
16
17/// The `ALTREP` parameter lens.
18#[allow(non_camel_case_types)]
19pub struct ALTREP;
20
21impl IcalParamLens for ALTREP {
22    const KIND: IcalParamKind = IcalParamKind::AltRep;
23
24    type Target<'v> = Cow<'v, str>;
25
26    fn decode<'v>(param: &'v IcalParamNode<'_>) -> Cow<'v, str> {
27        param
28            .values
29            .first()
30            .map(|value| unescape(value.get()))
31            .unwrap_or_default()
32    }
33
34    fn encode(decoded: &Cow<'_, str>) -> IcalParamNode<'static> {
35        IcalParamNode {
36            name: IcalLeaf::from(Self::KIND.to_string()),
37            values: vec![IcalLeaf::from(decoded.to_string())],
38        }
39    }
40}