styx-embed-macros 3.0.1

Proc macros for embedding Styx schemas in binaries
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
#![doc = include_str!("../README.md")]
//! Proc macros for embedding Styx schemas in binaries.
//!
//! These macros compress schemas at compile time and embed them
//! with a magic header so they can be extracted without execution.
//!
//! Each schema must have a `meta { id ... }` block. The ID is used to
//! generate a unique static name, allowing multiple schemas to coexist
//! in the same binary.

use proc_macro::{Delimiter, Group, Literal, Punct, Spacing, TokenStream, TokenTree};
use unsynn::{Comma, DelimitedVec, Parse, TokenIter};

/// Magic bytes that identify an embedded Styx schema.
/// 16 bytes: "STYX_SCHEMA_V2\0\0"
const MAGIC: &[u8; 16] = b"STYX_SCHEMA_V2\0\0";

/// Extract the schema ID from a parsed styx document.
///
/// Looks for `meta { id <value> }` at the root level.
fn extract_schema_id(schema: &str) -> Result<String, String> {
    let value = styx_tree::parse(schema).map_err(|e| format!("failed to parse schema: {e}"))?;

    let obj = value
        .as_object()
        .ok_or_else(|| "schema root must be an object".to_string())?;

    let meta = obj
        .get("meta")
        .ok_or_else(|| "schema must have a `meta` block".to_string())?;

    let meta_obj = meta
        .as_object()
        .ok_or_else(|| "`meta` must be an object".to_string())?;

    let id_value = meta_obj
        .get("id")
        .ok_or_else(|| "`meta` block must have an `id` field".to_string())?;

    // ID can be a bare identifier or a quoted string
    if let Some(s) = id_value.as_str() {
        return Ok(s.to_string());
    }

    Err("`meta.id` must be a string or identifier".to_string())
}

/// Sanitize an ID for the human-readable part of the symbol name.
///
/// Replaces non-alphanumeric characters with underscores.
fn sanitize_id(id: &str) -> String {
    let mut result = String::with_capacity(id.len());
    for c in id.chars() {
        if c.is_ascii_alphanumeric() {
            result.push(c);
        } else {
            result.push('_');
        }
    }
    // Ensure it doesn't start with a digit
    if result.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        result.insert(0, '_');
    }
    result
}

/// Generate a unique symbol suffix from a schema ID.
///
/// Format: `{sanitized_id}_{hash8}` where hash8 is 8 hex chars of blake3.
/// This gives human-readable symbols with guaranteed uniqueness.
fn id_to_symbol_suffix(id: &str) -> String {
    let sanitized = sanitize_id(id);
    let hash = blake3::hash(id.as_bytes());
    let bytes = hash.as_bytes();
    format!(
        "{}_{:02x}{:02x}{:02x}{:02x}",
        sanitized, bytes[0], bytes[1], bytes[2], bytes[3]
    )
}

/// Build the embedded blob for a single schema.
///
/// Format (V2 - single schema per blob):
/// ```text
/// STYX_SCHEMA_V2\0\0           // 16 bytes magic
/// <decompressed_len:u32le>
/// <compressed_len:u32le>
/// <blake3:32bytes>             // hash of decompressed content
/// <lz4 compressed schema>
/// ```
fn build_embedded_blob(schema: &str) -> Vec<u8> {
    let decompressed = schema.as_bytes();
    let hash = blake3::hash(decompressed);
    let compressed = lz4_flex::compress_prepend_size(decompressed);

    let mut blob = Vec::with_capacity(16 + 4 + 4 + 32 + compressed.len());
    blob.extend_from_slice(MAGIC);
    blob.extend_from_slice(&(decompressed.len() as u32).to_le_bytes());
    blob.extend_from_slice(&(compressed.len() as u32).to_le_bytes());
    blob.extend_from_slice(hash.as_bytes());
    blob.extend_from_slice(&compressed);
    blob
}

