Skip to content
Closed
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:
with:
crate: cargo-hack
- name: Build
run: cargo hack build --feature-powerset --mutually-exclusive-features=syn2,syn3 ${{ matrix.cargo_flags }}
run: cargo hack build --all-features ${{ matrix.cargo_flags }}
- name: Test
run: cargo hack test --feature-powerset --mutually-exclusive-features=syn2,syn3 --all-targets --no-fail-fast --workspace
run: cargo hack test --all-features --all-targets --no-fail-fast --workspace
- name: Doc Test
run: cargo test --features=darling --doc --no-fail-fast --workspace
- name: Build Docs
run: cargo doc --all-features --workspace
run: cargo doc --all-features --workspace
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,8 @@ macro_rules! __macro_handler {
$crate::__macro_handler! {! $name; $($(#attr=$attr)? $n: $input),+; $impl; $crate::__private::Dummy::None}
};
(! $name:ident; $($(#attr=$attr:tt)? $n:ident: $input:expr),+; $impl:expr $(; $dummy:expr)?) => {{
use $crate::__private::{ManyhowParse, ManyhowToTokens, ManyhowTry};
#[allow(unused_imports)]
use $crate::__private::{ManyhowParse, ManyhowParseSyn2, ManyhowParseSyn3, ManyhowToTokens, ManyhowTry};
let implementation = $impl;
$(let $n = &$crate::__private::WhatType::new();)+
if false {
Expand Down
46 changes: 26 additions & 20 deletions src/parse_to_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,23 @@ use crate::{
AnyTokenStream, AttributeMacroHandler, DeriveMacroHandler, Emitter, FunctionMacroHandler,
ToTokensError,
};

pub trait ManyhowParse<T> {
fn manyhow_parse(&self, input: impl AnyTokenStream, attr: bool) -> Result<T, TokenStream>;
}

pub trait ManyhowParseSyn2<T> {
fn manyhow_parse(&self, input: impl AnyTokenStream, attr: bool) -> Result<T, TokenStream>;
}

pub trait ManyhowParseSyn3<T> {
fn manyhow_parse(&self, input: impl AnyTokenStream, attr: bool) -> Result<T, TokenStream>;
}

pub trait ManyhowToTokens<T> {
fn manyhow_to_tokens(&self, input: T, tokens: &mut TokenStream);
}

pub trait ManyhowTry<T> {
type Ok;
type Err;
Expand Down Expand Up @@ -70,6 +81,13 @@ impl<E: ToTokensError> ManyhowToTokens<E> for WhatType<E> {
}
}

#[cfg(any(feature = "syn2", feature = "syn3"))]
impl<T: quote::ToTokens> ManyhowToTokens<T> for &WhatType<T> {
fn manyhow_to_tokens(&self, input: T, tokens: &mut TokenStream) {
input.to_tokens(tokens);
}
}

impl<T, E> ManyhowTry<Result<T, E>> for WhatType<Result<T, E>> {
type Err = E;
type Ok = T;
Expand All @@ -89,7 +107,7 @@ impl<T> ManyhowTry<T> for &WhatType<T> {
}

#[cfg(all(feature = "syn2", not(doc)))]
impl<T: syn2::parse::Parse> ManyhowParse<T> for &WhatType<T> {
impl<T: syn2::parse::Parse> ManyhowParseSyn2<T> for &WhatType<T> {
fn manyhow_parse(&self, input: impl AnyTokenStream, attr: bool) -> Result<T, TokenStream> {
let input = input.into();
let empty = input.is_empty();
Expand All @@ -103,27 +121,21 @@ impl<T: syn2::parse::Parse> ManyhowParse<T> for &WhatType<T> {
})
}
}
#[cfg(all(feature = "syn2", not(doc)))]
impl<T: quote::ToTokens> ManyhowToTokens<T> for &WhatType<T> {
fn manyhow_to_tokens(&self, input: T, tokens: &mut TokenStream) {
input.to_tokens(tokens);
}
}

#[cfg(all(feature = "syn2", not(doc)))]
#[test]
#[allow(unused)]
fn test_inference() {
fn syn2_test_inference() {
use syn2::parse::Parse;

if false {
let wt = &WhatType::new();
let ts: proc_macro::TokenStream = wt.manyhow_parse(quote::quote!(test), false).unwrap();
let wt = &WhatType::new();
if false {
let wt: Result<syn2::Ident, _> = wt.identify();
let wt: Result<syn2::LitInt, _> = wt.identify();
}
let ts: syn2::Ident = wt.manyhow_parse(quote::quote!(test), false).unwrap();
let ts: syn2::LitInt = wt.manyhow_parse(quote::quote!(test), false).unwrap();

struct Parsable;
impl Parse for Parsable {
Expand All @@ -138,7 +150,7 @@ fn test_inference() {
}

#[cfg(feature = "syn3")]
impl<T: syn3::parse::Parse> ManyhowParse<T> for &WhatType<T> {
impl<T: syn3::parse::Parse> ManyhowParseSyn3<T> for &WhatType<T> {
fn manyhow_parse(&self, input: impl AnyTokenStream, attr: bool) -> Result<T, TokenStream> {
let input = input.into();
let empty = input.is_empty();
Expand All @@ -152,27 +164,21 @@ impl<T: syn3::parse::Parse> ManyhowParse<T> for &WhatType<T> {
})
}
}
#[cfg(feature = "syn3")]
impl<T: quote::ToTokens> ManyhowToTokens<T> for &WhatType<T> {
fn manyhow_to_tokens(&self, input: T, tokens: &mut TokenStream) {
input.to_tokens(tokens);
}
}

#[cfg(feature = "syn3")]
#[test]
#[allow(unused)]
fn test_inference() {
fn syn3_test_inference() {
use syn3::parse::Parse;

if false {
let wt = &WhatType::new();
let ts: proc_macro::TokenStream = wt.manyhow_parse(quote::quote!(test), false).unwrap();
let wt = &WhatType::new();
if false {
let wt: Result<syn3::Ident, _> = wt.identify();
let wt: Result<syn3::LitInt, _> = wt.identify();
}
let ts: syn3::Ident = wt.manyhow_parse(quote::quote!(test), false).unwrap();
let ts: syn3::LitInt = wt.manyhow_parse(quote::quote!(test), false).unwrap();

struct Parsable;
impl Parse for Parsable {
Expand Down