Skip to content

Commit

Permalink
Use ColumnFromStrErr
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Aug 22, 2021
1 parent 332ab3c commit b8b699d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
11 changes: 3 additions & 8 deletions sea-orm-macros/src/derives/column.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use heck::{MixedCase, SnakeCase};
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use quote::{quote, quote_spanned};
use syn::{Data, DataEnum, Fields, Variant};

pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream> {
Expand Down Expand Up @@ -42,8 +42,6 @@ pub fn impl_default_as_str(ident: &Ident, data: &Data) -> syn::Result<TokenStrea
}

pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream> {
let parse_error_iden = format_ident!("Parse{}Err", ident);

let data_enum = match data {
Data::Enum(data_enum) => data_enum,
_ => {
Expand All @@ -63,16 +61,13 @@ pub fn impl_col_from_str(ident: &Ident, data: &Data) -> syn::Result<TokenStream>
});

Ok(quote!(
#[derive(Debug, Clone, Copy)]
pub struct #parse_error_iden;

impl std::str::FromStr for #ident {
type Err = #parse_error_iden;
type Err = sea_orm::ColumnFromStrErr;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
#(#columns),*,
_ => Err(#parse_error_iden),
_ => Err(sea_orm::ColumnFromStrErr(format!("Failed to parse '{}' into `{}`", s, stringify!(#ident)))),
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ impl std::fmt::Display for DbErr {
}
}
}

#[derive(Debug, Clone)]
pub struct ColumnFromStrErr(pub String);

impl std::error::Error for ColumnFromStrErr {}

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

0 comments on commit b8b699d

Please sign in to comment.