trackable_derive-1.0.0/.cargo_vcs_info.json0000644000000001121365417332600144220ustar00{ "git": { "sha1": "2e4444d904ce2650bd0a346912017a9e90f6e318" } } trackable_derive-1.0.0/.gitignore010066600017500001750000000000361342137113000152050ustar0000000000000000/target **/*.rs.bk Cargo.lock trackable_derive-1.0.0/.travis.yml010066600017500001750000000001431342137113000153250ustar0000000000000000language: rust rust: - stable - beta - nightly matrix: allow_failures: - rust: nightly trackable_derive-1.0.0/Cargo.toml0000644000000020461365417332600124300ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "trackable_derive" version = "1.0.0" authors = ["Takeru Ohta "] description = "Custom derive for `trackable` crate" homepage = "https://github.com/sile/trackable_derive" readme = "README.md" keywords = ["trackable", "custom-derive"] license = "MIT" repository = "https://github.com/sile/trackable_derive" [lib] proc-macro = true [dependencies.quote] version = "1" [dependencies.syn] version = "1" [dev-dependencies.trackable] version = "0.2" [badges.travis-ci] repository = "sile/trackable_derive" trackable_derive-1.0.0/Cargo.toml.orig010066600017500001750000000007671365417324000161310ustar0000000000000000[package] name = "trackable_derive" version = "1.0.0" authors = ["Takeru Ohta "] description = "Custom derive for `trackable` crate" homepage = "https://github.com/sile/trackable_derive" repository = "https://github.com/sile/trackable_derive" readme = "README.md" keywords = ["trackable", "custom-derive"] license = "MIT" [badges] travis-ci = {repository = "sile/trackable_derive"} [dependencies] quote = "1" syn = "1" [dev-dependencies] trackable = "0.2" [lib] proc-macro = truetrackable_derive-1.0.0/LICENSE010066600017500001750000000021051342137113000142210ustar0000000000000000The MIT License Copyright (c) 2018 Takeru Ohta Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. trackable_derive-1.0.0/README.md010066600017500001750000000012441342137113000144760ustar0000000000000000trackable_derive ================= [![Crates.io: trackable_derive](https://img.shields.io/crates/v/trackable_derive.svg)](https://crates.io/crates/trackable_derive) [![Documentation](https://docs.rs/trackable_derive/badge.svg)](https://docs.rs/trackable_derive) [![Build Status](https://travis-ci.org/sile/trackable_derive.svg?branch=master)](https://travis-ci.org/sile/trackable_derive) [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE) This crate provides `TrackableError` derive macro. This crate should not be used directly. See [trackable] documentation for the usage of `#[derive(TrackableError)]`. [trackable]: https://docs.rs/trackable trackable_derive-1.0.0/src/lib.rs010066600017500001750000000077741365417322100151510ustar0000000000000000//! This crate provides `TrackableError` derive macro. //! //! This crate should not be used directly. //! See [trackable] documentation for the usage of `#[derive(TrackableError)]`. //! //! [trackable]: https://docs.rs/trackable #![recursion_limit = "128"] extern crate proc_macro; #[macro_use] extern crate quote; #[macro_use] extern crate syn; use proc_macro::TokenStream; use syn::DeriveInput; #[doc(hidden)] #[proc_macro_derive(TrackableError, attributes(trackable))] pub fn derive_trackable_error(input: TokenStream) -> TokenStream { let ast = parse_macro_input!(input as DeriveInput); let expanded = impl_trackable_error(&ast); expanded.into() } fn impl_trackable_error(ast: &syn::DeriveInput) -> impl Into { let error = &ast.ident; let error_kind = get_error_kind(&ast.attrs); quote! { impl ::std::ops::Deref for #error { type Target = ::trackable::error::TrackableError<#error_kind>; #[inline] fn deref(&self) -> &Self::Target { &self.0 } } impl ::std::fmt::Display for #error { fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { self.0.fmt(f) } } impl ::std::error::Error for #error { fn source(&self) -> Option<&(::std::error::Error + 'static)> { self.0.source() } } impl ::trackable::Trackable for #error { type Event = ::trackable::Location; #[inline] fn history(&self) -> Option<&::trackable::History> { self.0.history() } #[inline] fn history_mut(&mut self) -> Option<&mut ::trackable::History> { self.0.history_mut() } } impl From<::trackable::error::TrackableError<#error_kind>> for #error { #[inline] fn from(f: ::trackable::error::TrackableError<#error_kind>) -> Self { #error(f) } } impl From<#error> for ::trackable::error::TrackableError<#error_kind> { #[inline] fn from(f: #error) -> Self { f.0 } } impl From<#error_kind> for #error { #[inline] fn from(f: #error_kind) -> Self { use ::trackable::error::ErrorKindExt; f.error().into() } } } } fn get_error_kind(attrs: &[syn::Attribute]) -> syn::Path { use syn::Lit::*; use syn::Meta::*; use syn::MetaNameValue; use syn::NestedMeta::*; let mut error_kind = "ErrorKind".to_owned(); let attrs = attrs .iter() .filter_map(|attr| { let path = &attr.path; if quote!(#path).to_string() == "trackable" { Some(attr.parse_meta().unwrap_or_else(|e| { panic!("invalid trackable syntax: {} (error={})", quote!(attr), e) })) } else { None } }) .flat_map(|m| match m { List(l) => l.nested, tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()), }) .map(|m| match m { Meta(m) => m, tokens => panic!("unsupported syntax: {}", quote!(#tokens).to_string()), }); for attr in attrs { match &attr { NameValue(MetaNameValue { path, lit: Str(value), .. }) if path .get_ident() .map_or(false, |ident| ident == "error_kind") => { error_kind = value.value().to_string(); } i @ List(..) | i @ Path(..) | i @ NameValue(..) => { panic!("unsupported option: {}", quote!(#i)) } } } match syn::parse_str(&error_kind) { Err(e) => panic!("{:?} is not a valid type (parse error: {})", error_kind, e), Ok(path) => path, } } trackable_derive-1.0.0/tests/derive.rs010066600017500001750000000026631342137113000162130ustar0000000000000000#[macro_use] extern crate trackable; #[macro_use] extern crate trackable_derive; #[test] fn derive_works() { use trackable::error::{ErrorKind as TrackableErrorKind, ErrorKindExt, TrackableError}; #[derive(Debug, Clone, TrackableError)] pub struct Error(TrackableError); #[derive(Debug, Clone)] pub enum ErrorKind { Other, } impl TrackableErrorKind for ErrorKind {} let error = track!(Error::from(ErrorKind::Other.cause("something wrong"))); let error = track!(error, "I passed here"); assert_eq!( format!("\nError: {}", error).replace('\\', "/"), r#" Error: Other (cause; something wrong) HISTORY: [0] at tests/derive.rs:19 [1] at tests/derive.rs:20 -- I passed here "# ); } #[test] fn error_kind_attribute_works() { use trackable::error::{ErrorKind as TrackableErrorKind, ErrorKindExt, TrackableError}; #[derive(Debug, Clone, TrackableError)] #[trackable(error_kind = "Failed")] pub struct Error(TrackableError); #[derive(Debug, Clone)] pub struct Failed; impl TrackableErrorKind for Failed {} let error = track!(Error::from(Failed.cause("something wrong"))); let error = track!(error, "I passed here"); assert_eq!( format!("\nError: {}", error).replace('\\', "/"), r#" Error: Failed (cause; something wrong) HISTORY: [0] at tests/derive.rs:44 [1] at tests/derive.rs:45 -- I passed here "# ); }