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
#![deny(
anonymous_parameters,
bare_trait_objects,
elided_lifetimes_in_paths,
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
single_use_lifetimes,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
unstable_features,
unused_extern_crates,
unused_import_braces,
unused_qualifications,
unused_results,
variant_size_differences,
missing_debug_implementations,
missing_docs,
clippy::all,
clippy::restriction,
clippy::pedantic,
clippy::nursery,
clippy::cargo
)]
#![allow(
clippy::missing_docs_in_private_items,
clippy::missing_inline_in_public_items,
clippy::implicit_return
)]
#![no_std]
#[allow(unused_extern_crates)]
extern crate alloc;
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{
parse::{Parse, ParseStream, Result},
parse_macro_input, LitStr, Token,
};
#[proc_macro]
pub fn to_lowercase(input: TokenStream) -> TokenStream {
let src_token: LitStr = parse_macro_input!(input as LitStr);
let dst = src_token.value().to_lowercase();
LitStr::new(&dst, src_token.span())
.into_token_stream()
.into()
}
#[proc_macro]
pub fn to_uppercase(input: TokenStream) -> TokenStream {
let src_token: LitStr = parse_macro_input!(input as LitStr);
let dst = src_token.value().to_uppercase();
LitStr::new(&dst, src_token.span())
.into_token_stream()
.into()
}
#[proc_macro]
pub fn replace(input: TokenStream) -> TokenStream {
struct Replace {
src: LitStr,
from: LitStr,
to: LitStr,
}
impl Parse for Replace {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let src = input.parse::<LitStr>()?;
let _ = input.parse::<Token![,]>()?;
let from = input.parse::<LitStr>()?;
let _ = input.parse::<Token![,]>()?;
let to = input.parse::<LitStr>()?;
Ok(Self { src, from, to })
}
}
impl Replace {
fn exec(&self) -> LitStr {
let src = self.src.value();
let from = self.from.value();
let to = self.to.value();
let dst = src.replace(&from, &to);
LitStr::new(&dst, self.src.span())
}
}
let f: Replace = parse_macro_input!(input as Replace);
f.exec().into_token_stream().into()
}
#[allow(unused_macros)]
macro_rules! emit_error {
($token:expr, $msg: expr) => {
return TokenStream::from(syn::Error::new($token.span(), $msg).to_compile_error());
};
}
#[cfg(feature = "regex")]
#[proc_macro]
pub fn verified_regex(input: TokenStream) -> TokenStream {
use alloc::string::ToString;
use regex::Regex;
let src_token: LitStr = parse_macro_input!(input as LitStr);
if let Err(e) = Regex::new(&src_token.value()) {
emit_error!(src_token, e.to_string());
}
src_token.into_token_stream().into()
}
#[cfg(feature = "http")]
#[proc_macro]
pub fn verified_header_name(input: TokenStream) -> TokenStream {
use alloc::string::ToString;
use http::header::HeaderName;
let src_token: LitStr = parse_macro_input!(input as LitStr);
if let Err(e) = HeaderName::from_lowercase(src_token.value().as_bytes()) {
emit_error!(src_token, e.to_string());
}
src_token.into_token_stream().into()
}