Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Debugging default value
Browse files Browse the repository at this point in the history
  • Loading branch information
amigin committed Aug 3, 2023
1 parent 0a2d131 commit b7af113
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/db_enum/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub fn generate(

let fn_is_none = super::utils::render_fn_is_none();

let default_value = super::utils::get_default_value(enum_name);
let default_value = super::utils::get_default_value(enum_name, enum_cases.as_slice())?;

let result = quote! {

Expand Down
2 changes: 1 addition & 1 deletion src/db_enum/generate_as_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn generate_as_string(ast: &syn::DeriveInput) -> Result<proc_macro::TokenStr

let fn_is_none = super::utils::render_fn_is_none();

let default_value = super::utils::get_default_value(enum_name);
let default_value = super::utils::get_default_value(enum_name, enum_cases.as_slice())?;

let result = quote! {

Expand Down
24 changes: 20 additions & 4 deletions src/db_enum/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::str::FromStr;

use proc_macro2::{Ident, TokenStream};
use types_reader::EnumCase;

use crate::postgres_enum_ext::PostgresEnumExt;

pub fn render_reading_db_row_metadata_model() -> proc_macro2::TokenStream {
quote::quote! {
Expand Down Expand Up @@ -48,15 +51,28 @@ pub fn render_fn_is_none() -> proc_macro2::TokenStream {
}
}

pub fn get_default_value(enum_name: &Ident) -> proc_macro2::TokenStream {
pub fn get_default_value(
enum_name: &Ident,
enum_cases: &[EnumCase],
) -> Result<proc_macro2::TokenStream, syn::Error> {
for enum_case in enum_cases {
if enum_case.attrs.has_attr("default_value") {
let value = enum_case.get_case_string_value()?;

return Ok(quote::quote!(#value));
}
}

let enum_name = enum_name.to_string();

TokenStream::from_str(
let result = TokenStream::from_str(
format!(
r#"panic!("Default value is not specified for the enum [{}]");"#,
r#"panic!("Default value is not specified for the enum '{}'");"#,
enum_name
)
.as_str(),
)
.unwrap()
.unwrap();

Ok(result)
}

0 comments on commit b7af113

Please sign in to comment.