| Crates.io | seferize |
| lib.rs | seferize |
| version | 1.5.6 |
| created_at | 2025-10-10 02:09:25.392844+00 |
| updated_at | 2025-10-25 19:16:43.319041+00 |
| description | Procedural macro for converting Rust code blocks into string constants for reflection or documentation generation. |
| homepage | |
| repository | https://github.com/patrickfp93/seferize |
| max_upload_size | |
| id | 1876430 |
| size | 23,802 |
"Revealing the written form of your Rust code." β¨
A procedural macro that converts any Rust item (struct, enum, trait, impl, fields, variants, methods, etc.) into its string representation at compile time.
It can automatically generate a &'static str constant with the textual content of the item β useful for reflection, documentation generation, code generation tests,code introspection, or debugging macro systems.
#[ignore] to exclude specific items or blocks from being stringified.#[stringify].#[stringify("TEXT")] or #[stringify(TEXT)].#[expose_for_tests] attribute to generate a public version of private functions only for tests, keeping the original function private.When you annotate an item with #[stringify], it generates a &'static str constant containing the exact Rust source of that item.
By default, the generated constant name follows the pattern CODE_<ITEM_NAME> unless another name is provided.
use seferize::stringify;
#[stringify]
pub struct User {
id: u32,
name: String,
}
Generates:
pub const CODE_USER: &str = "pub struct User { id: u32, name: String }";
You can apply #[stringify] directly to fields, enum variants, or methods inside an item annotated with #[stringify].
Each annotated element will generate its own &'static str constant alongside the main one.
use seferize::stringify;
#[stringify]
pub struct ExtractStruct {
pub field_1: usize,
#[stringify("FIELD_2")]
pub field_2: String,
}
Produces:
pub const CODE_EXTRACT_STRUCT: &'static str =
"pub struct ExtractStruct { pub field_1: usize, pub field_2: String, }";
pub const FIELD_2: &'static str = "pub field_2: String";
pub struct ExtractStruct {
pub field_1: usize,
pub field_2: String,
}
The same logic applies to enum variants and impl methods.
Use the #[expose_for_tests] attribute to generate a public version of a private function that is only available in test builds (#[cfg(test)]).
The original function stays private and encapsulated.
use seferize::expose_for_tests;
struct MyStruct;
impl MyStruct {
#[expose_for_tests]
fn hidden(&self) -> i32 {
42
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn can_call_hidden() {
let s = MyStruct;
// Call the public test version
assert_eq!(s.test_hidden(), 42);
}
}
The #[stringify] attribute accepts both quoted and unquoted identifiers for naming the generated constant:
#[stringify("CUSTOM_NAME")]
// or
#[stringify(CUSTOM_NAME)]
Both produce the same result:
pub const CUSTOM_NAME: &str = "...";
#[ignore]Mark any item, block, or function with #[ignore] to prevent it from being included in the generated string.
use seferize::stringify;
#[stringify]
mod my_module {
pub struct Visible {
field: i32,
}
#[ignore]
pub fn hidden_fn() {
println!("This function won't appear in CODE_MY_MODULE");
}
}
Generates:
pub const CODE_MY_MODULE: &str = r#"
mod my_module {
pub struct Visible {
field: i32,
}
}
"#;
Override the generated name by passing a custom identifier:
use seferize::stringify;
#[stringify("CUSTOM_NAME")]
pub enum Event {
Created,
Updated,
}
Produces:
pub const CUSTOM_NAME: &str = "pub enum Event { Created, Updated }";
seferize::stringify, stringify, seferize::ignore, and ignore are removed before string generation.#[stringify] produce individual constants.#[stringify(TEXT)] and #[stringify("TEXT")] are both supported.syn and quote crates for parsing and regenerating code.#[expose_for_tests] clones the original function, makes it pub and wraps it in #[cfg(test)].Add to your Cargo.toml:
[dependencies]
seferize = "1.5.5"
src/
βββ main.rs
βββ lib.rs
βββ samples/
βββ user.rs
βββ example.rs
The name Seferize comes from the Hebrew word βSeferβ (Χ‘Χ€Χ¨), meaning βbookβ or βscrollβ, symbolizing the act of revealing written words.
βThe entrance of Thy words giveth light.β β Psalm 119:130