markdoc 0.1.1

Rust implementation of the Markdoc document language (CommonMark plus a {% tag %} extension)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! [`JsString`]: a JavaScript string, UTF-16 code units exactly as a JS string holds them.
//!
//! A JS string is any sequence of 16-bit code units. Almost every one is well formed (each
//! surrogate in a pair) and is then text a Rust `String` holds. A JS string operation on valid
//! text can still make a *lone* surrogate: indexing a string (`$s[0]` of "😀" is `"\ud83d"`),
//! spreading one into an object (one code unit per key). A Rust `String` cannot hold that,
//! so a [`JsString`] keeps well-formed text as a `String` (the cheap path: no conversion, and
//! [`JsString::as_str`] borrows it) and any other string as its code units.
//!
//! Comparing, measuring, indexing and concatenating work on code units, as JS does: two
//! different halves are different strings, a half is not U+FFFD, and a lead half followed by
//! a trail half is the character they make ([`JsStringBuilder`]). A lone surrogate becomes
//! U+FFFD only where a string leaves as Rust text ([`JsString::to_string_lossy`], `Display`),
//! which is the bytes node writes for one when it encodes the string as UTF-8.

use std::borrow::Cow;
use std::fmt;

/// A JavaScript string (see [`crate::next::schema::value`]'s "Strings").
///
/// Built from text with `From<&str>`/`From<String>`, or from code units with
/// [`JsString::from_utf16`]; read as text with [`JsString::as_str`] (`None` when the string
/// holds a lone surrogate) or [`JsString::to_string_lossy`] (a lone surrogate as U+FFFD).
/// `==` compares code units.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct JsString(Repr);

/// Invariant: `Units` holds at least one lone surrogate, so that a string has one
/// representation and the derived `==` and `Hash` compare code units.
#[derive(Clone, PartialEq, Eq, Hash)]
enum Repr {
    /// A well-formed string: its text.
    Text(String),
    /// A string with a lone surrogate: its code units.
    Units(Box<[u16]>),
}

impl JsString {
    /// The empty string.
    pub const fn new() -> JsString {
        JsString(Repr::Text(String::new()))
    }

    /// The string these code units are, whatever they are.
    pub fn from_utf16(units: &[u16]) -> JsString {
        match String::from_utf16(units) {
            Ok(text) => JsString(Repr::Text(text)),
            Err(_) => JsString(Repr::Units(units.into())),
        }
    }

    /// [`JsString::from_utf16`] of an owned buffer.
    pub fn from_utf16_vec(units: Vec<u16>) -> JsString {
        match String::from_utf16(&units) {
            Ok(text) => JsString(Repr::Text(text)),
            Err(_) => JsString(Repr::Units(units.into_boxed_slice())),
        }
    }

    /// `String.fromCharCode(unit)`: the one-code-unit string.
    pub fn from_code_unit(unit: u16) -> JsString {
        JsString::from_utf16(&[unit])
    }

    /// The text, when the string is well formed (holds no lone surrogate).
    pub fn as_str(&self) -> Option<&str> {
        match &self.0 {
            Repr::Text(text) => Some(text),
            Repr::Units(_) => None,
        }
    }

    /// `String.prototype.isWellFormed`: no lone surrogate.
    pub fn is_well_formed(&self) -> bool {
        matches!(self.0, Repr::Text(_))
    }

    /// The string as Rust text: borrowed when well formed, else with each lone surrogate
    /// replaced by U+FFFD (what node writes for one in UTF-8).
    pub fn to_string_lossy(&self) -> Cow<'_, str> {
        match &self.0 {
            Repr::Text(text) => Cow::Borrowed(text),
            Repr::Units(units) => Cow::Owned(String::from_utf16_lossy(units)),
        }
    }

    /// [`JsString::to_string_lossy`], owned.
    pub fn into_string_lossy(self) -> String {
        match self.0 {
            Repr::Text(text) => text,
            Repr::Units(units) => String::from_utf16_lossy(&units),
        }
    }

    /// The text, or the string itself back when it holds a lone surrogate.
    pub fn into_string(self) -> Result<String, JsString> {
        match self.0 {
            Repr::Text(text) => Ok(text),
            units => Err(JsString(units)),
        }
    }

    /// `s === ''`.
    pub fn is_empty(&self) -> bool {
        match &self.0 {
            Repr::Text(text) => text.is_empty(),
            Repr::Units(units) => units.is_empty(),
        }
    }

    /// `s.length`: the number of UTF-16 code units.
    pub fn len_utf16(&self) -> usize {
        match &self.0 {
            Repr::Text(text) => text.chars().map(char::len_utf16).sum(),
            Repr::Units(units) => units.len(),
        }
    }

    /// The code units, in order.
    pub fn code_units(&self) -> CodeUnits<'_> {
        CodeUnits(match &self.0 {
            Repr::Text(text) => CodeUnitsRepr::Text(text.encode_utf16()),
            Repr::Units(units) => CodeUnitsRepr::Units(units.iter()),
        })
    }

    /// The code units as a slice (borrowed when the string holds a lone surrogate).
    pub fn to_utf16(&self) -> Cow<'_, [u16]> {
        match &self.0 {
            Repr::Text(text) => Cow::Owned(text.encode_utf16().collect()),
            Repr::Units(units) => Cow::Borrowed(units),
        }
    }

    /// `this + other`: code units appended, so a lead half at the end of `self` and a
    /// trail half at the start of `other` become one character.
    pub fn concat(&self, other: &JsString) -> JsString {
        let mut out = JsStringBuilder::new();
        out.push_js(self);
        out.push_js(other);
        out.finish()
    }
}

