ctb_formats_dcstring/
kv_map.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! Shared framing for canonical pair arrays and legacy alternating maps.
21
22#[allow(
23    unused_imports,
24    clippy::wildcard_imports,
25    reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29use crate::{DcChar, DcMstring};
30use ctb_formats_dcdata::dc::{
31    DC_BEGIN_KV_MAP, DC_BEGIN_KV_PAIR, DC_BEGIN_LEGACY_KV_MAP, DC_END_KV_MAP,
32    DC_END_KV_PAIR, DC_END_LEGACY_KV_MAP, DC_LIST_ELEMENT_SEPARATOR,
33    DC_TYPE_ANY,
34};
35
36/// Wire framing of a key-value array, determined by its opening Dc.
37#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum DcKvMapEncoding {
39    /// Deprecated Dc 313..314 alternating key/value operands.
40    Legacy,
41    /// Dc 559..560 containing adjacent Dc 514..515 pairs.
42    Pairs,
43}
44
45impl DcKvMapEncoding {
46    pub fn encode_header_to(
47        self,
48        target: &mut crate::DcWriter<'_>,
49    ) -> Result<()> {
50        target.push_char(match self {
51            Self::Legacy => DC_BEGIN_LEGACY_KV_MAP,
52            Self::Pairs => DC_BEGIN_KV_MAP,
53        })?;
54        target.push_char(DC_TYPE_ANY)?;
55        target.push_char(DC_TYPE_ANY)
56    }
57
58    pub fn encode_entry_begin_to(
59        self,
60        target: &mut crate::DcWriter<'_>,
61        first: bool,
62    ) -> Result<()> {
63        match self {
64            Self::Pairs => target.push_char(DC_BEGIN_KV_PAIR)?,
65            Self::Legacy if !first => {
66                target.push_char(DC_LIST_ELEMENT_SEPARATOR)?;
67            }
68            Self::Legacy => {}
69        }
70        Ok(())
71    }
72
73    pub fn encode_entry_end_to(
74        self,
75        target: &mut crate::DcWriter<'_>,
76    ) -> Result<()> {
77        if self == Self::Pairs {
78            target.push_char(DC_END_KV_PAIR)?;
79        }
80        Ok(())
81    }
82
83    pub fn encode_end_to(self, target: &mut crate::DcWriter<'_>) -> Result<()> {
84        target.push_char(self.closer())
85    }
86
87    /// Writes the array header with Object key and value types.
88    pub fn encode_header(self, target: &mut DcMstring) {
89        target.push_char(match self {
90            Self::Legacy => DC_BEGIN_LEGACY_KV_MAP,
91            Self::Pairs => DC_BEGIN_KV_MAP,
92        });
93        target.push_char(DC_TYPE_ANY);
94        target.push_char(DC_TYPE_ANY);
95    }
96
97    /// Writes the boundary before an entry.
98    pub fn encode_entry_begin(self, target: &mut DcMstring, first: bool) {
99        match self {
100            Self::Pairs => target.push_char(DC_BEGIN_KV_PAIR),
101            Self::Legacy if !first => {
102                target.push_char(DC_LIST_ELEMENT_SEPARATOR);
103            }
104            Self::Legacy => {}
105        }
106    }
107
108    /// Writes the boundary after an entry.
109    pub fn encode_entry_end(self, target: &mut DcMstring) {
110        if self == Self::Pairs {
111            target.push_char(DC_END_KV_PAIR);
112        }
113    }
114
115    /// Writes the closing array delimiter.
116    pub fn encode_end(self, target: &mut DcMstring) {
117        target.push_char(self.closer());
118    }
119
120    const fn closer(self) -> DcChar {
121        match self {
122            Self::Legacy => DC_END_LEGACY_KV_MAP,
123            Self::Pairs => DC_END_KV_MAP,
124        }
125    }
126}