onlytypes-core 0.1.4

Core functionality for onlytypes-rs
Documentation
use crate::TokenStream;
use syn::*;

pub type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T,E>;

#[allow(unused)]
pub fn return_error(error: &str) -> crate::TokenStream {
    format!(r#"compile_error!("{}")"#, error).parse().expect("Failed to parse macro")
}

const STUBBED_BODY: &str = "{ unimplemented!(\"This is a stub function generated by onlytypes.\") }";

pub fn stub_func_body<'a>(b: &'a Block) -> Result<(&'a Block, Block)> {
    let token_stream: TokenStream = STUBBED_BODY.parse().expect("Failed to parse stubbed body");
    let stubbed = parse2::<Block>(token_stream).expect("Failed to parse stubbed body");
    Ok((b, stubbed))
}

pub struct SomeFunction {
    pub attrs: Vec<Attribute>,
    pub vis: Visibility,
    pub defaultness: Option<Token![default]>,
    pub sig: Signature,
    pub block: Block,
}

impl From<ItemFn> for SomeFunction {
    fn from(f: ItemFn) -> Self {
        Self {
            attrs: f.attrs,
            vis: f.vis,
            defaultness: None,
            sig: f.sig,
            block: *f.block,
        }
    }
}

impl From<ImplItemFn> for SomeFunction {
    fn from(f: ImplItemFn) -> Self {
        Self {
            attrs: f.attrs,
            vis: f.vis,
            defaultness: f.defaultness,
            sig: f.sig,
            block: f.block,
        }
    }
}

impl From<&ItemFn> for SomeFunction {
    fn from(f: &ItemFn) -> Self {
        Self::from(f.clone())
    }
}

impl From<&ImplItemFn> for SomeFunction {
    fn from(f: &ImplItemFn) -> Self {
        Self::from(f.clone())
    }
}