Skip to main content

yog_data/wire/
export.rs

1//! The binary form's constants for the other languages' readers — the
2//! IDE's TypeScript (`yog-data-export`), so the tags are written once.
3
4mod types;
5
6use crate::binary::{self, tag};
7use crate::{Diagnostic, Schema, Seg, Severity};
8use yog_codegen::{Header, Literals, Regenerate};
9use yog_codegen_typescript::TypeScript;
10
11const HEADER: Header = Header {
12    tool: "yog-data",
13    source: "Yog-Mod-Loader/rust/crates/yog-data",
14    regenerate: Regenerate::Command("cargo run -p yog-data --bin yog-data-export"),
15};
16
17/// A TypeScript module: the header, the tags, the strings' and frames' rules.
18pub fn typescript() -> String {
19    let magic = String::from_utf8_lossy(binary::MAGIC);
20    let mut out = format!(
21        "{}\n\
22         /** What Yog Data starts with, and its version. */\n\
23         export const MAGIC = {};\n\
24         export const VERSION = {};\n\
25         /** Nested deeper than this is malformed. */\n\
26         export const MAX_DEPTH = {};\n\
27         /** A string's varint is odd for a new string (its length), even for one already read (its index). */\n\
28         export const NEW_STRING = {};\n\
29         /** A stream's length: this many bytes, little-endian. */\n\
30         export const STREAM_LEN_BYTES = {};\n\
31         /** A frame's length (`yog_data::io`): this many bytes, little-endian; and the longest frame. */\n\
32         export const FRAME_LEN_BYTES = {};\n\
33         export const MAX_FRAME = {};\n\
34         /** A result's two sides: `{{ ok }}` or `{{ err }}`. */\n\
35         export const RESULT_OK = {};\n\
36         export const RESULT_ERR = {};\n\
37         \n/** A value's first byte: what it is. */\nexport const tag = {{\n",
38        HEADER.comment::<TypeScript>(),
39        TypeScript::string(&magic),
40        binary::VERSION,
41        binary::MAX_DEPTH,
42        binary::NEW_STRING,
43        binary::STREAM_LEN_BYTES,
44        size_of::<crate::io::Len>(),
45        crate::io::MAX_FRAME,
46        TypeScript::string(crate::schema::RESULT_OK),
47        TypeScript::string(crate::schema::RESULT_ERR),
48    );
49    for (name, value) in tag::ALL {
50        out.push_str(&format!("  {name}: {value},\n"));
51    }
52    out.push_str("} as const;\n");
53    // The records every tool writes.
54    let shapes = [Severity::shape(), Seg::shape(), Diagnostic::shape()];
55    out.push('\n');
56    out.push_str(&types::declarations(&shapes));
57    out
58}