impl Default for JsString {
    fn default() -> Self {
        JsString::new()
    }
}

/// The code units of a [`JsString`] ([`JsString::code_units`]).
#[derive(Clone)]
pub struct CodeUnits<'a>(CodeUnitsRepr<'a>);

#[derive(Clone)]
enum CodeUnitsRepr<'a> {
    Text(std::str::EncodeUtf16<'a>),
    Units(std::slice::Iter<'a, u16>),
}

impl Iterator for CodeUnits<'_> {
    type Item = u16;

    fn next(&mut self) -> Option<u16> {
        match &mut self.0 {
            CodeUnitsRepr::Text(units) => units.next(),
            CodeUnitsRepr::Units(units) => units.next().copied(),
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        match &self.0 {
            CodeUnitsRepr::Text(units) => units.size_hint(),
            CodeUnitsRepr::Units(units) => units.size_hint(),
        }
    }
}

impl From<&str> for JsString {
    fn from(text: &str) -> Self {
        JsString(Repr::Text(text.to_owned()))
    }
}

impl From<String> for JsString {
    fn from(text: String) -> Self {
        JsString(Repr::Text(text))
    }
}

impl From<&String> for JsString {
    fn from(text: &String) -> Self {
        JsString(Repr::Text(text.clone()))
    }
}

impl From<Cow<'_, str>> for JsString {
    fn from(text: Cow<'_, str>) -> Self {
        JsString(Repr::Text(text.into_owned()))
    }
}

impl From<char> for JsString {
    fn from(c: char) -> Self {
        JsString(Repr::Text(c.to_string()))
    }
}

impl PartialEq<str> for JsString {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == Some(other)
    }
}

impl PartialEq<&str> for JsString {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == Some(*other)
    }
}

impl PartialEq<String> for JsString {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == Some(other.as_str())
    }
}

impl PartialEq<JsString> for str {
    fn eq(&self, other: &JsString) -> bool {
        other == self
    }
}

impl PartialEq<JsString> for &str {
    fn eq(&self, other: &JsString) -> bool {
        other == self
    }
}

impl PartialEq<JsString> for String {
    fn eq(&self, other: &JsString) -> bool {
        other == self
    }
}

/// The text, a lone surrogate as U+FFFD.
impl fmt::Display for JsString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.to_string_lossy())
    }
}

/// As `str`'s `Debug`, a lone surrogate written `\u{d83d}`.
impl fmt::Debug for JsString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.0 {
            Repr::Text(text) => fmt::Debug::fmt(text.as_str(), f),
            Repr::Units(units) => {
                f.write_str("\"")?;
                for c in char::decode_utf16(units.iter().copied()) {
                    match c {
                        Ok(c) => {
                            for e in c.escape_debug() {
                                fmt::Write::write_char(f, e)?;
                            }
                        }
                        Err(e) => write!(f, "\\u{{{:x}}}", e.unpaired_surrogate())?,
                    }
                }
                f.write_str("\"")
            }
        }
    }
}

/// A JS string built by concatenation (`a + b`), code unit by code unit: text while every
/// piece is well formed, code units once one is not, so that two halves that meet become the
/// character they make. [`JsStringBuilder::finish`] gives the string;
/// [`JsStringBuilder::finish_lossy`] the text a renderer writes (a lone surrogate that is
/// still lone as U+FFFD).
#[derive(Default)]
pub struct JsStringBuilder(Builder);

#[derive(Default)]
enum Builder {
    #[default]
    Empty,
    Text(String),
    Units(Vec<u16>),
}

