rain-metadata 0.1.7

Tooling and utilities for RainLanguage metadata.
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
use clap::Parser;
use anyhow::anyhow;
use itertools::izip;
use std::path::PathBuf;
use crate::cli::output::SupportedOutputEncoding;
use crate::meta::{
    RainMetaDocumentV1Item, KnownMeta, ContentType, ContentEncoding, ContentLanguage,
    magic::KnownMagic,
};

/// command for building rain meta
#[derive(Parser)]
pub struct Build {
    /// Output path. If not specified, the output is written to stdout.
    #[arg(short, long)]
    output_path: Option<PathBuf>,
    /// Output encoding. If not specified, the output is written in binary format.
    #[arg(short = 'E', long, default_value = "binary")]
    output_encoding: SupportedOutputEncoding,
    /// Global magic number. If not specified, the default magic number is used.
    /// The default magic number is rain-meta-document-v1. Don't change this
    /// unless you know what you are doing.
    #[arg(short = 'M', long, default_value = "rain-meta-document-v1")]
    global_magic: KnownMagic,
    /// Sequence of input paths. The number of input paths must match the number
    /// of magic numbers, content types, content encodings and content languages.
    /// Reading from stdin is not supported but proccess substitution can be used.
    #[arg(short, long, num_args = 1..)]
    input_path: Vec<PathBuf>,
    /// Sequence of magic numbers. The number of magic numbers must match the
    /// number of input paths, content types, content encodings and content languages.
    /// Magic numbers are arbitrary byte sequences used to build self-describing
    /// payloads.
    #[arg(short, long, num_args = 1..)]
    magic: Vec<KnownMagic>,
    /// Sequence of content types. The number of content types must match the
    /// number of input paths, magic numbers, content encodings and content languages.
    /// Content type is as per http headers.
    #[arg(short = 't', long, num_args = 1..)]
    content_type: Vec<ContentType>,
    /// Sequence of content encodings. The number of content encodings must match the
    /// number of input paths, magic numbers, content types and content languages.
    /// Content encoding is as per http headers.
    #[arg(short = 'e', long, num_args = 1..)]
    content_encoding: Vec<ContentEncoding>,
    /// Sequence of content languages. The number of content languages must match the
    /// number of input paths, magic numbers, content types and content encodings.
    /// Content language is as per http headers.
    #[arg(short = 'l', long, num_args = 1..)]
    content_language: Vec<ContentLanguage>,
}

/// Temporary housing for raw data before it is converted into a RainMetaDocumentV1Item.
#[derive(Clone, Debug)]
pub struct BuildItem {
    /// Raw data. Ostensibly this is the content of a file.
    pub data: Vec<u8>,
    /// Magic number taken from build options.
    pub magic: KnownMagic,
    /// Content type taken from build options.
    pub content_type: ContentType,
    /// Content encoding taken from build options.
    pub content_encoding: ContentEncoding,
    /// Content language taken from build options.
    pub content_language: ContentLanguage,
}

/// Moving from a BuildItem to a RainMetaDocumentV1Item requires normalization
/// according to the magic number and encoding from the build options.
impl TryFrom<&BuildItem> for RainMetaDocumentV1Item {
    type Error = anyhow::Error;
    fn try_from(item: &BuildItem) -> anyhow::Result<Self> {
        let normalized = TryInto::<KnownMeta>::try_into(item.magic)?.normalize(&item.data)?;
        let encoded = item.content_encoding.encode(&normalized);
        Ok(RainMetaDocumentV1Item {
            payload: serde_bytes::ByteBuf::from(encoded),
            magic: item.magic,
            content_type: item.content_type,
            content_encoding: item.content_encoding,
            content_language: item.content_language,
            schema: None,
        })
    }
}

/// Build a rain meta document from a sequence of BuildItems.
pub fn build_bytes(magic: KnownMagic, items: Vec<BuildItem>) -> anyhow::Result<Vec<u8>> {
    let mut metas: Vec<RainMetaDocumentV1Item> = vec![];
    for item in items {
        metas.push(RainMetaDocumentV1Item::try_from(&item)?);
    }
    Ok(RainMetaDocumentV1Item::cbor_encode_seq(&metas, magic)?)
}

