ical/tree/codec/mode.rs
1//! # Escaping mode
2//!
3//! The one place the codec consults the calendar version: value escaping
4//! differs between vCalendar 1.0 (versit) and iCalendar 2.0 (RFC 5545 3.3.11),
5//! so a value node carries an [`Escaper`] telling the sibling
6//! [`escape`](crate::tree::codec::escape) and
7//! [`unescape`](crate::tree::codec::unescape) codecs which rules to apply.
8
9use crate::version::IcalVersion;
10
11/// The value-escaping rules to apply, selected by the calendar version.
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
13pub enum Escaper {
14 /// vCalendar 1.0 (versit): only `;` is escaped (`\;`); a backslash before
15 /// anything else is literal.
16 V1_0,
17 /// iCalendar 2.0 (RFC 5545 3.3.11): `\\`, `\,`, `\;` and `\n`.
18 #[default]
19 Modern,
20}
21
22impl Escaper {
23 /// The escaping rules a calendar of `version` uses.
24 pub fn for_version(version: IcalVersion) -> Self {
25 match version {
26 IcalVersion::V1_0 => Self::V1_0,
27 IcalVersion::V2_0 => Self::Modern,
28 }
29 }
30
31 /// The escaping rules for a raw `VERSION` wire string (e.g. `"1.0"`).
32 pub fn for_version_str(version: &str) -> Self {
33 match version.parse() {
34 Ok(IcalVersion::V1_0) => Self::V1_0,
35 _ => Self::Modern,
36 }
37 }
38}