rd-rds 0.5.0

Read-only reader for the subset of R's RDS serialization format used by installed-package help databases
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use std::{borrow::Cow, sync::Arc};

use crate::Error;

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RObject {
    value: RValue,
    attributes: Attributes,
}

#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub enum RValue {
    Null,
    Logical(Vec<Option<bool>>),
    Integer(Vec<Option<i32>>),
    Real(Vec<Option<f64>>),
    Character(Vec<RStr>),
    List(Vec<RObject>),
    Symbol(Symbol),
    Persisted(Persisted),
    Environment(EnvHandle),
}

/// An opaque handle to an environment.
///
/// `rd-rds` never models environment frames or enclosures: it only consumes
/// the wire bytes correctly and keeps the reference table aligned. All
/// non-singleton environments collapse to [`EnvHandle::Other`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum EnvHandle {
    /// `R_GlobalEnv`.
    Global,
    /// `R_BaseEnv` / `R_BaseNamespace`.
    Base,
    /// `R_EmptyEnv`.
    Empty,
    /// Any other environment/package/namespace. Its wire fields were
    /// decoded (to keep the stream and reference table in sync) and then
    /// discarded.
    Other,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Attribute {
    name: Symbol,
    value: RObject,
}

impl Attribute {
    pub fn new(name: Symbol, value: RObject) -> Self {
        Self { name, value }
    }

    pub fn name(&self) -> &Symbol {
        &self.name
    }

    pub fn value(&self) -> &RObject {
        &self.value
    }
}

#[derive(Debug, Clone, PartialEq, Default)]
pub struct Attributes(Vec<Attribute>);

impl Attributes {
    /// Constructs an attribute collection from decoded attributes.
    pub fn new(values: Vec<Attribute>) -> Self {
        Self(values)
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn iter(&self) -> impl Iterator<Item = &Attribute> {
        self.0.iter()
    }

    pub fn get(&self, name: &str) -> Option<&RObject> {
        self.0
            .iter()
            .find(|attribute| attribute.name().as_str() == name)
            .map(|attribute| attribute.value())
    }
}

impl RObject {
    pub fn from_parts(value: RValue, attributes: Attributes) -> Self {
        Self { value, attributes }
    }

    pub fn value(&self) -> &RValue {
        &self.value
    }

    pub fn attributes(&self) -> &Attributes {
        &self.attributes
    }

    pub fn into_parts(self) -> (RValue, Attributes) {
        (self.value, self.attributes)
    }

    pub fn class(&self) -> Option<&[RStr]> {
        match self.attributes.get("class")?.value() {
            RValue::Character(values) => Some(values),
            _ => None,
        }
    }

    pub fn names(&self) -> Option<&[RStr]> {
        match self.attributes.get("names")?.value() {
            RValue::Character(values) => Some(values),
            _ => None,
        }
    }