/// Build a rain meta document from command line options.
/// Enforces length constraints on the input paths, magic numbers, content types,
/// content encodings and content languages.
/// Handles reading input files and writing to files/stdout according to the
/// build options.
pub fn build(b: Build) -> anyhow::Result<()> {
    if b.input_path.len() != b.magic.len() {
        return Err(anyhow!(
            "{} inputs does not match {} magic numbers.",
            b.input_path.len(),
            b.magic.len()
        ));
    }

    if b.input_path.len() != b.content_type.len() {
        return Err(anyhow!(
            "{} inputs does not match {} content types.",
            b.input_path.len(),
            b.content_type.len()
        ));
    }

    if b.input_path.len() != b.content_encoding.len() {
        return Err(anyhow!(
            "{} inputs does not match {} content encodings.",
            b.input_path.len(),
            b.content_encoding.len()
        ));
    }

    if b.input_path.len() != b.content_language.len() {
        return Err(anyhow!(
            "{} inputs does not match {} content languages.",
            b.input_path.len(),
            b.content_language.len()
        ));
    }
    let mut items: Vec<BuildItem> = vec![];
    for (input_path, magic, content_type, content_encoding, content_language) in izip!(
        b.input_path.iter(),
        b.magic.iter(),
        b.content_type.iter(),
        b.content_encoding.iter(),
        b.content_language.iter()
    ) {
        items.push(BuildItem {
            data: std::fs::read(input_path)?,
            magic: *magic,
            content_type: *content_type,
            content_encoding: *content_encoding,
            content_language: *content_language,
        });
    }
    crate::cli::output::output(
        &b.output_path,
        b.output_encoding,
        &build_bytes(b.global_magic, items)?,
    )
}

#[cfg(all(test, not(target_family = "wasm")))]
mod tests {
    use strum::IntoEnumIterator;
    use crate::meta::{
        magic::{self, KnownMagic},
        ContentType, ContentEncoding, ContentLanguage, RainMetaDocumentV1Item,
    };
    use super::BuildItem;
    use super::build_bytes;

    /// Test that the magic number prefix is correct for all known magic numbers
    /// in isolation from all build items.
    #[test]
    fn test_build_empty() -> anyhow::Result<()> {
        for global_magic in magic::KnownMagic::iter() {
            let built_bytes = build_bytes(global_magic, vec![])?;
            assert_eq!(built_bytes, global_magic.to_prefix_bytes());
        }
        Ok(())
    }

    /// We can build a single document item from a single build item.
    /// Empty ABI documents are used to avoid testing the normalisation and
    /// encoding process.
    #[test]
    fn test_into_meta_document() -> anyhow::Result<()> {
        let build_item = BuildItem {
            data: "[]".as_bytes().to_vec(),
            magic: KnownMagic::SolidityAbiV2,
            content_type: ContentType::Json,
            content_encoding: ContentEncoding::None,
            content_language: ContentLanguage::En,
        };

        let meta_document = RainMetaDocumentV1Item::try_from(&build_item)?;
        let expected_meta_document = RainMetaDocumentV1Item {
            payload: serde_bytes::ByteBuf::from("[]".as_bytes().to_vec()),
            magic: KnownMagic::SolidityAbiV2,
            content_type: ContentType::Json,
            content_encoding: ContentEncoding::None,
            content_language: ContentLanguage::En,
            schema: None,
        };
        assert_eq!(meta_document, expected_meta_document);
        Ok(())
    }

