Skip to content

Commit

Permalink
Merge pull request #30 from greyblake/format-ident
Browse files Browse the repository at this point in the history
Format ident
  • Loading branch information
greyblake committed Apr 15, 2023
2 parents 98aa30b + 8f539f4 commit cf263ac
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 75 deletions.
7 changes: 3 additions & 4 deletions nutype_macros/src/common/gen/error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::common::models::{ErrorTypeName, TypeName};

pub fn gen_error_type_name(type_name: &TypeName) -> ErrorTypeName {
let error_name_str = format!("{type_name}Error");
let ident = Ident::new(&error_name_str, Span::call_site());
let ident = format_ident!("{type_name}Error");
ErrorTypeName::new(ident)
}

Expand Down
13 changes: 7 additions & 6 deletions nutype_macros/src/common/gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ pub mod parse_error;
pub mod traits;

use super::models::{ErrorTypeName, ParseErrorTypeName, TypeName};
use proc_macro2::{Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use quote::{quote, ToTokens};
use crate::common::models::ModuleName;
use proc_macro2::{Punct, Spacing, TokenStream, TokenTree};
use quote::{format_ident, quote, ToTokens};
use syn::Visibility;

/// Inject an inner type into a closure, so compiler does not complain if the token stream matchers
Expand Down Expand Up @@ -47,15 +48,15 @@ fn is_ident(token: &TokenTree) -> bool {
matches!(token, TokenTree::Ident(_))
}

pub fn gen_module_name_for_type(type_name: &TypeName) -> Ident {
let module_name = format!("__nutype_private_{type_name}__");
Ident::new(&module_name, Span::call_site())
pub fn gen_module_name_for_type(type_name: &TypeName) -> ModuleName {
let ident = format_ident!("__nutype_private_{type_name}__");
ModuleName::new(ident)
}

pub fn gen_reimports(
vis: Visibility,
type_name: &TypeName,
module_name: &Ident,
module_name: &ModuleName,
maybe_error_type_name: Option<&ErrorTypeName>,
maybe_parse_error_type_name: Option<&ParseErrorTypeName>,
) -> TokenStream {
Expand Down
7 changes: 3 additions & 4 deletions nutype_macros/src/common/gen/parse_error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use proc_macro2::{Ident, Span, TokenStream};
use quote::quote;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use crate::common::models::{ErrorTypeName, InnerType, ParseErrorTypeName, TypeName};

/// Generate a name for the error which is used for FromStr trait implementation.
pub fn gen_parse_error_name(type_name: &TypeName) -> ParseErrorTypeName {
let error_name = format!("{type_name}ParseError");
let ident = Ident::new(&error_name, Span::call_site());
let ident = format_ident!("{type_name}ParseError");
ParseErrorTypeName::new(ident)
}

Expand Down
93 changes: 32 additions & 61 deletions nutype_macros/src/common/models.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::fmt::Debug;

use proc_macro2::{Ident, Span, TokenStream};
use proc_macro2::{Span, TokenStream};
use quote::{quote, ToTokens};
use syn::spanned::Spanned;
use syn::Attribute;
Expand Down Expand Up @@ -123,74 +123,45 @@ impl ToTokens for FloatInnerType {
}
}

/// A type that represents a newtype name.
/// For example: `Username`, `Email`, etc.
#[derive(Debug)]
pub struct TypeName(Ident);
macro_rules! define_ident_type {
($tp_name:ident) => {
#[derive(Debug)]
pub struct $tp_name(proc_macro2::Ident);

impl TypeName {
pub fn new(name: Ident) -> Self {
Self(name)
}
}

impl core::fmt::Display for TypeName {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}

impl ToTokens for TypeName {
fn to_tokens(&self, token_stream: &mut TokenStream) {
self.0.to_tokens(token_stream)
}
}

/// Repesents a type for a validation error.
/// For example, if `TypeName` is `Email`, then `ErrorTypeName` would usually be `EmailError`.
#[derive(Debug)]
pub struct ErrorTypeName(Ident);

impl ErrorTypeName {
pub fn new(name: Ident) -> Self {
Self(name)
}
}
impl $tp_name {
pub fn new(name: proc_macro2::Ident) -> Self {
Self(name)
}
}

impl core::fmt::Display for ErrorTypeName {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
impl core::fmt::Display for $tp_name {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}

impl ToTokens for ErrorTypeName {
fn to_tokens(&self, token_stream: &mut TokenStream) {
self.0.to_tokens(token_stream)
}
impl ::quote::ToTokens for $tp_name {
fn to_tokens(&self, token_stream: &mut TokenStream) {
self.0.to_tokens(token_stream)
}
}
};
}

/// A type that represents an error name which is returned by `FromStr` traits.
/// For example, if `TypeName` is `Amount`, then this would be `AmountParseError`.
#[derive(Debug)]
pub struct ParseErrorTypeName(Ident);
// A type that represents a newtype name.
// For example: `Username`, `Email`, etc.
define_ident_type!(TypeName);

impl ParseErrorTypeName {
pub fn new(name: Ident) -> Self {
Self(name)
}
}
// Repesents a type for a validation error.
// For example, if `TypeName` is `Email`, then `ErrorTypeName` would usually be `EmailError`.
define_ident_type!(ErrorTypeName);

impl core::fmt::Display for ParseErrorTypeName {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
write!(f, "{}", self.0)
}
}
// A type that represents an error name which is returned by `FromStr` traits.
// For example, if `TypeName` is `Amount`, then this would be `AmountParseError`.
define_ident_type!(ParseErrorTypeName);

impl ToTokens for ParseErrorTypeName {
fn to_tokens(&self, token_stream: &mut TokenStream) {
self.0.to_tokens(token_stream)
}
}
// Module name, where the type is placed.
define_ident_type!(ModuleName);

#[derive(Debug)]
pub struct NewtypeMeta {
Expand Down

0 comments on commit cf263ac

Please sign in to comment.