vcard-rs 0.2.1

vCard parser, validator, editor and builder library for Rust
Documentation
//! # VALUE parameter lens
//!
//! The `VALUE` parameter lens: a single value naming the value type to read the
//! property value as (RFC 6350 5.2).

use alloc::{borrow::Cow, string::ToString, vec};

use crate::{
    param::VcardParamKind,
    tree::{
        codec::unescape::unescape,
        leaf::VcardLeaf,
        param::{lens::VcardParamLens, node::VcardParamNode},
    },
};

/// The `VALUE` parameter lens.
pub struct VALUE;

impl VcardParamLens for VALUE {
    const KIND: VcardParamKind = VcardParamKind::Value;

    type Target<'v> = Cow<'v, str>;

    fn decode<'v>(param: &'v VcardParamNode<'_>) -> Cow<'v, str> {
        param
            .values
            .first()
            .map(|value| unescape(value.get()))
            .unwrap_or_default()
    }

    fn encode(decoded: &Cow<'_, str>) -> VcardParamNode<'static> {
        VcardParamNode {
            name: VcardLeaf::from(Self::KIND.to_string()),
            values: vec![VcardLeaf::from(decoded.to_string())],
        }
    }
}