    /// The final CBOR bytes are as expected for a single build item. An empty
    /// ABI is used to avoid testing the normalisation and encoding process.
    #[test]
    fn test_empty_item() -> anyhow::Result<()> {
        let build_item = BuildItem {
            data: "[]".as_bytes().to_vec(),
            magic: KnownMagic::SolidityAbiV2,
            content_type: ContentType::Json,
            content_encoding: ContentEncoding::Identity,
            content_language: ContentLanguage::En,
        };

        let bytes = super::build_bytes(KnownMagic::RainMetaDocumentV1, vec![build_item.clone()])?;

        // https://github.com/rainprotocol/specs/blob/main/metadata-v1.md#example
        // 8 byte magic number prefix
        assert_eq!(
            &bytes[0..8],
            KnownMagic::RainMetaDocumentV1.to_prefix_bytes()
        );
        // cbor map with 5 keys
        assert_eq!(bytes[8], 0xa5);
        // key 0
        assert_eq!(bytes[9], 0x00);
        // major type 2 (bytes) length 2
        assert_eq!(bytes[10], 0b010_00010);
        // payload
        assert_eq!(bytes[11..13], "[]".as_bytes()[..]);
        // key 1
        assert_eq!(bytes[13], 0x01);
        // major type 0 (unsigned integer) value 27
        assert_eq!(bytes[14], 0b000_11011);
        // magic number
        assert_eq!(&bytes[15..23], KnownMagic::SolidityAbiV2.to_prefix_bytes());
        // key 2
        assert_eq!(bytes[23], 0x02);
        // text string application/json length 16
        assert_eq!(bytes[24], 0b011_10000);
        // the string application/json
        assert_eq!(&bytes[25..41], "application/json".as_bytes());
        // key 3
        assert_eq!(bytes[41], 0x03);
        // text string identity length 8
        assert_eq!(bytes[42], 0b011_01000);
        // the string identity
        assert_eq!(&bytes[43..51], "identity".as_bytes());
        // key 4
        assert_eq!(bytes[51], 0x04);
        // text string en length 2
        assert_eq!(bytes[52], 0b011_00010);
        // the string en
        assert_eq!(&bytes[53..55], "en".as_bytes());

        assert_eq!(bytes.len(), 55);

        Ok(())
    }

    #[test]
    fn test_cbor_encoding_type() -> anyhow::Result<()> {
        let build_item = BuildItem {
            data: "[]".as_bytes().to_vec(),
            magic: KnownMagic::DotrainV1,
            content_type: ContentType::Cbor,
            content_encoding: ContentEncoding::Identity,
            content_language: ContentLanguage::En,
        };

        let bytes = super::build_bytes(KnownMagic::RainMetaDocumentV1, vec![build_item.clone()])?;

        // https://github.com/rainprotocol/specs/blob/main/metadata-v1.md#example
        // 8 byte magic number prefix
        assert_eq!(
            &bytes[0..8],
            KnownMagic::RainMetaDocumentV1.to_prefix_bytes()
        );
        // cbor map with 5 keys
        assert_eq!(bytes[8], 0xa5);
        // key 0
        assert_eq!(bytes[9], 0x00);
        // major type 2 (bytes) length 2
        assert_eq!(bytes[10], 0b010_00010);
        // payload
        assert_eq!(bytes[11..13], "[]".as_bytes()[..]);
        // key 1
        assert_eq!(bytes[13], 0x01);
        // major type 0 (unsigned integer) value 27
        assert_eq!(bytes[14], 0b000_11011);
        // magic number
        assert_eq!(&bytes[15..23], KnownMagic::DotrainV1.to_prefix_bytes());
        // key 2
        assert_eq!(bytes[23], 0x02);
        // text string application/cbor length 16
        assert_eq!(bytes[24], 0b011_10000);
        // the string application/cbor
        assert_eq!(&bytes[25..41], "application/cbor".as_bytes());
        // key 3
        assert_eq!(bytes[41], 0x03);
        // text string identity length 8
        assert_eq!(bytes[42], 0b011_01000);
        // the string identity
        assert_eq!(&bytes[43..51], "identity".as_bytes());
        // key 4
        assert_eq!(bytes[51], 0x04);
        // text string en length 2
        assert_eq!(bytes[52], 0b011_00010);
        // the string en
        assert_eq!(&bytes[53..55], "en".as_bytes());

        assert_eq!(bytes.len(), 55);

        Ok(())
    }

    use clap::Parser;
    use std::io::Write;
    use super::{Build, build};