/// Parse a string literal (regular or raw) and return its content.
fn parse_string_literal(lit: &unsynn::Literal) -> Option<String> {
    let s = lit.to_string();

    // Raw string: r#"..."# or r"..."
    if let Some(after_r) = s.strip_prefix("r") {
        // Find the opening quote pattern (r#, r##, etc.)
        let hash_count = after_r.chars().take_while(|&c| c == '#').count();
        let prefix_len = hash_count + 1; // hashes + '"'
        let suffix_len = 1 + hash_count; // '"' + hashes

        if after_r.len() >= prefix_len + suffix_len {
            return Some(after_r[prefix_len..after_r.len() - suffix_len].to_string());
        }
    }

    // Regular string: "..."
    if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
        let inner = &s[1..s.len() - 1];
        // Handle basic escapes
        let mut result = String::new();
        let mut chars = inner.chars().peekable();
        while let Some(c) = chars.next() {
            if c == '\\' {
                match chars.next() {
                    Some('n') => result.push('\n'),
                    Some('r') => result.push('\r'),
                    Some('t') => result.push('\t'),
                    Some('\\') => result.push('\\'),
                    Some('"') => result.push('"'),
                    Some('0') => result.push('\0'),
                    Some(other) => {
                        result.push('\\');
                        result.push(other);
                    }
                    None => result.push('\\'),
                }
            } else {
                result.push(c);
            }
        }
        return Some(result);
    }

    None
}

/// Generate the static declaration for an embedded schema.
fn generate_static(schema: &str) -> Result<TokenStream, String> {
    let id = extract_schema_id(schema)?;
    let suffix = id_to_symbol_suffix(&id);
    let blob = build_embedded_blob(schema);
    let blob_len = blob.len();

    // Generate: [u8; N] = [b0, b1, b2, ...];
    let mut array_contents = Vec::new();
    for (i, byte) in blob.iter().enumerate() {
        array_contents.push(TokenTree::Literal(Literal::u8_unsuffixed(*byte)));
        if i < blob.len() - 1 {
            array_contents.push(TokenTree::Punct(Punct::new(',', Spacing::Alone)));
        }
    }

    let output = format!(
        r#"
        #[used]
        #[unsafe(no_mangle)]
        #[cfg_attr(target_os = "macos", unsafe(link_section = "__DATA,__styx_schemas"))]
        #[cfg_attr(target_os = "linux", unsafe(link_section = ".styx_schemas"))]
        #[cfg_attr(target_os = "windows", unsafe(link_section = ".styx"))]
        static __STYX_SCHEMA_{suffix}: [u8; {blob_len}] = "#
    );

    let mut result: TokenStream = output.parse().unwrap();
    let array_group = TokenTree::Group(Group::new(
        Delimiter::Bracket,
        array_contents.into_iter().collect(),
    ));
    result.extend(std::iter::once(array_group));
    result.extend(";".parse::<TokenStream>().unwrap());

    Ok(result)
}

/// Embed a schema from an inline string literal.
///
/// The schema must have a `meta { id ... }` block.
///
/// # Example
///
/// ```rust,ignore
/// styx_embed::embed_inline!(r#"
/// meta { id my-schema, version 1.0.0 }
/// schema { @ @string }
/// "#);
/// ```
#[proc_macro]
pub fn embed_inline(input: TokenStream) -> TokenStream {
    let mut tokens = TokenIter::new(proc_macro2::TokenStream::from(input));

    let literal: unsynn::Literal = match Parse::parse(&mut tokens) {
        Ok(l) => l,
        Err(e) => {
            return format!("compile_error!(\"expected string literal: {e}\")")
                .parse()
                .unwrap();
        }
    };

    let schema = match parse_string_literal(&literal) {
        Some(s) => s,
        None => {
            return "compile_error!(\"expected string literal\")"
                .parse()
                .unwrap();
        }
    };

    match generate_static(&schema) {
        Ok(ts) => ts,
        Err(e) => format!("compile_error!(\"{}\")", e.replace('"', "\\\""))
            .parse()
            .unwrap(),
    }
}

/// Embed a schema from a file (reads at compile time).
///
/// The schema must have a `meta { id ... }` block.
///
/// # Example
///
/// ```rust,ignore
/// styx_embed::embed_file!("schema.styx");
/// ```
#[proc_macro]
pub fn embed_file(input: TokenStream) -> TokenStream {
    let mut tokens = TokenIter::new(proc_macro2::TokenStream::from(input));

    let literal: unsynn::Literal = match Parse::parse(&mut tokens) {
        Ok(l) => l,
        Err(e) => {
            return format!("compile_error!(\"expected file path string: {e}\")")
                .parse()
                .unwrap();
        }
    };

    let path = match parse_string_literal(&literal) {
        Some(s) => s,
        None => {
            return "compile_error!(\"expected string literal for file path\")"
                .parse()
                .unwrap();
        }
    };

    let content = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(e) => {
            return format!("compile_error!(\"failed to read {}: {}\")", path, e)
                .parse()
                .unwrap();
        }
    };

    match generate_static(&content) {
        Ok(ts) => ts,
        Err(e) => format!("compile_error!(\"{}\")", e.replace('"', "\\\""))
            .parse()
            .unwrap(),
    }
}

