2021-08-06 07:07:58 -04:00
|
|
|
import { mkdirSync, writeFileSync } from "fs";
|
2021-08-06 08:53:33 -04:00
|
|
|
import { join } from "path";
|
2020-06-19 03:16:23 -04:00
|
|
|
|
2021-08-08 03:40:42 -04:00
|
|
|
export const RUST_OUT_DIR = join(__dirname, "..", "rust", "common", "src", "gen");
|
2020-06-19 03:16:23 -04:00
|
|
|
|
|
|
|
try {
|
|
|
|
mkdirSync(RUST_OUT_DIR);
|
|
|
|
} catch (err) {
|
2021-08-06 07:07:58 -04:00
|
|
|
if (err.code !== "EEXIST") {
|
2020-06-19 03:16:23 -04:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
}
|
2021-08-06 07:07:58 -04:00
|
|
|
writeFileSync(
|
|
|
|
join(RUST_OUT_DIR, "mod.rs"),
|
|
|
|
`
|
2020-06-19 03:16:23 -04:00
|
|
|
pub mod attrs;
|
2020-07-09 03:06:08 -04:00
|
|
|
pub mod codepoints;
|
2020-06-19 03:16:23 -04:00
|
|
|
pub mod entities;
|
2021-08-06 07:07:58 -04:00
|
|
|
`
|
|
|
|
);
|
2020-06-19 03:16:23 -04:00
|
|
|
|
2021-08-06 07:07:58 -04:00
|
|
|
export const DATA_DIR = join(__dirname, "data");
|
2020-06-19 03:16:23 -04:00
|
|
|
|
2021-08-06 07:07:58 -04:00
|
|
|
export const leftPad = (str: string, n: number) =>
|
|
|
|
"0".repeat(n - str.length) + str;
|
2020-06-19 03:16:23 -04:00
|
|
|
|
|
|
|
export const prettyJson = (v: any) => JSON.stringify(v, null, 2);
|
|
|
|
|
2021-08-06 07:07:58 -04:00
|
|
|
export const byteStringLiteral = (bytes: number[]): string =>
|
2021-08-06 08:53:33 -04:00
|
|
|
[
|
|
|
|
'b"',
|
|
|
|
...bytes.map((c) => {
|
|
|
|
if (!Number.isSafeInteger(c) || c < 0 || c > 255) {
|
|
|
|
throw new Error("Not a byte");
|
|
|
|
}
|
2021-08-06 07:07:58 -04:00
|
|
|
// 0x20 == ' '.
|
|
|
|
// 0x7E == '~'.
|
|
|
|
// 0x5C == '\\'.
|
|
|
|
// 0x22 == '"'.
|
|
|
|
if (c >= 0x20 && c <= 0x7e && c != 0x5c && c != 0x22) {
|
|
|
|
return String.fromCharCode(c);
|
|
|
|
} else {
|
|
|
|
return `\\x${leftPad(c.toString(16), 2)}`;
|
|
|
|
}
|
2021-08-06 08:53:33 -04:00
|
|
|
}),
|
|
|
|
'"',
|
|
|
|
].join("");
|