v_escape_codegen_base/lib.rs
1#![allow(unused)]
2#![deny(missing_docs)]
3
4//! Code generation base library for v_escape
5//!
6//! This crate provides the core functionality for generating SIMD-optimized escape functions
7//! from character mappings. It parses template files and generates the necessary code
8//! for efficient string escaping operations.
9
10use generator::{Generator, parse_template};
11use proc_macro2::TokenStream;
12
13mod generator;
14mod pairs;
15mod switch;
16
17/// Generate escape functions from a token stream template
18///
19/// This function takes a token stream representing character mappings and generates
20/// the corresponding escape function implementations. It returns both the generated
21/// code and metadata about the escape mappings.
22///
23/// # Arguments
24///
25/// * `tokens` - A token stream containing the character mappings in the format `new!(char -> "escape", ...)`
26/// * `crate_name` - The name of the crate where the generated code will be used
27///
28/// # Returns
29///
30/// Returns a tuple containing:
31/// * The generated code as a `TokenStream`
32/// * A `Vec<(u8, String)>` of `(char_byte, replacement)` pairs, sorted by `char_byte`
33/// ascending. Callers can derive convenience strings (such as the concatenation of
34/// all source bytes or replacements) from this list as needed.
35///
36/// # Errors
37///
38/// Returns a `syn::Error` if the token stream cannot be parsed or if the character
39/// mappings are invalid.
40pub fn generate(
41 tokens: TokenStream,
42 crate_name: &str,
43) -> syn::Result<(TokenStream, Vec<(u8, String)>)> {
44 let pairs = parse_template(tokens)?;
45 let generator = Generator::new(&pairs, crate_name);
46 let generated = generator.build();
47 let mappings: Vec<(u8, String)> = pairs.into_iter().map(|p| (p.ch, p.quote)).collect();
48
49 Ok((generated, mappings))
50}