    pub fn get_named(&self, name: &str) -> Option<&RObject> {
        let RValue::List(items) = self.value() else {
            return None;
        };
        let names = self.names()?;
        names
            .iter()
            .position(|value| value.as_str().and_then(Result::ok).as_deref() == Some(name))
            .and_then(|index| items.get(index))
    }
}

/// A symbol whose print name is decoded eagerly during parsing, so it always
/// holds valid text.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Symbol(Arc<str>);

impl Symbol {
    pub fn new(value: impl Into<Arc<str>>) -> Self {
        Self(value.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl From<&str> for Symbol {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Persisted(Arc<[RStr]>);

impl Persisted {
    pub(crate) fn new(values: Vec<RStr>) -> Self {
        Self(values.into())
    }

    #[cfg(test)]
    pub(crate) fn ptr_eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }

    pub fn as_slice(&self) -> &[RStr] {
        &self.0
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RStr {
    Na,
    Value(RStrValue),
}

/// The source of native-encoding context used to interpret an R string.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum NativeEncodingSource {
    /// The stream header declared this native encoding name (format 3 only).
    Header(Arc<str>),
    /// The header carried no native encoding and the caller opted into
    /// [`crate::NativeEncodingPolicy::AssumeUtf8`].
    AssumedUtf8,
    /// The header carried no native encoding and the caller made no
    /// assumption.
    Unknown,
}

impl NativeEncodingSource {
    pub(crate) fn decodes_as_utf8(&self) -> bool {
        match self {
            Self::Header(name) => is_utf8_native_encoding(Some(name)),
            Self::AssumedUtf8 => true,
            Self::Unknown => false,
        }
    }
}

/// The opaque payload of an [`RStr::Value`] string.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RStrValue {
    bytes: Arc<[u8]>,
    encoding: REncoding,
    native_encoding_source: NativeEncodingSource,
}

impl RStr {
    /// Constructs an R string value from its serialized bytes and encoding
    /// context.
    pub fn new(
        bytes: &[u8],
        encoding: REncoding,
        native_encoding_source: NativeEncodingSource,
    ) -> Self {
        Self::Value(RStrValue {
            bytes: Arc::from(bytes),
            encoding,
            native_encoding_source,
        })
    }

    pub fn as_str(&self) -> Option<Result<Cow<'_, str>, Error>> {
        match self {
            Self::Na => None,
            Self::Value(value) => Some(value.as_str()),
        }
    }

    /// Reports the CHARSXP encoding flag recorded in the serialized data.
    /// R's `Encoding()` normalizes ASCII strings to "unknown" after reading,
    /// so the two can differ while the decoded string contents agree.
    pub fn encoding(&self) -> Option<REncoding> {
        match self {
            Self::Na => None,
            Self::Value(value) => Some(value.encoding()),
        }
    }
}

impl RStrValue {
    fn as_str(&self) -> Result<Cow<'_, str>, Error> {
        match self.encoding {
            REncoding::Native => {
                if self.bytes.is_ascii() || self.native_encoding_source.decodes_as_utf8() {
                    std::str::from_utf8(&self.bytes)
                        .map(Cow::Borrowed)
                        .map_err(|_| Error::InvalidStringEncoding)
                } else {
                    Err(Error::InvalidStringEncoding)
                }
            }
            REncoding::Utf8 => std::str::from_utf8(&self.bytes)
                .map(Cow::Borrowed)
                .map_err(|_| Error::InvalidStringEncoding),
            REncoding::Latin1 => Ok(Cow::Owned(
                self.bytes.iter().map(|&byte| byte as char).collect(),
            )),
            REncoding::Bytes => Err(Error::InvalidStringEncoding),
        }
    }

    /// Returns the serialized string bytes without converting them.
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Returns the CHARSXP encoding flag recorded in the serialized data.
    pub fn encoding(&self) -> REncoding {
        self.encoding
    }

    /// Returns the native-encoding provenance used for this value.
    pub fn native_encoding_source(&self) -> &NativeEncodingSource {
        &self.native_encoding_source
    }

    /// Returns the native encoding name declared by the stream header, if any.
    ///
    /// This reports header evidence only; it is not the effective decoding
    /// context and therefore returns `None` for [`NativeEncodingSource::AssumedUtf8`]
    /// and [`NativeEncodingSource::Unknown`].
    pub fn header_native_encoding(&self) -> Option<&str> {
        match &self.native_encoding_source {
            NativeEncodingSource::Header(name) => Some(name),
            NativeEncodingSource::AssumedUtf8 | NativeEncodingSource::Unknown => None,
        }
    }
}

fn is_utf8_native_encoding(encoding: Option<&str>) -> bool {
    encoding
        .map(|encoding| {
            encoding.eq_ignore_ascii_case("UTF-8") || encoding.eq_ignore_ascii_case("UTF8")
        })
        .unwrap_or(false)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum REncoding {
    Native,
    Utf8,
    Latin1,
    Bytes,
}

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

    #[test]
    fn native_ascii_strings_decode_without_header_encoding() {
        let value = RStr::new(b"name", REncoding::Native, NativeEncodingSource::Unknown);
        assert_eq!(value.as_str().unwrap().unwrap().as_ref(), "name");
    }

    #[test]
    fn native_utf8_strings_decode_when_header_encoding_is_utf8() {
        let value = RStr::new(
            "cafe\u{301}".as_bytes(),
            REncoding::Native,
            NativeEncodingSource::Header(Arc::from("UTF-8")),
        );
        assert_eq!(value.as_str().unwrap().unwrap().as_ref(), "cafe\u{301}");
    }

    #[test]
    fn native_non_ascii_strings_reject_unknown_native_encoding() {
        let value = RStr::new(
            "é".as_bytes(),
            REncoding::Native,
            NativeEncodingSource::Unknown,
        );
        assert_eq!(
            value.as_str().unwrap().unwrap_err(),
            Error::InvalidStringEncoding
        );
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Limits {
    max_depth: u32,
    max_vector_len: usize,
    max_total_elements: usize,
    max_references: usize,
}

impl Default for Limits {
    fn default() -> Self {
        Self {
            max_depth: 5_000,
            max_vector_len: 8_000_000,
            max_total_elements: 16_000_000,
            max_references: 16_000_000,
        }
    }
}

impl Limits {
    pub fn max_depth(mut self, value: u32) -> Self {
        self.max_depth = value;
        self
    }

    pub fn max_vector_len(mut self, value: usize) -> Self {
        self.max_vector_len = value;
        self
    }

    pub fn max_total_elements(mut self, value: usize) -> Self {
        self.max_total_elements = value;
        self
    }

    /// Sets the maximum number of entries in the serialized reference table.
    #[must_use]
    pub fn max_references(mut self, value: usize) -> Self {
        self.max_references = value;
        self
    }

    pub(crate) fn max_depth_value(self) -> u32 {
        self.max_depth
    }
    pub(crate) fn max_vector_len_value(self) -> usize {
        self.max_vector_len
    }
    pub(crate) fn max_total_elements_value(self) -> usize {
        self.max_total_elements
    }
    pub(crate) fn max_references_value(self) -> usize {
        self.max_references
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SexpKind {
    Nil,
    Sym,
    PairList,
    Env,
    Char,
    Logical,
    Integer,
    Real,
    String,
    List,
    ExtPtr,
    WeakRef,
    Persist,
    Package,
    Namespace,
    Ref,
    Closure,
    BuiltIn,
    Special,
    Promise,
    Lang,
    DotDotDot,
    ByteCode,
    S4,
    Complex,
    Raw,
    Other(u8),
}

impl SexpKind {
    pub fn from_type_code(type_code: u8) -> Self {
        match type_code {
            0 | 254 => Self::Nil,
            1 => Self::Sym,
            2 => Self::PairList,
            3 => Self::Closure,
            4 => Self::Env,
            5 => Self::Promise,
            6 => Self::Lang,
            7 => Self::Special,
            8 => Self::BuiltIn,
            9 => Self::Char,
            10 => Self::Logical,
            13 => Self::Integer,
            14 => Self::Real,
            15 => Self::Complex,
            16 => Self::String,
            17 => Self::DotDotDot,
            19 => Self::List,
            21 => Self::ByteCode,
            22 => Self::ExtPtr,
            23 => Self::WeakRef,
            24 => Self::Raw,
            25 => Self::S4,
            247 => Self::Persist,
            248 => Self::Package,
            249 | 250 => Self::Namespace,
            255 => Self::Ref,
            other => Self::Other(other),
        }
    }
}