Compare commits
3 Commits
14e4ba0747
...
c399b89632
| Author | SHA1 | Date | |
|---|---|---|---|
| c399b89632 | |||
| 921088a22a | |||
| cbdc635cb3 |
12
Cargo.toml
12
Cargo.toml
@@ -5,8 +5,20 @@ description = "PHP Macro Pre-processor (PMP) written in Rust."
|
|||||||
version = "0.1.0-alpha"
|
version = "0.1.0-alpha"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
|
[workspace]
|
||||||
|
members = ["crates/*"]
|
||||||
|
resolver = "2"
|
||||||
|
|
||||||
|
[workspace.package]
|
||||||
|
edition = "2024"
|
||||||
|
authors = ["Dean J. Karstedt <dk@cl.team>"]
|
||||||
|
license-file = "LICENSE"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
pmp-emitter = { version = "1.0.0", path = "crates/pmp-emitter" }
|
||||||
|
|
||||||
clap = { version = "^4", features = ["derive"] }
|
clap = { version = "^4", features = ["derive"] }
|
||||||
|
owo-colors = { version = "^4", features = ["supports-colors"] }
|
||||||
|
|
||||||
rayon = "^1"
|
rayon = "^1"
|
||||||
|
|
||||||
|
|||||||
16
crates/pmp-emitter/Cargo.toml
Normal file
16
crates/pmp-emitter/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
[package]
|
||||||
|
name = "pmp-emitter"
|
||||||
|
version = "1.0.0"
|
||||||
|
|
||||||
|
edition.workspace = true
|
||||||
|
authors.workspace = true
|
||||||
|
license-file.workspace = true
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
proc-macro = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
syn = { version = "^2", features = ["full"] }
|
||||||
|
quote = "^1"
|
||||||
|
darling = "^0"
|
||||||
|
proc-macro2 = "^1"
|
||||||
185
crates/pmp-emitter/src/lib.rs
Normal file
185
crates/pmp-emitter/src/lib.rs
Normal file
@@ -0,0 +1,185 @@
|
|||||||
|
use quote::quote;
|
||||||
|
use proc_macro::TokenStream;
|
||||||
|
use syn::{ Lit, Path, Type, Ident, DeriveInput, parse_macro_input };
|
||||||
|
use darling::{ ast, Error, Result, FromMeta, FromVariant, FromDeriveInput };
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
#[derive(Debug, FromDeriveInput)]
|
||||||
|
#[darling(forward_attrs(pmp_emitter))]
|
||||||
|
struct EmitterInput {
|
||||||
|
ident: Ident,
|
||||||
|
data: ast::Data<VariantReceiver, ()>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, FromVariant)]
|
||||||
|
#[darling(forward_attrs(pmp_emitter))]
|
||||||
|
struct VariantReceiver {
|
||||||
|
ident: Ident,
|
||||||
|
fields: ast::Fields<Type>,
|
||||||
|
attrs: Vec<syn::Attribute>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
struct StrictBool(bool);
|
||||||
|
|
||||||
|
/// - `descriptor = "..."` - prefix shown before all variant messages (default: enum name)
|
||||||
|
/// - `exit = true|false` - whether to call process::exit (default: false)
|
||||||
|
#[derive(Debug, FromMeta)]
|
||||||
|
struct EnumAttrs {
|
||||||
|
#[darling(default)]
|
||||||
|
descriptor: Option<String>,
|
||||||
|
#[darling(default)]
|
||||||
|
exit: Option<StrictBool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// - `message = "..."` - error message (default: variant name as title case)
|
||||||
|
/// - `colorizer = fn` - optional fn(String) -> String for formatting
|
||||||
|
/// - `exit = true|false` - overrides enum-level exit
|
||||||
|
#[derive(Debug, FromMeta)]
|
||||||
|
struct VariantAttrs {
|
||||||
|
#[darling(default)]
|
||||||
|
exit: Option<StrictBool>,
|
||||||
|
#[darling(default)]
|
||||||
|
message: Option<String>,
|
||||||
|
#[darling(default)]
|
||||||
|
colorizer: Option<Path>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
impl FromMeta for StrictBool
|
||||||
|
{
|
||||||
|
fn from_value(value: &Lit) -> Result<Self>
|
||||||
|
{
|
||||||
|
match value {
|
||||||
|
Lit::Bool(b) => Ok(StrictBool(b.value)),
|
||||||
|
_ => Err(Error::unexpected_lit_type(value)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// intentionally NOT implementing from_word()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_pmp_attr<T: FromMeta>(attrs: &[syn::Attribute]) -> T
|
||||||
|
where
|
||||||
|
T: Default,
|
||||||
|
{
|
||||||
|
attrs
|
||||||
|
.iter()
|
||||||
|
.find(|a| a.path().is_ident("pmp_emitter"))
|
||||||
|
.map(|a| T::from_meta(&a.meta).expect("invalid pmp_emitter attribute"))
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for EnumAttrs {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { descriptor: None, exit: None }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for VariantAttrs {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { message: None, exit: None, colorizer: None }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
fn pascal_to_title(s: &str) -> String
|
||||||
|
{
|
||||||
|
let mut out = String::new();
|
||||||
|
|
||||||
|
for (i, ch) in s.chars().enumerate()
|
||||||
|
{
|
||||||
|
if ch.is_uppercase() && i > 0 { out.push(' '); }
|
||||||
|
out.push(ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
#[proc_macro_derive(PmpEmitter, attributes(pmp_emitter))]
|
||||||
|
pub fn pmp_emitter(item: TokenStream) -> TokenStream
|
||||||
|
{
|
||||||
|
let input = parse_macro_input!(item as DeriveInput);
|
||||||
|
|
||||||
|
// parse enum shape
|
||||||
|
let shape = match EmitterInput::from_derive_input(&input) {
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(e) => return e.write_errors().into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
// parse enum-level #[pmp_emitter(...)] directly from the raw attrs
|
||||||
|
let enum_attrs: EnumAttrs = parse_pmp_attr(&input.attrs);
|
||||||
|
|
||||||
|
let enum_ident = &shape.ident;
|
||||||
|
let descriptor = enum_attrs.descriptor
|
||||||
|
.unwrap_or_else(|| pascal_to_title(&enum_ident.to_string()));
|
||||||
|
let global_exit = enum_attrs.exit.map(|b| b.0).unwrap_or(false);
|
||||||
|
|
||||||
|
let variants = shape
|
||||||
|
.data
|
||||||
|
.take_enum()
|
||||||
|
.expect("PmpEmitter can only be derived on enums");
|
||||||
|
|
||||||
|
let match_arms = variants.iter().map(|v|
|
||||||
|
{
|
||||||
|
let var_ident = &v.ident;
|
||||||
|
let title = pascal_to_title(&var_ident.to_string());
|
||||||
|
|
||||||
|
// parse variant-level #[pmp_emitter(...)] from the variant's raw attrs
|
||||||
|
let vattrs: VariantAttrs = parse_pmp_attr(&v.attrs);
|
||||||
|
|
||||||
|
let message = vattrs.message.unwrap_or_else(|| title.clone());
|
||||||
|
let should_exit = vattrs.exit.map(|b| b.0).unwrap_or(global_exit);
|
||||||
|
|
||||||
|
// ┌─ Output format ───────────────────────────────┐
|
||||||
|
// │ {descriptor}: {Variant Title} ("{message}") │
|
||||||
|
// │ │
|
||||||
|
// │ {stacktrace} │
|
||||||
|
// └───────────────────────────────────────────────┘
|
||||||
|
let header = format!("{}: {} (\"{}\")", descriptor, title, message);
|
||||||
|
|
||||||
|
let formatted_header = match &vattrs.colorizer
|
||||||
|
{
|
||||||
|
Some(path) => quote! { #path(#header) },
|
||||||
|
None => quote! { #header.to_string() },
|
||||||
|
};
|
||||||
|
|
||||||
|
let exit_tokens = if should_exit { quote! { std::process::exit(1); } } else { quote! {} };
|
||||||
|
|
||||||
|
match v.fields.style
|
||||||
|
{
|
||||||
|
ast::Style::Tuple => quote! {
|
||||||
|
#enum_ident::#var_ident(trace) =>
|
||||||
|
{
|
||||||
|
println!("{}\n\n{}", #formatted_header, trace);
|
||||||
|
#exit_tokens
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => quote! {
|
||||||
|
#enum_ident::#var_ident =>
|
||||||
|
{
|
||||||
|
println!("{}", #formatted_header);
|
||||||
|
#exit_tokens
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
quote! {
|
||||||
|
impl #enum_ident
|
||||||
|
{
|
||||||
|
pub fn emit(&self) {
|
||||||
|
match self {
|
||||||
|
#(#match_arms),*
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.into()
|
||||||
|
}
|
||||||
0
src/cli/notifications.rs
Normal file
0
src/cli/notifications.rs
Normal file
Reference in New Issue
Block a user