Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Option types #18

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions instruct-macros-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,55 @@ impl InstructMacroResult {
InstructMacroResult::Enum(enum_info) => enum_info.wrap_info(new_name),
}
}

pub fn override_description(self, new_description: String) -> InstructMacroResult {
match self {
InstructMacroResult::Struct(struct_info) => {
InstructMacroResult::Struct(struct_info.override_description(new_description))
}
InstructMacroResult::Enum(enum_info) => {
InstructMacroResult::Enum(enum_info.override_description(new_description))
}
}
}

pub fn set_optional(self, is_optional: bool) -> InstructMacroResult {
match self {
InstructMacroResult::Struct(struct_info) => {
InstructMacroResult::Struct(struct_info.set_optional(is_optional))
}
InstructMacroResult::Enum(enum_info) => {
InstructMacroResult::Enum(enum_info.set_optional(is_optional))
}
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct StructInfo {
pub name: String,
pub description: String,
pub parameters: Vec<Parameter>,
pub is_optional: bool,
}

impl StructInfo {
pub fn wrap_info(mut self, new_name: String) -> Parameter {
self.name = new_name;
Parameter::Struct(self)
}

pub fn override_description(mut self, new_description: String) -> StructInfo {
if new_description.len() > 0 {
self.description = new_description;
}
self
}

pub fn set_optional(mut self, is_optional: bool) -> StructInfo {
self.is_optional = is_optional;
self
}
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand All @@ -46,6 +81,7 @@ pub struct ParameterInfo {
pub name: String,
pub r#type: String,
pub comment: String,
pub is_optional: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand All @@ -54,18 +90,32 @@ pub struct EnumInfo {
pub r#enum: Vec<String>,
pub r#type: String,
pub description: String,
pub is_optional: bool,
}

impl EnumInfo {
pub fn wrap_info(mut self, new_name: String) -> Parameter {
self.title = new_name;
Parameter::Enum(self)
}

pub fn override_description(mut self, new_description: String) -> EnumInfo {
if new_description.len() > 0 {
self.description = new_description;
}
self
}

pub fn set_optional(mut self, is_optional: bool) -> EnumInfo {
self.is_optional = is_optional;
self
}
}

pub struct FieldInfo {
pub name: String,
pub description: String,
pub r#type: String,
pub is_complex: bool,
pub is_optional: bool,
}
76 changes: 71 additions & 5 deletions instruct-macros/src/helpers/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,20 @@ pub fn extract_parameter_information(fields: &syn::FieldsNamed) -> Vec<FieldInfo
let field_type = &field.ty;
let serialized_field_type = quote!(#field_type).to_string();

let serialized_field_type = if serialized_field_type.contains("Option <") {
serialized_field_type.replace(" ", "")
} else {
serialized_field_type
};

FieldInfo {
name: field_name
.as_ref()
.map_or_else(|| "Unnamed".to_string(), |ident| ident.to_string()),
description: field_comment,
r#type: serialized_field_type.clone(),
is_complex: is_complex_type(serialized_field_type.clone()),
is_optional: is_option_type(&serialized_field_type),
}
})
.collect()
Expand All @@ -50,40 +57,99 @@ pub fn is_complex_type(field_type: String) -> bool {
"u8", "u16", "u32", "u64", "u128", "usize",
];

let option_types: Vec<String> = simple_types
.iter()
.map(|&t| format!("Option<{}>", t))
.collect();

if simple_types.contains(&field_type.as_str()) {
return false;
}

if field_type.starts_with("Vec<") {
let inner_type = &field_type[4..field_type.len() - 1];
return simple_types.contains(&inner_type);
if option_types.contains(&field_type) {
return false;
}

true
}

fn is_option_type(field_type: &str) -> bool {
field_type.starts_with("Option<") && field_type.ends_with(">")
}

fn extract_nested_type(field_type: &str) -> String {
if is_option_type(field_type) {
field_type[7..field_type.len() - 1].to_string()
} else {
field_type.to_string()
}
}

pub fn extract_parameters(fields: &syn::FieldsNamed) -> Vec<proc_macro2::TokenStream> {
extract_parameter_information(fields)
.iter()
.map(|field| {
let field_name = &field.name;
let field_type = &field.r#type;
let field_comment = &field.description;
let is_option = is_option_type(field_type);

if !field.is_complex {
quote! {
parameters.push(Parameter::Field(ParameterInfo {
name: #field_name.to_string(),
r#type: #field_type.to_string(),
comment: #field_comment.to_string(),
is_optional: #is_option,
}));
}
} else if is_option_type(field_type) {
let field_type = extract_nested_type(field_type);
let field_type = Ident::new(&field_type, proc_macro2::Span::call_site()); // Convert string to an identifier

quote! {
parameters.push(#field_type::get_info().override_description(#field_comment.to_string()).set_optional(#is_option).wrap_info(#field_name.to_string()));
}
} else {
let field_type = Ident::new(&field.r#type, proc_macro2::Span::call_site()); // Convert string to an identifier
let field_type = Ident::new(&field_type, proc_macro2::Span::call_site()); // Convert string to an identifier

quote! {
parameters.push(#field_type::get_info().wrap_info(#field_name.to_string()));
parameters.push(#field_type::get_info().override_description(#field_comment.to_string()).set_optional(#is_option).wrap_info(#field_name.to_string()));
}
}
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_complex_type() {
// Simple types
let simple_types = vec![
"bool", "char", "f32", "f64", "i8", "i16", "i32", "i64", "i128", "isize", "str",
"String", "u8", "u16", "u32", "u64", "u128", "usize",
];

for simple_type in &simple_types {
assert_eq!(is_complex_type(simple_type.to_string()), false);
assert_eq!(is_complex_type(format!("Option<{}>", simple_type)), false);
}

// Complex types

assert_eq!(is_complex_type("Option<User>".to_string()), true);
}

#[test]
fn test_extract_nested_type() {
// Test cases for extract_nested_type function
let test_cases = vec![("Option<User>", "User")];

for (input, expected) in test_cases {
assert_eq!(extract_nested_type(input), expected);
}
}
}
2 changes: 2 additions & 0 deletions instruct-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn generate_instruct_macro_enum(input: &DeriveInput) -> proc_macro2::TokenStream
r#enum: vec![#(#enum_variants.to_string()),*],
r#type: stringify!(#name).to_string(),
description: #description.to_string(),
is_optional:false
})
};

Expand Down Expand Up @@ -121,6 +122,7 @@ fn generate_instruct_macro_struct(input: &DeriveInput) -> proc_macro2::TokenStre
name: stringify!(#name).to_string(),
description: #description.to_string(),
parameters,
is_optional:false
})
}

Expand Down
13 changes: 13 additions & 0 deletions instruct-macros/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ mod tests {
name: "field1".to_string(),
r#type: "String".to_string(),
comment: "This is a sample example that spans across three lines".to_string(),
is_optional: false,
}),
Parameter::Field(ParameterInfo {
name: "field2".to_string(),
r#type: "str".to_string(),
comment: "This is a test field".to_string(),
is_optional: false,
}),
],
is_optional: false,
};

let info_struct = match info {
Expand Down Expand Up @@ -117,11 +120,13 @@ mod tests {
name: "name".to_string(),
r#type: "String".to_string(),
comment: "".to_string(),
is_optional: false,
}),
Parameter::Field(ParameterInfo {
name: "age".to_string(),
r#type: "u8".to_string(),
comment: "".to_string(),
is_optional: false,
}),
Parameter::Struct(StructInfo {
name: "address".to_string(),
Expand All @@ -131,15 +136,19 @@ mod tests {
name: "street".to_string(),
r#type: "String".to_string(),
comment: "".to_string(),
is_optional: false,
}),
Parameter::Field(ParameterInfo {
name: "city".to_string(),
r#type: "String".to_string(),
comment: "".to_string(),
is_optional: false,
}),
],
is_optional: false,
}),
],
is_optional: false,
};

let info_struct = match info {
Expand Down Expand Up @@ -171,6 +180,7 @@ mod tests {
],
r#type: "Status".to_string(),
description: "".to_string(),
is_optional: false,
};

let info_enum = match info {
Expand Down Expand Up @@ -209,6 +219,7 @@ mod tests {
name: "name".to_string(),
r#type: "String".to_string(),
comment: "".to_string(),
is_optional: false,
}),
Parameter::Enum(EnumInfo {
title: "status".to_string(),
Expand All @@ -219,8 +230,10 @@ mod tests {
],
r#type: "Status".to_string(),
description: "This is an enum representing the status of a person".to_string(),
is_optional: false,
}),
],
is_optional: false,
};

let info_struct = match info {
Expand Down
Loading