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
4use crate::binary::{self, tag};
5
6const 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`.";
7
8/// A TypeScript module: the header, the tags, the strings' and frames' rules.
9pub fn typescript() -> String {
10    let magic = String::from_utf8_lossy(binary::MAGIC);
11    let mut out = format!(
12        "// {HEADER}\n\n\
13         /** What Yog Data starts with, and its version. */\n\
14         export const MAGIC = {magic:?};\n\
15         export const VERSION = {};\n\
16         /** Nested deeper than this is malformed. */\n\
17         export const MAX_DEPTH = {};\n\
18         /** A string's varint is odd for a new string (its length), even for one already read (its index). */\n\
19         export const NEW_STRING = {};\n\
20         /** A stream's length: this many bytes, little-endian. */\n\
21         export const STREAM_LEN_BYTES = {};\n\
22         /** A frame's length (`yog_data::io`): this many bytes, little-endian; and the longest frame. */\n\
23         export const FRAME_LEN_BYTES = {};\n\
24         export const MAX_FRAME = {};\n\
25         /** A result's two sides: `{{ ok }}` or `{{ err }}`. */\n\
26         export const RESULT_OK = {:?};\n\
27         export const RESULT_ERR = {:?};\n\
28         \n/** A value's first byte: what it is. */\nexport const tag = {{\n",
29        binary::VERSION,
30        binary::MAX_DEPTH,
31        binary::NEW_STRING,
32        binary::STREAM_LEN_BYTES,
33        size_of::<crate::io::Len>(),
34        crate::io::MAX_FRAME,
35        crate::schema::RESULT_OK,
36        crate::schema::RESULT_ERR,
37    );
38    for (name, value) in tag::ALL {
39        out.push_str(&format!("  {name}: {value},\n"));
40    }
41    out.push_str("} as const;\n");
42    out
43}