impl JsStringBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    /// The code units so far (switching to code units, which every later piece then joins).
    fn units(&mut self) -> &mut Vec<u16> {
        if !matches!(self.0, Builder::Units(_)) {
            let units = match std::mem::take(&mut self.0) {
                Builder::Text(text) => text.encode_utf16().collect(),
                _ => Vec::new(),
            };
            self.0 = Builder::Units(units);
        }
        match &mut self.0 {
            Builder::Units(units) => units,
            _ => unreachable!("just set"),
        }
    }

    /// Appends text.
    pub fn push_str(&mut self, text: &str) {
        match &mut self.0 {
            Builder::Empty => self.0 = Builder::Text(text.to_owned()),
            Builder::Text(out) => out.push_str(text),
            Builder::Units(units) => units.extend(text.encode_utf16()),
        }
    }

    /// Appends a character.
    pub fn push(&mut self, c: char) {
        match &mut self.0 {
            Builder::Empty => self.0 = Builder::Text(c.to_string()),
            Builder::Text(out) => out.push(c),
            Builder::Units(units) => units.extend(c.encode_utf16(&mut [0; 2]).iter()),
        }
    }

    /// Appends a JS string.
    pub fn push_js(&mut self, s: &JsString) {
        match &s.0 {
            Repr::Text(text) => self.push_str(text),
            Repr::Units(more) => self.units().extend_from_slice(more),
        }
    }

    /// Appends code units.
    pub fn push_units(&mut self, more: &[u16]) {
        match String::from_utf16(more) {
            // Well formed on its own, but a trail half at its start could still join a lead
            // half before it: only text after text stays text.
            Ok(text) if !matches!(self.0, Builder::Units(_)) => self.push_str(&text),
            _ => self.units().extend_from_slice(more),
        }
    }

    /// Nothing appended yet (or only empty strings).
    pub fn is_empty(&self) -> bool {
        match &self.0 {
            Builder::Empty => true,
            Builder::Text(text) => text.is_empty(),
            Builder::Units(units) => units.is_empty(),
        }
    }

    /// The string.
    pub fn finish(self) -> JsString {
        match self.0 {
            Builder::Empty => JsString::new(),
            Builder::Text(text) => JsString(Repr::Text(text)),
            Builder::Units(units) => JsString::from_utf16_vec(units),
        }
    }

    /// The string as Rust text (a lone surrogate as U+FFFD).
    pub fn finish_lossy(self) -> String {
        match self.0 {
            Builder::Empty => String::new(),
            Builder::Text(text) => text,
            Builder::Units(units) => String::from_utf16_lossy(&units),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const LEAD: u16 = 0xD83D;
    const TRAIL: u16 = 0xDE00;

    #[test]
    fn well_formed_text_is_text() {
        let s = JsString::from("a😀");
        assert_eq!(s.as_str(), Some("a😀"));
        assert!(s.is_well_formed());
        assert_eq!(s.len_utf16(), 3);
        assert_eq!(s.code_units().collect::<Vec<_>>(), [0x61, LEAD, TRAIL]);
        assert_eq!(JsString::from_utf16(&[0x61, LEAD, TRAIL]), s);
        assert_eq!(s, "a😀");
        assert!(JsString::new().is_empty() && JsString::default().is_empty());
    }

    #[test]
    fn a_lone_surrogate_is_kept() {
        let lead = JsString::from_code_unit(LEAD);
        let trail = JsString::from_code_unit(TRAIL);
        // node: '😀'[0] !== '😀'[1], '😀'[0] !== '\ufffd', '😀'[0].length === 1
        assert_ne!(lead, trail);
        assert_ne!(lead, JsString::from("\u{FFFD}"));
        assert_ne!(lead, "\u{FFFD}");
        assert_eq!(lead.len_utf16(), 1);
        assert_eq!(lead.as_str(), None);
        assert!(!lead.is_well_formed() && !lead.is_empty());
        assert_eq!(lead.to_string_lossy(), "\u{FFFD}");
        assert_eq!(lead.to_string(), "\u{FFFD}");
        assert_eq!(format!("{lead:?}"), "\"\\u{d83d}\"");
        assert_eq!(lead.clone().into_string(), Err(lead.clone()));
        assert_eq!(lead.to_utf16().as_ref(), [LEAD]);
    }

    #[test]
    fn halves_that_meet_are_the_character() {
        // node: '😀'[0] + '😀'[1] === '😀'
        let lead = JsString::from_code_unit(LEAD);
        let trail = JsString::from_code_unit(TRAIL);
        assert_eq!(lead.concat(&trail), JsString::from("😀"));
        assert_eq!(lead.concat(&trail).as_str(), Some("😀"));
        // ... and not the other way round: '😀'[1] + '😀'[0] has two lone surrogates.
        assert_eq!(trail.concat(&lead).to_string_lossy(), "\u{FFFD}\u{FFFD}");
        let mut b = JsStringBuilder::new();
        b.push_str("x");
        b.push_js(&lead);
        b.push_units(&[TRAIL, 0x79]);
        assert_eq!(b.finish(), JsString::from("x😀y"));
        let mut b = JsStringBuilder::new();
        b.push_js(&lead);
        b.push('z');
        assert_eq!(b.finish_lossy(), "\u{FFFD}z");
    }
}