Skip to content

Commit

Permalink
docs: Update docs
Browse files Browse the repository at this point in the history
- Add description of the project in the README.md, with usage example
-  Add lib.rs documentation
  • Loading branch information
Yag000 committed Sep 13, 2023
1 parent 3016c19 commit f1f4df8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
# enum-stringify

Future set of macros to generate to string functions for enums, similar to [string_enum](https://crates.io/crates/string-enum) but with a different approach.
Set of macros (only one for now) to generate a string representation of an enum. When using
`#[derive(EnumStringify)]` on an enum, it will implement `std::fmt::Display`, `TryFrom<&str>`,
`TryFrom<String>` and `std::str::FromStr` for it. It will use the name of the enum variant as the
string representation.

## Usage

```rust
use enum_stringify::EnumStringify;

#[derive(EnumStringify)]
enum MyEnum {
Variant1,
Variant2,
Variant3,
}

fn main() {
println!("{}", MyEnum::Variant1); // Prints "Variant1"
assert_eq!(MyEnum::Variant1.to_string(), "Variant1");
assert_eq!(MyEnum::try_from("Variant2").unwrap(), MyEnum::Variant2);
assert_eq!(MyEnum::try_from("Variant3".to_string()).unwrap(), MyEnum::Variant3);
assert_eq!(MyEnum::from_str("Variant1").unwrap(), MyEnum::Variant1);
}
```

## Documentation and installation

See [docs.rs](https://docs.rs/enum-stringify) for documentation.
It is available on [crates.io](https://crates.io/crates/enum-stringify) as well.
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! # enum-stringify
//!
//! Derive [`std::fmt::Display`], [`std::str::FromStr`], [`TryFrom<&str>`] and
//! [`TryFrom<String>`] with a simple derive macro: [`EnumStringify`].

use std::{
fmt::{Display, Formatter},
str::FromStr,
Expand Down Expand Up @@ -80,8 +85,6 @@ use quote::quote;
/// }
/// }
/// ```
///
///
#[proc_macro_derive(EnumStringify)]
pub fn enum_stringify(input: TokenStream) -> TokenStream {
let ast = syn::parse(input).unwrap();
Expand All @@ -105,6 +108,7 @@ fn impl_enum_to_string(ast: &syn::DeriveInput) -> TokenStream {
gen
}

/// Implementation of [`std::fmt::Display`].
fn impl_display(name: &syn::Ident, names: &Vec<&syn::Ident>) -> TokenStream {
let gen = quote! {
impl std::fmt::Display for #name {
Expand All @@ -119,6 +123,7 @@ fn impl_display(name: &syn::Ident, names: &Vec<&syn::Ident>) -> TokenStream {
gen.into()
}

/// Implementation of [`TryFrom<&str>`].
fn impl_from_str(name: &syn::Ident, names: &Vec<&syn::Ident>) -> TokenStream {
let gen = quote! {
impl TryFrom<&str> for #name {
Expand All @@ -136,6 +141,7 @@ fn impl_from_str(name: &syn::Ident, names: &Vec<&syn::Ident>) -> TokenStream {
gen.into()
}

/// Implementation of [`TryFrom<String>`].
fn impl_from_string(name: &syn::Ident) -> TokenStream {
let gen = quote! {
impl TryFrom<String> for #name {
Expand All @@ -150,6 +156,7 @@ fn impl_from_string(name: &syn::Ident) -> TokenStream {
gen.into()
}

/// Implementation of [`std::str::FromStr`].
fn impl_from_str_trait(name: &syn::Ident) -> TokenStream {
let gen = quote! {
impl std::str::FromStr for #name {
Expand Down

0 comments on commit f1f4df8

Please sign in to comment.