ctb_formats_dcstring/
kv_map.rs1#[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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
38pub enum DcKvMapEncoding {
39 Legacy,
41 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 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 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 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 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}