onlytypes-core 0.1.4

Core functionality for onlytypes-rs
Documentation
mod stubbing;
mod ignore;
mod utils;

#[cfg(feature = "debugging")]
pub mod debugging;

pub use proc_macro2::TokenStream;

/// A trait that allows for conversion both ways between two types.
pub trait BothWays<T>: From<T> + Into<T> {}

/// A trait that allows for conversion between two types, but only if the intermediate type can be converted into the target type.
pub trait SomehowInto<T, Itermediate: Into<T>>: Into<Itermediate> {
    fn somehow(self) -> T {
        self.into().into()
    }
}

/// Ignores the input and returns an empty token stream.
pub fn ignore<T: BothWays<TokenStream>>(_cfg: T, input: T) -> T {
    ignore::ignore(input.into()).into()
}

pub fn stub<T: BothWays<TokenStream>>(_cfg: T, input: T) -> T {
    use syn::*;
    let input = input.into();
    if let Ok(f) = syn::parse2::<ItemFn>(input.clone()) {
        return stubbing::stub_function(f).somehow();
    }
    if let Ok(f) = syn::parse2::<ImplItemFn>(input.clone()) {
        return stubbing::stub_function(f).somehow();
    }
    if let Ok(i) = syn::parse2::<ItemImpl>(input.clone()) {
        return stubbing::stub_impl(i).somehow();
    }
    panic!("Unable to stub that thing!");
}

impl<T, U> BothWays<U> for T where T: From<U> + Into<U> {}

impl<T, U, I> SomehowInto<U, I> for T where T: Into<I>, I: Into<U> {}