From 16d702c455472c103556eb4d6d713af05393b8cc Mon Sep 17 00:00:00 2001 From: Collins Muriuki Date: Fri, 8 Nov 2024 14:34:00 +0300 Subject: [PATCH] fix: Fix misplaced doc comments (#411) * Fix misplaced doc comments * Slightly refactor link_type integrity codegen --- src/file_tree.rs | 4 +- src/scaffold/link_type/integrity.rs | 287 ++++++++++++---------------- src/scaffold/zome.rs | 2 +- src/scaffold/zome/coordinator.rs | 2 +- src/utils.rs | 20 +- 5 files changed, 135 insertions(+), 180 deletions(-) diff --git a/src/file_tree.rs b/src/file_tree.rs index f220c8a1..21dbff58 100644 --- a/src/file_tree.rs +++ b/src/file_tree.rs @@ -82,8 +82,6 @@ pub fn insert_file( let mut folder_path = file_path.to_path_buf(); folder_path.pop(); - let content = convert_rust_line_to_doc_comments(file_path, content); - insert_file_tree_in_dir( file_tree, &folder_path, @@ -232,7 +230,7 @@ pub fn map_rust_files ScaffoldResult + C /// otherwise returns the original content unchanged. fn convert_rust_line_to_doc_comments(file_path: &Path, content: &str) -> String { if file_path.extension().and_then(|ext| ext.to_str()) == Some("rs") { - let re = Regex::new(r"^\/\/[^\/]|[^\/]\/\/[^\/]").expect("Failed to create regex"); + let re = Regex::new(r"(?:^|[^:])/(/[^/])").expect("Failed to create regex"); content .lines() .map(|line| re.replace_all(line, "/// ").into_owned() + "\n") diff --git a/src/scaffold/link_type/integrity.rs b/src/scaffold/link_type/integrity.rs index d9d86cc9..1118e0fe 100644 --- a/src/scaffold/link_type/integrity.rs +++ b/src/scaffold/link_type/integrity.rs @@ -45,14 +45,12 @@ pub fn add_link_type_to_integrity_zome( &|_path, file| { file.items.clone().into_iter().find(|i| { if let syn::Item::Enum(item_enum) = i.clone() { - if item_enum.attrs.iter().any(|a| { + return item_enum.attrs.iter().any(|a| { a.path() .segments .iter() .any(|s| s.ident == "hdk_link_types") - }) { - return true; - } + }); } false }) @@ -101,27 +99,24 @@ pub fn add_link_type_to_integrity_zome( for item in &mut file.items { if let syn::Item::Fn(item_fn) = item { - if item_fn.sig.ident.to_string().eq(&String::from("validate")) { - for stmt in &mut item_fn.block.stmts { - if let syn::Stmt::Expr(syn::Expr::Match(match_expr), _) = stmt { - if let syn::Expr::Try(try_expr) = &mut *match_expr.expr { - if let syn::Expr::MethodCall(call) = &mut *try_expr.expr { - if call - .method - .to_string() - .eq(&String::from("flattened")) - { - if let Some(turbofish) = &mut call.turbofish { - if let Some(last_arg) = - turbofish.args.last_mut() - { - *last_arg = syn::GenericArgument::Type( - syn::parse_str::( - "LinkTypes", - )?, - ); - } - } + if item_fn.sig.ident != "validate" { + continue; + } + for stmt in &mut item_fn.block.stmts { + if let syn::Stmt::Expr(syn::Expr::Match(match_expr), _) = stmt { + if let syn::Expr::Try(try_expr) = &mut *match_expr.expr { + if let syn::Expr::MethodCall(call) = &mut *try_expr.expr { + if call.method != "flattened" { + continue; + } + if let Some(turbofish) = &mut call.turbofish { + if let Some(last_arg) = turbofish.args.last_mut() { + *last_arg = + syn::GenericArgument::Type(syn::parse_str::< + syn::Type, + >( + "LinkTypes" + )?); } } } @@ -141,17 +136,17 @@ pub fn add_link_type_to_integrity_zome( a.path() .segments .iter() - .any(|s| s.ident.eq("hdk_link_types")) + .any(|s| s.ident == "hdk_link_types") }) { if item_enum .variants .iter() - .any(|v| v.ident.to_string().eq(&pascal_case_link_type_name)) + .any(|v| v.ident == pascal_case_link_type_name) { return Err(ScaffoldError::LinkTypeAlreadyExists( link_type_name.to_owned(), dna_manifest.name(), - zome_manifest.name.0.to_string(), + zome_manifest.name.to_string(), )); } @@ -184,7 +179,7 @@ pub fn add_link_type_to_integrity_zome( .path_mut(&mut v.iter()) .ok_or(ScaffoldError::PathNotFound(crate_src_path.clone()))?, |file_path, mut file| { - if file_path.eq(file_to_add_validation_to) { + if file_path == file_to_add_validation_to { let validate_create_fn = format_ident!( "validate_create_link_{}", link_type_name.to_case(Case::Snake) @@ -202,7 +197,7 @@ pub fn add_link_type_to_integrity_zome( let validate_delete_result: TokenStream = if delete { quote! { - // TODO: add the appropriate validation rules + /// TODO: add the appropriate validation rules Ok(ValidateCallbackResult::Valid) } } else { @@ -240,7 +235,7 @@ pub fn add_link_type_to_integrity_zome( #validate_create_to - // TODO: add the appropriate validation rules + /// TODO: add the appropriate validation rules Ok(ValidateCallbackResult::Valid) } }; @@ -305,7 +300,7 @@ fn validate_referenceable( if entry_type.reference_entry_hash { quote! { - /// Check the entry type for the given entry hash + // Check the entry type for the given entry hash let entry_hash = #address_ident.into_entry_hash().ok_or(wasm_error!(WasmErrorInner::Guest("No entry hash associated with link".to_string())))?; let entry = must_get_entry(entry_hash)?.content; @@ -313,7 +308,7 @@ fn validate_referenceable( } } else { quote! { - /// Check the entry type for the given action hash + // Check the entry type for the given action hash let action_hash = #address_ident.into_action_hash().ok_or(wasm_error!( WasmErrorInner::Guest("No action hash associated with link".to_string()) ))?; @@ -356,12 +351,7 @@ fn add_link_type_signals( } if let syn::Item::Fn(item_fn) = item { - if item_fn - .sig - .ident - .to_string() - .eq(&String::from("signal_action")) - { + if item_fn.sig.ident == "signal_action" { if find_ending_match_expr_in_block(&mut item_fn.block).is_none() { item_fn.block = Box::new(syn::parse_str::( "{ match action.hashed.content.clone() { _ => Ok(()) } }", @@ -391,20 +381,14 @@ fn signal_has_link_types(signal_enum: &syn::ItemEnum) -> bool { signal_enum .variants .iter() - .any(|v| v.ident.to_string().eq(&String::from("LinkCreated"))) + .any(|v| v.ident == "LinkCreated") } fn signal_action_has_link_types(expr_match: &syn::ExprMatch) -> bool { expr_match.arms.iter().any(|arm| { if let syn::Pat::TupleStruct(tuple_struct_pat) = &arm.pat { if let Some(first_segment) = tuple_struct_pat.path.segments.last() { - if first_segment - .ident - .to_string() - .eq(&String::from("CreateLink")) - { - return true; - } + return first_segment.ident == "CreateLink"; } } false @@ -471,9 +455,7 @@ fn signal_action_match_arms() -> ScaffoldResult> { fn is_create_link(pat: &syn::Pat) -> bool { if let syn::Pat::Struct(pat_struct) = pat { if let Some(ps) = pat_struct.path.segments.last() { - if ps.ident.to_string().eq(&String::from("CreateLink")) { - return true; - } + return ps.ident == "CreateLink"; } } false @@ -482,9 +464,7 @@ fn is_create_link(pat: &syn::Pat) -> bool { fn is_delete_link(pat: &syn::Pat) -> bool { if let syn::Pat::Struct(pat_struct) = pat { if let Some(ps) = pat_struct.path.segments.last() { - if ps.ident.to_string().eq(&String::from("DeleteLink")) { - return true; - } + return ps.ident == "DeleteLink"; } } false @@ -495,66 +475,61 @@ fn add_link_type_to_validation_arms( link_type_name: &str, ) -> ScaffoldResult<()> { if let syn::Item::Fn(item_fn) = item { - if item_fn.sig.ident.to_string().eq(&String::from("validate")) { - for stmt in &mut item_fn.block.stmts { - if let syn::Stmt::Expr(syn::Expr::Match(match_expr), _) = stmt { - if let syn::Expr::Try(try_expr) = &mut *match_expr.expr { - if let syn::Expr::MethodCall(call) = &mut *try_expr.expr { - if call.method.to_string().eq(&String::from("flattened")) { - for arm in &mut match_expr.arms { - if let syn::Pat::TupleStruct(tuple_struct) = &mut arm.pat { - if let Some(path_segment) = - tuple_struct.path.segments.last() - { - let path_segment_str = path_segment.ident.to_string(); - if path_segment_str.eq(&String::from("StoreRecord")) { - if let Some(op_entry_match_expr) = - find_ending_match_expr(&mut arm.body) + if item_fn.sig.ident != "validate" { + return Ok(()); + } + for stmt in &mut item_fn.block.stmts { + if let syn::Stmt::Expr(syn::Expr::Match(match_expr), _) = stmt { + if let syn::Expr::Try(try_expr) = &mut *match_expr.expr { + if let syn::Expr::MethodCall(call) = &mut *try_expr.expr { + if call.method != "flattened" { + continue; + } + for arm in &mut match_expr.arms { + if let syn::Pat::TupleStruct(tuple_struct) = &mut arm.pat { + if let Some(path_segment) = tuple_struct.path.segments.last() { + if path_segment.ident != "StoreRecord" { + continue; + } + if let Some(op_entry_match_expr) = + find_ending_match_expr(&mut arm.body) + { + for op_record_arm in &mut op_entry_match_expr.arms { + if is_create_link(&op_record_arm.pat) { + // Add new link type to match arm + if find_ending_match_expr(&mut op_record_arm.body) + .is_none() { - for op_record_arm in - &mut op_entry_match_expr.arms - { - if is_create_link(&op_record_arm.pat) { - // Add new link type to match arm - if find_ending_match_expr( - &mut op_record_arm.body, - ) - .is_none() - { - // Change empty invalid to match on link_type - *op_record_arm.body = - syn::parse_str::( - "match link_type {}", - )?; - } - - // Add new link type to match arm - if let Some(link_type_match) = - find_ending_match_expr( - &mut op_record_arm.body, - ) - { - let new_arm: syn::Arm = syn::parse_str( + // Change empty invalid to match on link_type + *op_record_arm.body = + syn::parse_str::( + "match link_type {}", + )?; + } + + // Add new link type to match arm + if let Some(link_type_match) = + find_ending_match_expr(&mut op_record_arm.body) + { + let new_arm: syn::Arm = syn::parse_str( format!( "LinkTypes::{} => validate_create_link_{}(action, base_address, target_address, tag),", link_type_name.to_case(Case::Pascal), link_type_name.to_case(Case::Snake) ).as_str() )?; - link_type_match.arms.push(new_arm); - } - } else if is_delete_link(&op_record_arm.pat) - { - // Add new link type to match arm - if find_ending_match_expr( - &mut op_record_arm.body, - ) - .is_none() - { - // Change empty invalid to match on link_type - *op_record_arm.body = - syn::parse_str::( - r#"{ + link_type_match.arms.push(new_arm); + } + } else if is_delete_link(&op_record_arm.pat) { + // Add new link type to match arm + if find_ending_match_expr(&mut op_record_arm.body) + .is_none() + { + // Change empty invalid to match on link_type + *op_record_arm.body = syn::parse_str::< + syn::Expr, + >( + r#"{ let record = must_get_valid_record(original_action_hash)?; let create_link = match record.action() { Action::CreateLink(create_link) => create_link.clone(), @@ -570,16 +545,14 @@ fn add_link_type_to_validation_arms( }; match link_type {} }"#, - )?; - } - - // Add new entry type to match arm - if let Some(link_type_match) = - find_ending_match_expr( - &mut op_record_arm.body, - ) - { - let new_arm: syn::Arm = + )?; + } + + // Add new entry type to match arm + if let Some(link_type_match) = + find_ending_match_expr(&mut op_record_arm.body) + { + let new_arm: syn::Arm = syn::parse_str( format!( "LinkTypes::{} => validate_delete_link_{}(action, create_link.clone(), base_address, create_link.target_address, create_link.tag),", @@ -587,69 +560,57 @@ fn add_link_type_to_validation_arms( link_type_name.to_case(Case::Snake), ).as_str() )?; - link_type_match.arms.push(new_arm); - } - } - } + link_type_match.arms.push(new_arm); } } } } - if let syn::Pat::Struct(pat_struct) = &mut arm.pat { - if let Some(path_segment) = pat_struct.path.segments.last() - { - let path_segment_str = path_segment.ident.to_string(); - - if path_segment_str - .eq(&String::from("RegisterCreateLink")) - { - // Add new link type to match arm - if find_ending_match_expr(&mut arm.body).is_none() { - // Change empty invalid to match on link_type - *arm.body = syn::parse_str::( - "match link_type {}", - )?; - } + } + } + if let syn::Pat::Struct(pat_struct) = &mut arm.pat { + if let Some(path_segment) = pat_struct.path.segments.last() { + if path_segment.ident == "RegisterCreateLink" { + // Add new link type to match arm + if find_ending_match_expr(&mut arm.body).is_none() { + // Change empty invalid to match on link_type + *arm.body = + syn::parse_str::("match link_type {}")?; + } - // Add new link type to match arm - if let Some(link_type_match) = - find_ending_match_expr(&mut arm.body) - { - let new_arm: syn::Arm = syn::parse_str( - format!( + // Add new link type to match arm + if let Some(link_type_match) = + find_ending_match_expr(&mut arm.body) + { + let new_arm: syn::Arm = syn::parse_str( + format!( "LinkTypes::{} => validate_create_link_{}(action, base_address, target_address, tag),", link_type_name.to_case(Case::Pascal), link_type_name.to_case(Case::Snake) ) - .as_str(), - )?; - link_type_match.arms.push(new_arm); - } - } else if path_segment_str - .eq(&String::from("RegisterDeleteLink")) - { - // Add new link type to match arm - if find_ending_match_expr(&mut arm.body).is_none() { - // Change empty invalid to match on link_type - *arm.body = syn::parse_str::( - "match link_type {}", - )?; - } + .as_str(), + )?; + link_type_match.arms.push(new_arm); + } + } else if path_segment.ident == "RegisterDeleteLink" { + // Add new link type to match arm + if find_ending_match_expr(&mut arm.body).is_none() { + // Change empty invalid to match on link_type + *arm.body = + syn::parse_str::("match link_type {}")?; + } - // Add new link type to match arm - if let Some(link_type_match) = - find_ending_match_expr(&mut arm.body) - { - let new_arm: syn::Arm = syn::parse_str( + // Add new link type to match arm + if let Some(link_type_match) = + find_ending_match_expr(&mut arm.body) + { + let new_arm: syn::Arm = syn::parse_str( format!( "LinkTypes::{} => validate_delete_link_{}(action, original_action, base_address, target_address, tag),", link_type_name.to_case(Case::Pascal), link_type_name.to_case(Case::Snake) ).as_str() )?; - link_type_match.arms.push(new_arm); - } - } + link_type_match.arms.push(new_arm); } } } diff --git a/src/scaffold/zome.rs b/src/scaffold/zome.rs index 57d5cd8a..1a102391 100644 --- a/src/scaffold/zome.rs +++ b/src/scaffold/zome.rs @@ -454,7 +454,7 @@ pub fn scaffold_coordinator_zome_in_path( let file_tree = add_common_zome_dependencies_to_workspace_cargo(dna_file_tree.file_tree())?; let mut file_tree = add_workspace_path_dependency(file_tree, zome_name, &path.join(zome_name))?; - let initial_lib_rs = &coordinator::initial_lib_rs(dependencies); + let initial_lib_rs = coordinator::initial_lib_rs(dependencies); let zome: FileTree = dir! { "Cargo.toml" => file!(coordinator::initial_cargo_toml(zome_name, dependencies)), diff --git a/src/scaffold/zome/coordinator.rs b/src/scaffold/zome/coordinator.rs index 682f4011..ce5dba52 100644 --- a/src/scaffold/zome/coordinator.rs +++ b/src/scaffold/zome/coordinator.rs @@ -74,7 +74,7 @@ pub fn initial_lib_rs(dependencies: Option<&Vec>) -> TokenStream { /// Whenever an action is committed, we emit a signal to the UI elements to reactively update them #[hdk_extern(infallible)] pub fn post_commit(committed_actions: Vec) { - /// Don't modify this loop if you want the scaffolding tool to generate appropriate signals for your entries and links + /// Don't modify the for loop if you want the scaffolding tool to generate appropriate signals for your entries and links for action in committed_actions { if let Err(err) = signal_action(action) { error!("Error signaling new action: {:?}", err); diff --git a/src/utils.rs b/src/utils.rs index 78909e90..6559b6d2 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -76,20 +76,16 @@ fn get_folder_names(folder: &BTreeMap) -> Vec { #[inline] /// "yes" or "no" input dialog, with the option to specify a recommended answer (yes = true, no = false) pub fn input_yes_or_no(prompt: &str, recommended: Option) -> ScaffoldResult { - let yes_recommended = if recommended == Some(true) { - " (recommended)" - } else { - "" - }; - let no_recommended = if recommended == Some(false) { - " (recommended)" - } else { - "" - }; + let yes_recommended = (recommended == Some(true)) + .then_some("(recommended)") + .unwrap_or_default(); + let no_recommended = (recommended == Some(false)) + .then_some("(recommended)") + .unwrap_or_default(); let items = [ - format!("Yes{}", yes_recommended), - format!("No{}", no_recommended), + format!("Yes {}", yes_recommended), + format!("No {}", no_recommended), ]; let selection = Select::with_theme(&ColorfulTheme::default())