Skip to main content

ical/tree/value/
recur.rs

1//! # RECUR value codec (RFC 5545 3.3.10)
2//!
3//! [`Codec`] for a recurrence rule. A RECUR value uses `;` to separate its
4//! rule parts (`FREQ=DAILY;COUNT=10`), which the generic node reads as separate
5//! components; they are rejoined with `;` on decode and written verbatim
6//! (unescaped, since RECUR is not TEXT) on encode.
7
8use crate::{
9    tree::{
10        codec::{Codec, mode::Escaper},
11        value::IcalValueNode,
12    },
13    value::recur::IcalRecur,
14};
15
16use alloc::{borrow::Cow, string::String};
17
18impl<'v> Codec<'v> for IcalRecur<'v> {
19    fn decode(node: &'v IcalValueNode<'_>) -> Self {
20        let mut joined = String::new();
21
22        for i in 0..node.component_count() {
23            if i > 0 {
24                joined.push(';');
25            }
26            joined.push_str(&node.decode_joined_at(i));
27        }
28
29        IcalRecur(Cow::Owned(joined))
30    }
31
32    fn encode(&self, escaper: Escaper) -> IcalValueNode<'static> {
33        let mut node = IcalValueNode::parse(self.0.as_bytes()).into_static();
34        node.escaper = escaper;
35        node
36    }
37}