allow use with no_std

rename
Charles Johnson 2 years ago
parent e3a465443b
commit ad2c5c40eb
No known key found for this signature in database

@ -18,6 +18,7 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
[features] [features]
default = ["derive", "derive_serde"] default = ["std", "derive", "derive_serde"]
std = ["enumscribe_derive/std"]
derive = ["enumscribe_derive"] derive = ["enumscribe_derive"]
derive_serde = ["derive", "enumscribe_derive/serde"] derive_serde = ["derive", "enumscribe_derive/serde"]

@ -42,13 +42,13 @@
//! //!
//! assert_eq!(Website::try_unscribe("GiThUb.CoM"), Some(Website::Github)); //! assert_eq!(Website::try_unscribe("GiThUb.CoM"), Some(Website::Github));
//! ``` //! ```
//! //!
//! The same attribute can be used on the enum itself to make all variants case-insensitive. Individual fields may opt back //! The same attribute can be used on the enum itself to make all variants case-insensitive. Individual fields may opt back
//! in to case sensitivity with `#[enumscribe(case_sensitive)]`. //! in to case sensitivity with `#[enumscribe(case_sensitive)]`.
//! ```rust //! ```rust
//! use enumscribe::TryUnscribe; //! use enumscribe::TryUnscribe;
//! //!
//! #[derive(TryUnscribe, PartialEq, Eq, Debug)] //! #[derive(TryUnscribe, PartialEq, Eq, Debug)]
//! #[enumscribe(case_insensitive)] //! #[enumscribe(case_insensitive)]
//! enum Website { //! enum Website {
@ -181,12 +181,14 @@
//! you *really* don't want to use a `Cow` for whatever reason. //! you *really* don't want to use a `Cow` for whatever reason.
#![deny(missing_docs)] #![deny(missing_docs)]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use] #[macro_use]
extern crate enumscribe_derive; extern crate enumscribe_derive;
pub use enumscribe_derive::*; pub use enumscribe_derive::*;
#[cfg(feature = "std")]
use std::borrow::Cow; use std::borrow::Cow;
/// Trait for converting an enum to a static string slice. /// Trait for converting an enum to a static string slice.
@ -295,6 +297,7 @@ pub trait TryScribeStaticStr {
/// assert_eq!(Airport::Gatwick.scribe(), "LGW".to_owned()); /// assert_eq!(Airport::Gatwick.scribe(), "LGW".to_owned());
/// assert_eq!(Airport::Other("STN".to_owned()).scribe(), "STN".to_owned()); /// assert_eq!(Airport::Other("STN".to_owned()).scribe(), "STN".to_owned());
/// ``` /// ```
#[cfg(feature = "std")]
pub trait ScribeString { pub trait ScribeString {
/// Converts this enum to an allocated `String`. /// Converts this enum to an allocated `String`.
/// ///
@ -330,6 +333,7 @@ pub trait ScribeString {
/// assert_eq!(Airport::Gatwick.try_scribe(), Some("LGW".to_owned())); /// assert_eq!(Airport::Gatwick.try_scribe(), Some("LGW".to_owned()));
/// assert_eq!(Airport::Other("STN".to_owned()).try_scribe(), Some("STN".to_owned())); /// assert_eq!(Airport::Other("STN".to_owned()).try_scribe(), Some("STN".to_owned()));
/// ``` /// ```
#[cfg(feature = "std")]
pub trait TryScribeString { pub trait TryScribeString {
/// Converts this enum to an allocated `String`. /// Converts this enum to an allocated `String`.
/// ///
@ -381,6 +385,7 @@ pub trait TryScribeString {
/// assert_eq!(Airport::Other("STN".to_owned()).scribe(), /// assert_eq!(Airport::Other("STN".to_owned()).scribe(),
/// Cow::Owned::<'static, str>("STN".to_owned())); /// Cow::Owned::<'static, str>("STN".to_owned()));
/// ``` /// ```
#[cfg(feature = "std")]
pub trait ScribeCowStr { pub trait ScribeCowStr {
/// Converts this enum to a `Cow<'static, str>`. /// Converts this enum to a `Cow<'static, str>`.
/// ///
@ -430,6 +435,7 @@ pub trait ScribeCowStr {
/// assert_eq!(Airport::Other("STN".to_owned()).try_scribe(), /// assert_eq!(Airport::Other("STN".to_owned()).try_scribe(),
/// Some(Cow::Owned::<'static, str>("STN".to_owned()))); /// Some(Cow::Owned::<'static, str>("STN".to_owned())));
/// ``` /// ```
#[cfg(feature = "std")]
pub trait TryScribeCowStr { pub trait TryScribeCowStr {
/// Converts this enum to a `Option<Cow<'static, str>>`. /// Converts this enum to a `Option<Cow<'static, str>>`.
/// ///

@ -17,5 +17,6 @@ syn = "1.0"
quote = "1.0" quote = "1.0"
[features] [features]
default = ["serde"] default = ["std", "serde"]
std = []
serde = [] serde = []

@ -10,7 +10,7 @@ use proc_macro::TokenStream;
use proc_macro2::Ident; use proc_macro2::Ident;
use quote::quote; use quote::quote;
use syn::{Data, DataEnum, DeriveInput, Attribute}; use syn::{Attribute, Data, DataEnum, DeriveInput};
use error::{MacroError, MacroResult}; use error::{MacroError, MacroResult};
@ -364,10 +364,10 @@ pub fn derive_try_scribe_static_str(input: TokenStream) -> TokenStream {
gen_try_scribe_impl( gen_try_scribe_impl(
input, input,
quote! { ::enumscribe::TryScribeStaticStr }, quote! { ::enumscribe::TryScribeStaticStr },
quote! { ::std::option::Option<&'static str> }, quote! { ::core::option::Option<&'static str> },
|_, _, name| { |_, _, name| {
Ok(quote! { Ok(quote! {
::std::option::Option::Some(#name) ::core::option::Option::Some(#name)
}) })
}, },
|variant, enum_ident, _| { |variant, enum_ident, _| {
@ -381,7 +381,7 @@ pub fn derive_try_scribe_static_str(input: TokenStream) -> TokenStream {
variant.span, variant.span,
)) ))
}, },
quote! { ::std::option::Option::None }, quote! { ::core::option::Option::None },
) )
} }
@ -394,6 +394,7 @@ pub fn derive_try_scribe_static_str(input: TokenStream) -> TokenStream {
/// Since a `String` is returned, an allocation must always be performed, which is wasteful. /// Since a `String` is returned, an allocation must always be performed, which is wasteful.
/// [`ScribeCowStr`](derive.ScribeCowStr.html) should be preferred because it avoids unnecessary /// [`ScribeCowStr`](derive.ScribeCowStr.html) should be preferred because it avoids unnecessary
/// allocations. /// allocations.
#[cfg(feature = "std")]
#[proc_macro_derive(ScribeString, attributes(enumscribe))] #[proc_macro_derive(ScribeString, attributes(enumscribe))]
pub fn derive_scribe_string(input: TokenStream) -> TokenStream { pub fn derive_scribe_string(input: TokenStream) -> TokenStream {
gen_scribe_impl( gen_scribe_impl(
@ -434,6 +435,7 @@ pub fn derive_scribe_string(input: TokenStream) -> TokenStream {
/// Since a `String` is returned, an allocation must always be performed, which is wasteful. /// Since a `String` is returned, an allocation must always be performed, which is wasteful.
/// [`TryScribeCowStr`](derive.TryScribeCowStr.html) should be preferred because it avoids /// [`TryScribeCowStr`](derive.TryScribeCowStr.html) should be preferred because it avoids
/// unnecessary allocations. /// unnecessary allocations.
#[cfg(feature = "std")]
#[proc_macro_derive(TryScribeString, attributes(enumscribe))] #[proc_macro_derive(TryScribeString, attributes(enumscribe))]
pub fn derive_try_scribe_string(input: TokenStream) -> TokenStream { pub fn derive_try_scribe_string(input: TokenStream) -> TokenStream {
gen_try_scribe_impl( gen_try_scribe_impl(
@ -477,6 +479,7 @@ pub fn derive_try_scribe_string(input: TokenStream) -> TokenStream {
/// ///
/// This derive does not support ignoring variants with `#[enumscribe(ignore)]`. If you want to /// This derive does not support ignoring variants with `#[enumscribe(ignore)]`. If you want to
/// ignore variants, try deriving [`TryScribeCowStr`](derive.TryScribeCowStr.html) instead. /// ignore variants, try deriving [`TryScribeCowStr`](derive.TryScribeCowStr.html) instead.
#[cfg(feature = "std")]
#[proc_macro_derive(ScribeCowStr, attributes(enumscribe))] #[proc_macro_derive(ScribeCowStr, attributes(enumscribe))]
pub fn derive_scribe_cow_str(input: TokenStream) -> TokenStream { pub fn derive_scribe_cow_str(input: TokenStream) -> TokenStream {
gen_scribe_impl( gen_scribe_impl(
@ -530,6 +533,7 @@ pub fn derive_scribe_cow_str(input: TokenStream) -> TokenStream {
/// ///
/// If you do not want to use `#[enumscribe(other)]`, you should derive /// If you do not want to use `#[enumscribe(other)]`, you should derive
/// [`TryScribeStaticStr`](derive.TryScribeStaticStr.html) instead. /// [`TryScribeStaticStr`](derive.TryScribeStaticStr.html) instead.
#[cfg(feature = "std")]
#[proc_macro_derive(TryScribeCowStr, attributes(enumscribe))] #[proc_macro_derive(TryScribeCowStr, attributes(enumscribe))]
pub fn derive_try_scribe_cow_str(input: TokenStream) -> TokenStream { pub fn derive_try_scribe_cow_str(input: TokenStream) -> TokenStream {
gen_try_scribe_impl( gen_try_scribe_impl(
@ -625,10 +629,10 @@ pub fn derive_try_unscribe(input: TokenStream) -> TokenStream {
input, input,
quote! { ::enumscribe::TryUnscribe }, quote! { ::enumscribe::TryUnscribe },
quote! { try_unscribe }, quote! { try_unscribe },
quote! { ::std::option::Option<Self> }, quote! { ::core::option::Option<Self> },
|constructed_named_variant| quote! { ::std::option::Option::Some(#constructed_named_variant) }, |constructed_named_variant| quote! { ::core::option::Option::Some(#constructed_named_variant) },
|constructed_other_variant| quote! { ::std::option::Option::Some(#constructed_other_variant) }, |constructed_other_variant| quote! { ::core::option::Option::Some(#constructed_other_variant) },
|_| Ok(quote! { _ => ::std::option::Option::None }), |_| Ok(quote! { _ => ::core::option::Option::None }),
) )
} }
@ -693,7 +697,7 @@ pub fn derive_enum_serialize(input: TokenStream) -> TokenStream {
enum_ident enum_ident
); );
quote! { quote! {
_ => ::std::result::Result::Err( _ => ::core::result::Result::Err(
::serde::ser::Error::custom(#err_string) ::serde::ser::Error::custom(#err_string)
) )
} }
@ -704,7 +708,7 @@ pub fn derive_enum_serialize(input: TokenStream) -> TokenStream {
(quote! { (quote! {
#[automatically_derived] #[automatically_derived]
impl ::serde::Serialize for #enum_ident { impl ::serde::Serialize for #enum_ident {
fn serialize<S>(&self, #serializer_ident: S) -> ::std::result::Result<S::Ok, S::Error> fn serialize<S>(&self, #serializer_ident: S) -> ::core::result::Result<S::Ok, S::Error>
where S: ::serde::Serializer where S: ::serde::Serializer
{ {
match self { match self {
@ -744,7 +748,6 @@ pub fn derive_enum_deserialize(input: TokenStream) -> TokenStream {
let enum_ident = &input.ident; let enum_ident = &input.ident;
let deserializer_ident = quote! { __enumscribe_deserializer }; let deserializer_ident = quote! { __enumscribe_deserializer };
let deserialized_string_ident = quote! { __enumscribe_deserialized_string };
let deserialized_str_ident = quote! { __enumscribe_deserialized_str }; let deserialized_str_ident = quote! { __enumscribe_deserialized_str };
let variant_strings = parsed_enum let variant_strings = parsed_enum
@ -762,13 +765,13 @@ pub fn derive_enum_deserialize(input: TokenStream) -> TokenStream {
&parsed_enum, &parsed_enum,
&deserialized_str_ident, &deserialized_str_ident,
|constructed_named_variant| quote! { |constructed_named_variant| quote! {
::std::result::Result::Ok(#constructed_named_variant) ::core::result::Result::Ok(#constructed_named_variant)
}, },
|constructed_other_variant| quote! { |constructed_other_variant| quote! {
::std::result::Result::Ok(#constructed_other_variant) ::core::result::Result::Ok(#constructed_other_variant)
}, },
|_| Ok(quote! { |_| Ok(quote! {
__enumscribe_deserialize_base_case => ::std::result::Result::Err( __enumscribe_deserialize_base_case => ::core::result::Result::Err(
::serde::de::Error::unknown_variant( ::serde::de::Error::unknown_variant(
__enumscribe_deserialize_base_case, __enumscribe_deserialize_base_case,
&[#(#variant_strings),*] &[#(#variant_strings),*]
@ -780,12 +783,10 @@ pub fn derive_enum_deserialize(input: TokenStream) -> TokenStream {
(quote! { (quote! {
#[automatically_derived] #[automatically_derived]
impl<'de> ::serde::Deserialize<'de> for #enum_ident { impl<'de> ::serde::Deserialize<'de> for #enum_ident {
fn deserialize<D>(#deserializer_ident: D) -> ::std::result::Result<Self, D::Error> fn deserialize<D>(#deserializer_ident: D) -> ::core::result::Result<Self, D::Error>
where D: ::serde::Deserializer<'de> where D: ::serde::Deserializer<'de>
{ {
let #deserialized_string_ident = <::std::string::String as ::serde::Deserialize<'_>> let #deserialized_str_ident = <&str as ::serde::Deserialize<'_>>::deserialize(#deserializer_ident)?;
::deserialize(#deserializer_ident)?;
let #deserialized_str_ident = #deserialized_string_ident.as_str();
#main_match #main_match
} }
} }

Loading…
Cancel
Save