/// Embed multiple schema files (reads at compile time).
///
/// Each schema must have a `meta { id ... }` block. Each generates
/// its own static with a unique name derived from the ID.
///
/// # Example
///
/// ```rust,ignore
/// styx_embed::embed_files!(
///     "config.styx",
///     "plugin.styx",
/// );
/// ```
#[proc_macro]
pub fn embed_files(input: TokenStream) -> TokenStream {
    let mut tokens = TokenIter::new(proc_macro2::TokenStream::from(input));

    let literals: DelimitedVec<unsynn::Literal, Comma> = match Parse::parse(&mut tokens) {
        Ok(l) => l,
        Err(e) => {
            return format!("compile_error!(\"expected file path strings: {e}\")")
                .parse()
                .unwrap();
        }
    };

    let mut result = TokenStream::new();

    for delimited in literals.iter() {
        let path = match parse_string_literal(&delimited.value) {
            Some(s) => s,
            None => {
                return "compile_error!(\"expected string literal for file path\")"
                    .parse()
                    .unwrap();
            }
        };

        let content = match std::fs::read_to_string(&path) {
            Ok(c) => c,
            Err(e) => {
                return format!("compile_error!(\"failed to read {}: {}\")", path, e)
                    .parse()
                    .unwrap();
            }
        };

        match generate_static(&content) {
            Ok(ts) => result.extend(ts),
            Err(e) => {
                return format!("compile_error!(\"{}\")", e.replace('"', "\\\""))
                    .parse()
                    .unwrap();
            }
        }
    }

    if result.is_empty() {
        return "compile_error!(\"embed_files! requires at least one file\")"
            .parse()
            .unwrap();
    }

    result
}

/// Embed a schema file from OUT_DIR (for build script output).
///
/// The schema must have a `meta { id ... }` block.
///
/// # Example
///
/// ```rust,ignore
/// // In build.rs:
/// // facet_styx::generate_schema::<Config>("schema.styx");
///
/// // In src/main.rs:
/// styx_embed::embed_outdir_file!("schema.styx");
/// ```
#[proc_macro]
pub fn embed_outdir_file(input: TokenStream) -> TokenStream {
    let mut tokens = TokenIter::new(proc_macro2::TokenStream::from(input));

    let literal: unsynn::Literal = match Parse::parse(&mut tokens) {
        Ok(l) => l,
        Err(e) => {
            return format!("compile_error!(\"expected filename string: {e}\")")
                .parse()
                .unwrap();
        }
    };

    let filename = match parse_string_literal(&literal) {
        Some(s) => s,
        None => {
            return "compile_error!(\"expected string literal for filename\")"
                .parse()
                .unwrap();
        }
    };

    let out_dir = match std::env::var("OUT_DIR") {
        Ok(dir) => dir,
        Err(_) => {
            return "compile_error!(\"OUT_DIR not set - this macro must be used in a crate with a build script\")"
                .parse()
                .unwrap()
        }
    };

    let path = std::path::Path::new(&out_dir).join(&filename);
    let path_str = path.display().to_string();

    let content = match std::fs::read_to_string(&path) {
        Ok(c) => c,
        Err(e) => {
            return format!("compile_error!(\"failed to read {}: {}\")", path_str, e)
                .parse()
                .unwrap();
        }
    };

    match generate_static(&content) {
        Ok(ts) => ts,
        Err(e) => format!("compile_error!(\"{}\")", e.replace('"', "\\\""))
            .parse()
            .unwrap(),
    }
}

// Keep the old names as aliases for compatibility
#[proc_macro]
pub fn embed_schema(input: TokenStream) -> TokenStream {
    embed_inline(input)
}

#[proc_macro]
pub fn embed_schemas(input: TokenStream) -> TokenStream {
    embed_inline(input)
}