Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretty-print precise capture syntax (use<>) #80

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,19 @@ impl Printer {
#[cfg(feature = "verbatim")]
fn type_param_bound_verbatim(&mut self, tokens: &TokenStream) {
use syn::parse::{Parse, ParseStream, Result};
use syn::{parenthesized, token, Token};
use syn::{parenthesized, token, Ident, Lifetime, Token};

enum TypeParamBoundVerbatim {
Ellipsis,
PreciseCapture(Vec<Capture>),
TildeConst(TraitBound),
}

enum Capture {
Lifetime(Lifetime),
Type(Ident),
}

impl Parse for TypeParamBoundVerbatim {
fn parse(input: ParseStream) -> Result<Self> {
let content;
Expand All @@ -167,7 +173,33 @@ impl Printer {
(None, input)
};
let lookahead = content.lookahead1();
if lookahead.peek(Token![~]) {
if lookahead.peek(Token![use]) {
input.parse::<Token![use]>()?;
input.parse::<Token![<]>()?;
let mut captures = Vec::new();
loop {
let lookahead = input.lookahead1();
captures.push(if lookahead.peek(Lifetime) {
input.parse().map(Capture::Lifetime)?
} else if lookahead.peek(Ident) {
input.parse().map(Capture::Type)?
} else if lookahead.peek(Token![>]) {
break;
} else {
return Err(lookahead.error());
});
let lookahead = input.lookahead1();
if lookahead.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else if lookahead.peek(Token![>]) {
break;
} else {
return Err(lookahead.error());
}
}
input.parse::<Token![>]>()?;
Ok(TypeParamBoundVerbatim::PreciseCapture(captures))
} else if lookahead.peek(Token![~]) {
content.parse::<Token![~]>()?;
content.parse::<Token![const]>()?;
let mut bound: TraitBound = content.parse()?;
Expand All @@ -191,6 +223,19 @@ impl Printer {
TypeParamBoundVerbatim::Ellipsis => {
self.word("...");
}
TypeParamBoundVerbatim::PreciseCapture(captures) => {
self.word("use<");
for capture in captures.iter().delimited() {
match *capture {
Capture::Lifetime(lifetime) => self.lifetime(lifetime),
Capture::Type(ident) => self.ident(ident),
}
if !capture.is_last {
self.word(", ");
}
}
self.word(">");
}
TypeParamBoundVerbatim::TildeConst(trait_bound) => {
let tilde_const = true;
self.trait_bound(&trait_bound, tilde_const);
Expand Down
Loading