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
#![doc = include_str!("../README.md")]
use proc_macro2::TokenStream;
#[cfg(test)]
use quote::quote;
mod product;
use product::Product;
mod token;
/// The scope mode for the product expansion. This defines how scopes are handled at the top
/// level.
pub enum ScopeMode {
/// Expand products in all scopes, including the root scope.
Expand,
/// Expand products in sub-scopes but not in the root scope. Thus one has to insert at
/// least one manual scope with `${ }` to expand products.
SubExpand,
/// Insert scopes automatically and close/expand them after a ending brace or a
/// semicolon. Then start a new scope This is convenient when a lot structs or impls shall
/// be independently expanded.
///
/// # Example
///
/// ```text
/// impl Foo for $((First)(Second)) { }
/// impl Bar for $((Third)(Fourth)) { }
/// ```
///
/// will treat each impl item as it is surrounded by it own scope and expand to:
///
/// ```text
/// impl Foo for First { }
/// impl Foo for Second { }
/// impl Bar for Third { }
/// impl Bar for Fourth { }
/// ```
AutoBraceSemi,
// when you read this, you prolly want to add another mode. Just do and send a PR.
}
/// Expands a product syntax into a `TokenStream`.
///
/// # Examples
///
/// ## Complete expansion
///
/// ```
/// # use quote::quote;
/// # use code_product_lib::*;
///
/// let input = quote! {
/// impl Foo for $((This)(That)) { }
/// };
///
/// let output = expand_products(input, ScopeMode::Expand);
/// assert_eq!(
/// output.to_string(),
/// "impl Foo for This { } \
/// impl Foo for That { }");
/// ```
///
/// ## Sub-expand
///
/// ```
/// # use quote::quote;
/// # use code_product_lib::*;
///
/// let input = quote! {
/// let toplevel = thing ;
/// ${ impl Foo for $((This)(That)) { } }
/// ${ impl Bar for $((This)(That)) { } }
/// };
///
/// let output = expand_products(input, ScopeMode::SubExpand);
/// assert_eq!(
/// output.to_string(),
/// "let toplevel = thing ; \
/// impl Foo for This { } \
/// impl Foo for That { } \
/// impl Bar for This { } \
/// impl Bar for That { }"
/// );
/// ```
///
/// ## Auto-brace-semi
///
/// ```
/// # use quote::quote;
/// # use code_product_lib::*;
///
/// let input = quote! {
/// impl Foo for $((This)(That)) { }
/// impl Bar for $((This)(That)) { }
/// };
///
/// let output = expand_products(input, ScopeMode::AutoBraceSemi);
/// assert_eq!(
/// output.to_string(),
/// "impl Foo for This { } \
/// impl Foo for That { } \
/// impl Bar for This { } \
/// impl Bar for That { }"
/// );
/// ```
pub fn expand_products(input: TokenStream, mode: ScopeMode) -> TokenStream {
Product::new(input).parse(mode).expand()
}
#[cfg(test)]
macro_rules! assert_expansion {
($expand_mode:ident {$($in:tt)*} == {$($out:tt)*}) => {
let input = quote! { $($in)* };
let output = expand_products(input, ScopeMode::$expand_mode);
let expected = quote! { $($out)* };
assert_eq!(output.to_string(), expected.to_string());
};
($($in:tt)*) => {
assert_expansion!(Expand {$($in)*} == {$($in)*});
};
(SubExpand $($in:tt)*) => {
assert_expansion!(SubExpand {$($in)*} == {$($in)*});
};
}
#[test]
fn smoke() {
assert_expansion! {
#[newtype]
pub struct MyNewtype<T>(T);
};
}
#[test]
fn smoke_sub() {
assert_expansion! {
noexpand
#[newtype]
pub struct MyNewtype<T>(T);
};
}
#[test]
fn test_tt_groups_parsed_recursive() {
let input = quote! {
{{$$}}
};
let output = expand_products(input, ScopeMode::Expand);
assert_eq!(output.to_string(), "{ { $ } }");
}
#[test]
fn test_scopes() {
assert_expansion! {
Expand {
foo ${bar ${baz}}
} == {
foo bar baz
}
};
}
#[test]
fn test_sub_scopes() {
assert_expansion! {
SubExpand {
foo ${bar ${baz}}
} == {
foo bar baz
}
};
}
#[test]
fn expand_at_escaping() {
assert_expansion! {
Expand {
$$ ${$$ foo}
} == {
$ $ foo
}
};
}
#[test]
#[should_panic]
fn single_at_is_invalid() {
assert_expansion! {
Expand {
$
} == {
$
}
};
}
#[test]
fn noexpand_leaves_at_verbatim() {
assert_expansion! {
SubExpand {
$ ${$$}
} == {
$ $
}
};
}
#[test]
fn simple_expand_unnamed() {
assert_expansion! {
Expand {
$((foo))
} == {
foo
}
};
}
#[test]
fn simple_expand_named() {
assert_expansion! {
Expand {
$(bar: (foo))
$bar
} == {
foo
}
};
}
#[test]
fn simple_expand_named_visible() {
assert_expansion! {
Expand {
$($bar: (foo))
} == {
foo
}
};
}
#[test]
fn empty_defintions_expand() {
assert_expansion! {
Expand {
$(()()())
$((foo)(bar)) ;
} == {
foo ;
foo ;
foo ;
bar ;
bar ;
bar ;
}
};
}