    /// Conversion normalizes the payload for the item's magic and then
    /// applies the content encoding.
    #[test]
    fn test_item_normalize_then_encode() -> anyhow::Result<()> {
        // "[ ]" normalizes to "[]" for a solidity ABI, then deflates.
        let build_item = BuildItem {
            data: "[ ]".as_bytes().to_vec(),
            magic: KnownMagic::SolidityAbiV2,
            content_type: ContentType::Json,
            content_encoding: ContentEncoding::Deflate,
            content_language: ContentLanguage::En,
        };
        let meta_document = RainMetaDocumentV1Item::try_from(&build_item)?;
        assert_eq!(
            meta_document.payload.as_ref(),
            ContentEncoding::Deflate.encode("[]".as_bytes())
        );

        // Un-normalizable data is rejected.
        let invalid_item = BuildItem {
            data: "not json".as_bytes().to_vec(),
            ..build_item
        };
        assert!(RainMetaDocumentV1Item::try_from(&invalid_item).is_err());
        Ok(())
    }

    fn parse_build(args: &[&str]) -> Build {
        Build::try_parse_from(args).unwrap()
    }

    /// Each arity guard fires with its own message, before any file IO:
    /// the input path never exists and yet the mismatch is what errors.
    #[test]
    fn test_build_arity_guards() {
        let b = parse_build(&[
            "build",
            "-i",
            "does-not-exist.json",
            "-m",
            "solidity-abi-v2",
            "-m",
            "solidity-abi-v2",
        ]);
        assert_eq!(
            build(b).unwrap_err().to_string(),
            "1 inputs does not match 2 magic numbers."
        );

        let b = parse_build(&[
            "build",
            "-i",
            "does-not-exist.json",
            "-m",
            "solidity-abi-v2",
            "-t",
            "json",
            "-t",
            "json",
        ]);
        assert_eq!(
            build(b).unwrap_err().to_string(),
            "1 inputs does not match 2 content types."
        );

        let b = parse_build(&[
            "build",
            "-i",
            "does-not-exist.json",
            "-m",
            "solidity-abi-v2",
            "-t",
            "json",
            "-e",
            "identity",
            "-e",
            "identity",
        ]);
        assert_eq!(
            build(b).unwrap_err().to_string(),
            "1 inputs does not match 2 content encodings."
        );

        let b = parse_build(&[
            "build",
            "-i",
            "does-not-exist.json",
            "-m",
            "solidity-abi-v2",
            "-t",
            "json",
            "-e",
            "identity",
            "-l",
            "en",
            "-l",
            "en",
        ]);
        assert_eq!(
            build(b).unwrap_err().to_string(),
            "1 inputs does not match 2 content languages."
        );
    }

    /// build() reads each input file, builds the document under the
    /// global magic and writes it to the output path; the hex output
    /// encoding is honored.
    #[test]
    fn test_build_reads_files_and_encodes_output() -> anyhow::Result<()> {
        let mut input = tempfile::NamedTempFile::new()?;
        input.write_all("[ ]".as_bytes())?;
        let output = tempfile::NamedTempFile::new()?;

        let expected = build_bytes(
            KnownMagic::RainMetaDocumentV1,
            vec![BuildItem {
                data: "[ ]".as_bytes().to_vec(),
                magic: KnownMagic::SolidityAbiV2,
                content_type: ContentType::Json,
                content_encoding: ContentEncoding::Identity,
                content_language: ContentLanguage::En,
            }],
        )?;

        let input_path = input.path().to_str().unwrap().to_string();
        let output_path = output.path().to_str().unwrap().to_string();

        let b = parse_build(&[
            "build",
            "-i",
            &input_path,
            "-m",
            "solidity-abi-v2",
            "-t",
            "json",
            "-e",
            "identity",
            "-l",
            "en",
            "-o",
            &output_path,
        ]);
        build(b)?;
        assert_eq!(std::fs::read(output.path())?, expected);

        let b = parse_build(&[
            "build",
            "-i",
            &input_path,
            "-m",
            "solidity-abi-v2",
            "-t",
            "json",
            "-e",
            "identity",
            "-l",
            "en",
            "-o",
            &output_path,
            "-E",
            "hex",
        ]);
        build(b)?;
        assert_eq!(
            std::fs::read_to_string(output.path())?,
            alloy::primitives::hex::encode_prefixed(&expected)
        );
        Ok(())
    }
}