Skip to main content

yog_data/
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};
8
9const HEADER: &str = "Generated by yog-data from Yog-Mod-Loader/rust/crates/yog-data — don't edit; regenerate with `cargo run -p yog-data --bin yog-data-export`.";
10
11/// A TypeScript module: the header, the tags, the strings' and frames' rules.
12pub fn typescript() -> String {
13    let magic = String::from_utf8_lossy(binary::MAGIC);
14    let mut out = format!(
15        "// {HEADER}\n\n\
16         /** What Yog Data starts with, and its version. */\n\
17         export const MAGIC = {magic:?};\n\
18         export const VERSION = {};\n\
19         /** Nested deeper than this is malformed. */\n\
20         export const MAX_DEPTH = {};\n\
21         /** A string's varint is odd for a new string (its length), even for one already read (its index). */\n\
22         export const NEW_STRING = {};\n\
23         /** A stream's length: this many bytes, little-endian. */\n\
24         export const STREAM_LEN_BYTES = {};\n\
25         /** A frame's length (`yog_data::io`): this many bytes, little-endian; and the longest frame. */\n\
26         export const FRAME_LEN_BYTES = {};\n\
27         export const MAX_FRAME = {};\n\
28         /** A result's two sides: `{{ ok }}` or `{{ err }}`. */\n\
29         export const RESULT_OK = {:?};\n\
30         export const RESULT_ERR = {:?};\n\
31         \n/** A value's first byte: what it is. */\nexport const tag = {{\n",
32        binary::VERSION,
33        binary::MAX_DEPTH,
34        binary::NEW_STRING,
35        binary::STREAM_LEN_BYTES,
36        size_of::<crate::io::Len>(),
37        crate::io::MAX_FRAME,
38        crate::schema::RESULT_OK,
39        crate::schema::RESULT_ERR,
40    );
41    for (name, value) in tag::ALL {
42        out.push_str(&format!("  {name}: {value},\n"));
43    }
44    out.push_str("} as const;\n");
45    // The records every tool writes.
46    let shapes = [Severity::shape(), Seg::shape(), Diagnostic::shape()];
47    out.push('\n');
48    out.push_str(&types::declarations(&shapes));
49    out
50}