From f18c18cd15f180b5067069ec6f10530515715f3d Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 9 May 2026 12:15:21 +0900 Subject: refactor(console): accept format args directly in console_writeln! macros Eliminate the nested &console_format!(...) boilerplate at every call site by teaching console_writeln!, console_write!, console_writeln_error!, and console_write_error! to accept a format literal + variadic args directly, matching the println!/eprintln! ergonomics. Propagate the format string span into generated code so rustc errors point to the right location. Co-Authored-By: Claude Sonnet 4.6 --- crates/mozart/src/commands/show.rs | 363 ++++++++++++++++--------------------- 1 file changed, 155 insertions(+), 208 deletions(-) (limited to 'crates/mozart/src/commands/show.rs') diff --git a/crates/mozart/src/commands/show.rs b/crates/mozart/src/commands/show.rs index 8bd53cb..6a07fb0 100644 --- a/crates/mozart/src/commands/show.rs +++ b/crates/mozart/src/commands/show.rs @@ -117,9 +117,7 @@ pub async fn execute( if args.installed && !args.self_info { console_writeln_error!( console, - &console_format!( - "You are using the deprecated option \"installed\". Only installed packages are shown by default now. The --all option can be used to show all packages." - ), + "You are using the deprecated option \"installed\". Only installed packages are shown by default now. The --all option can be used to show all packages.", ); } @@ -173,9 +171,7 @@ pub async fn execute( if !args.ignore.is_empty() && !args.outdated { console_writeln_error!( console, - &console_format!( - "You are using the option \"ignore\" for action other than \"outdated\", it will be ignored." - ), + "You are using the option \"ignore\" for action other than \"outdated\", it will be ignored.", ); } @@ -571,7 +567,7 @@ fn render_package_list( console_writeln!( console, - &console_format!("Direct dependencies required in composer.json:"), + "Direct dependencies required in composer.json:", ); if direct_entries.is_empty() { console_writeln!(console, "Everything up to date"); @@ -582,7 +578,7 @@ fn render_package_list( console_writeln!(console, ""); console_writeln!( console, - &console_format!("Transitive dependencies not required in composer.json:"), + "Transitive dependencies not required in composer.json:", ); if transitive_entries.is_empty() { console_writeln!(console, "Everything up to date"); @@ -700,18 +696,21 @@ fn print_package_rows( }; console_writeln!( console, - &format!( - "{}{} {} {} {}", - ascii_prefix, name_str, version_str, latest_str, entry.description - ), + "{}{} {} {} {}", + ascii_prefix, + name_str, + version_str, + latest_str, + entry.description, ); } else { console_writeln!( console, - &format!( - "{}{} {} {}", - ascii_prefix, name_str, version_str, entry.description - ), + "{}{} {} {}", + ascii_prefix, + name_str, + version_str, + entry.description, ); } @@ -730,7 +729,7 @@ fn print_package_rows( entry.name, replacement ) }; - console_writeln_error!(console, &console_format!("{}", msg),); + console_writeln_error!(console, "{}", msg); } } } @@ -738,24 +737,21 @@ fn print_package_rows( /// Print the color legend before the list (A6, mirrors Composer 626-642). fn print_color_legend(console: &mozart_core::console::Console) { if console.decorated { - console_writeln!(console, &console_format!("Color legend:"),); + console_writeln!(console, "Color legend:"); console_writeln!( console, - &format!( - "- {} release available - update recommended", - console_format!("patch or minor") - ), + "- {} release available - update recommended", + console_format!("patch or minor"), ); console_writeln!( console, - &format!( - "- {} release available - update possible", - console_format!("major") - ), + "- {} release available - update possible", + console_format!("major"), ); console_writeln!( console, - &format!("- {} version", console_format!("up to date")), + "- {} version", + console_format!("up to date"), ); } else { console_writeln!(console, "Legend:"); @@ -801,7 +797,7 @@ fn render_list_json( .collect(); let output = serde_json::json!({ section_key: json_entries }); - console_writeln!(console, &serde_json::to_string_pretty(&output)?,); + console_writeln!(console, "{}", &serde_json::to_string_pretty(&output)?); Ok(()) } @@ -945,38 +941,36 @@ async fn print_package_detail( console_writeln!( console, - &format!("{} : {}", console_format!("name"), detail.name), + "{} : {}", + console_format!("name"), + detail.name, ); console_writeln!( console, - &format!( - "{} : {}", - console_format!("descrip."), - detail.description - ), + "{} : {}", + console_format!("descrip."), + detail.description, ); console_writeln!( console, - &format!( - "{} : {}", - console_format!("keywords"), - detail.keywords.join(", ") - ), + "{} : {}", + console_format!("keywords"), + detail.keywords.join(", "), ); console_writeln!( console, - &format!( - "{} : {}", - console_format!("versions"), - format_version_highlight(&detail.version) - ), + "{} : {}", + console_format!("versions"), + format_version_highlight(&detail.version), ); // A13: released if let Some(ref date) = detail.release_date { console_writeln!( console, - &format!("{} : {}", console_format!("released"), date), + "{} : {}", + console_format!("released"), + date, ); } @@ -1000,43 +994,35 @@ async fn print_package_detail( }; console_writeln!( console, - &format!( - "{} : {}", - console_format!("latest"), - latest_str - ), + "{} : {}", + console_format!("latest"), + latest_str, ); } } console_writeln!( console, - &format!( - "{} : {}", - console_format!("type"), - detail.package_type.as_deref().unwrap_or("library") - ), + "{} : {}", + console_format!("type"), + detail.package_type.as_deref().unwrap_or("library"), ); for license_id in &detail.licenses { console_writeln!( console, - &format!( - "{} : {}", - console_format!("license"), - format_license_for_show(license_id), - ), + "{} : {}", + console_format!("license"), + format_license_for_show(license_id), ); } if let Some(ref homepage) = detail.homepage { console_writeln!( console, - &format!( - "{} : {}", - console_format!("homepage"), - homepage - ), + "{} : {}", + console_format!("homepage"), + homepage, ); } @@ -1045,13 +1031,11 @@ async fn print_package_detail( let src_ref = detail.source_ref.as_deref().unwrap_or(""); console_writeln!( console, - &format!( - "{} : [{}] {} {}", - console_format!("source"), - src_type, - console_format!("{}", src_url), - src_ref - ), + "{} : [{}] {} {}", + console_format!("source"), + src_type, + console_format!("{}", src_url), + src_ref, ); } @@ -1060,20 +1044,20 @@ async fn print_package_detail( let dist_ref = detail.dist_ref.as_deref().unwrap_or(""); console_writeln!( console, - &format!( - "{} : [{}] {} {}", - console_format!("dist"), - dist_type, - console_format!("{}", dist_url), - dist_ref - ), + "{} : [{}] {} {}", + console_format!("dist"), + dist_type, + console_format!("{}", dist_url), + dist_ref, ); } if let Some(ref path) = detail.install_path { console_writeln!( console, - &format!("{} : {}", console_format!("path"), path), + "{} : {}", + console_format!("path"), + path, ); } @@ -1081,11 +1065,9 @@ async fn print_package_detail( if detail.names.len() > 1 { console_writeln!( console, - &format!( - "{} : {}", - console_format!("names"), - detail.names.join(", ") - ), + "{} : {}", + console_format!("names"), + detail.names.join(", "), ); } @@ -1095,12 +1077,14 @@ async fn print_package_detail( && !obj.is_empty() { console_writeln!(console, ""); - console_writeln!(console, &console_format!("support"),); + console_writeln!(console, "support"); for (key, val) in obj { let v = val.as_str().unwrap_or(""); console_writeln!( console, - &format!("{} {}", key, console_format!("{}", v)), + "{} {}", + key, + console_format!("{}", v), ); } } @@ -1108,7 +1092,7 @@ async fn print_package_detail( // A13: autoload if let Some(ref autoload) = detail.autoload { console_writeln!(console, ""); - console_writeln!(console, &console_format!("autoload"),); + console_writeln!(console, "autoload"); if let Some(obj) = autoload.as_object() { for (loader_type, config) in obj { match config { @@ -1117,12 +1101,10 @@ async fn print_package_detail( let v_str = v.as_str().unwrap_or(""); console_writeln!( console, - &format!( - "{}: {} => {}", - loader_type, - k, - console_format!("{}", v_str) - ), + "{}: {} => {}", + loader_type, + k, + console_format!("{}", v_str), ); } } @@ -1131,11 +1113,9 @@ async fn print_package_detail( let v_str = item.as_str().unwrap_or(""); console_writeln!( console, - &format!( - "{}: {}", - loader_type, - console_format!("{}", v_str) - ), + "{}: {}", + loader_type, + console_format!("{}", v_str), ); } } @@ -1166,15 +1146,13 @@ fn print_links_section( return; } console_writeln!(console, ""); - console_writeln!(console, &console_format!("{}", label),); + console_writeln!(console, "{}", label); for (name, constraint) in links { console_writeln!( console, - &format!( - "{} {}", - name, - console_format!("{}", constraint) - ), + "{} {}", + name, + console_format!("{}", constraint), ); } } @@ -1241,7 +1219,7 @@ async fn print_package_detail_json( } } - console_writeln!(console, &serde_json::to_string_pretty(&obj)?,); + console_writeln!(console, "{}", &serde_json::to_string_pretty(&obj)?); Ok(()) } @@ -1265,9 +1243,7 @@ async fn execute_installed( if !root.require.is_empty() || !root.require_dev.is_empty() { console_writeln_error!( console, - &console_format!( - "No dependencies installed. Try running mozart install or update." - ), + "No dependencies installed. Try running mozart install or update.", ); } } @@ -1287,7 +1263,7 @@ async fn execute_installed( Some(p) => { let install_path = vendor_dir.join(&p.name); let path_str = resolve_path(&install_path); - console_writeln!(console, &format!("{} {}", p.name, path_str),); + console_writeln!(console, "{} {}", p.name, path_str); } None => { anyhow::bail!( @@ -1333,7 +1309,7 @@ async fn execute_installed( for pkg in &packages { let install_path = vendor_dir.join(&pkg.name); let path_str = resolve_path(&install_path); - console_writeln!(console, &format!("{} {}", pkg.name, path_str),); + console_writeln!(console, "{} {}", pkg.name, path_str); } return Ok(()); } @@ -1342,7 +1318,7 @@ async fn execute_installed( let show_latest = args.latest || args.outdated; if args.name_only && !show_latest { for pkg in &packages { - console_writeln!(console, &pkg.name); + console_writeln!(console, "{}", &pkg.name); } return Ok(()); } @@ -1355,7 +1331,7 @@ async fn execute_installed( if args.name_only { for e in &entries { - console_writeln!(console, &e.name); + console_writeln!(console, "{}", &e.name); } return Ok(()); } @@ -1460,7 +1436,7 @@ async fn execute_locked( if args.path { console_writeln_error!( console, - &console_format!("--path is not supported with --locked"), + "--path is not supported with --locked", ); return Ok(()); } @@ -1469,7 +1445,7 @@ async fn execute_locked( let show_latest = args.latest || args.outdated; if args.name_only && !show_latest { for pkg in &packages { - console_writeln!(console, &pkg.name); + console_writeln!(console, "{}", &pkg.name); } return Ok(()); } @@ -1482,7 +1458,7 @@ async fn execute_locked( if args.name_only { for e in &entries { - console_writeln!(console, &e.name); + console_writeln!(console, "{}", &e.name); } return Ok(()); } @@ -1514,63 +1490,55 @@ fn show_self( let root = mozart_core::package::read_from_file(&composer_json_path)?; if args.name_only { - console_writeln!(console, &root.name); + console_writeln!(console, "{}", &root.name); return Ok(()); } console_writeln!( console, - &format!("{} : {}", console_format!("name"), root.name), + "{} : {}", + console_format!("name"), + root.name, ); console_writeln!( console, - &format!( - "{} : {}", - console_format!("descrip."), - root.description.as_deref().unwrap_or("") - ), + "{} : {}", + console_format!("descrip."), + root.description.as_deref().unwrap_or(""), ); console_writeln!( console, - &format!( - "{} : {}", - console_format!("type"), - root.package_type.as_deref().unwrap_or("project") - ), + "{} : {}", + console_format!("type"), + root.package_type.as_deref().unwrap_or("project"), ); if let Some(ref license) = root.license { console_writeln!( console, - &format!( - "{} : {}", - console_format!("license"), - format_license_for_show(license), - ), + "{} : {}", + console_format!("license"), + format_license_for_show(license), ); } if let Some(ref homepage) = root.homepage { console_writeln!( console, - &format!( - "{} : {}", - console_format!("homepage"), - homepage - ), + "{} : {}", + console_format!("homepage"), + homepage, ); } // Requires if !root.require.is_empty() { console_writeln!(console, ""); - console_writeln!(console, &console_format!("requires"),); + console_writeln!(console, "requires"); for (name, constraint) in &root.require { console_writeln!( console, - &format!( - "{} {}", - name, - console_format!("{}", constraint) - ), + "{} {}", + name, + console_format!("{}", constraint), ); } } @@ -1578,15 +1546,13 @@ fn show_self( // Requires (dev) if !root.require_dev.is_empty() { console_writeln!(console, ""); - console_writeln!(console, &console_format!("requires (dev)"),); + console_writeln!(console, "requires (dev)"); for (name, constraint) in &root.require_dev { console_writeln!( console, - &format!( - "{} {}", - name, - console_format!("{}", constraint) - ), + "{} {}", + name, + console_format!("{}", constraint), ); } } @@ -1643,11 +1609,9 @@ fn show_tree( console_writeln!( console, - &console_format!( - "{} {}", - &root.name, - root.description.as_deref().unwrap_or("") - ), + "{} {}", + &root.name, + root.description.as_deref().unwrap_or(""), ); let mut visited_global: IndexSet = IndexSet::new(); @@ -1693,19 +1657,19 @@ fn print_tree_node( console_writeln!( console, - &format!( - "{} {} {}", - prefix, - console_format!("{} {}", pkg_name, &version), - description - ), + "{} {} {}", + prefix, + console_format!("{} {}", pkg_name, &version), + description, ); if visited.contains(&key) || depth >= MAX_DEPTH { if visited.contains(&key) { console_writeln!( console, - &format!("{} {} (circular dependency)", child_prefix, pkg_name), + "{} {} (circular dependency)", + child_prefix, + pkg_name, ); } return; @@ -1753,12 +1717,10 @@ fn print_tree_node( if !is_platform_package(&key) { console_writeln!( console, - &format!( - "{} {} {} (not installed)", - prefix, - console_format!("{}", pkg_name), - constraint - ), + "{} {} {} (not installed)", + prefix, + console_format!("{}", pkg_name), + constraint, ); } } @@ -1829,6 +1791,7 @@ fn show_platform( .collect(); console_writeln!( console, + "{}", &serde_json::to_string_pretty(&serde_json::json!({ "platform": json_entries }))?, ); return Ok(()); @@ -1843,7 +1806,7 @@ fn show_platform( if args.name_only { for (name, _, _) in &platform_packages { - console_writeln!(console, name); + console_writeln!(console, "{}", name); } return Ok(()); } @@ -1862,14 +1825,12 @@ fn show_platform( for (name, version, _source) in &platform_packages { console_writeln!( console, - &format!( - "{} {}", - console_format!("{:", name, width = name_width), - console_format!( - "{:", - version, - width = version_width - ), + "{} {}", + console_format!("{:", name, width = name_width), + console_format!( + "{:", + version, + width = version_width ), ); } @@ -1902,9 +1863,7 @@ async fn show_available( let lock = mozart_registry::lockfile::LockFile::read_from_file(&lock_path)?; console_writeln!( console, - &console_format!( - "Available versions for locked packages (from Packagist):" - ), + "Available versions for locked packages (from Packagist):", ); console_writeln!(console, ""); @@ -1927,9 +1886,7 @@ async fn show_available( console_writeln_error!( console, - &console_format!( - "No dependencies installed. Try running mozart install or update." - ), + "No dependencies installed. Try running mozart install or update.", ); return Ok(()); } @@ -1937,9 +1894,7 @@ async fn show_available( console_writeln!( console, - &console_format!( - "Available versions for installed packages (from Packagist):" - ), + "Available versions for installed packages (from Packagist):", ); console_writeln!(console, ""); @@ -1971,7 +1926,7 @@ async fn show_available( } } let output = serde_json::json!({ "packages": json_entries }); - console_writeln!(console, &serde_json::to_string_pretty(&output)?,); + console_writeln!(console, "{}", &serde_json::to_string_pretty(&output)?); return Ok(()); } @@ -1993,7 +1948,7 @@ async fn show_available_versions( ) -> anyhow::Result<()> { let versions = mozart_registry::packagist::fetch_package_versions(pkg_name, repo_cache).await?; if versions.is_empty() { - console_writeln!(console, &format!("No versions found for {pkg_name}"),); + console_writeln!(console, "No versions found for {pkg_name}"); return Ok(()); } @@ -2004,18 +1959,16 @@ async fn show_available_versions( "name": pkg_name, "versions": version_strings, }); - console_writeln!(console, &serde_json::to_string_pretty(&output)?,); + console_writeln!(console, "{}", &serde_json::to_string_pretty(&output)?); return Ok(()); } - console_writeln!( - console, - &console_format!("Available versions for {pkg_name}:"), - ); + console_writeln!(console, "Available versions for {pkg_name}:"); for v in &versions { console_writeln!( console, - &format!(" {}", console_format!("{}", &v.version)), + " {}", + console_format!("{}", &v.version), ); } Ok(()) @@ -2031,10 +1984,8 @@ async fn show_available_versions_inline( if versions.is_empty() { console_writeln!( console, - &format!( - "{}: no versions found", - console_format!("{}", pkg_name) - ), + "{}: no versions found", + console_format!("{}", pkg_name), ); return; } @@ -2050,21 +2001,17 @@ async fn show_available_versions_inline( }; console_writeln!( console, - &format!( - "{}: {}{}", - console_format!("{}", pkg_name), - console_format!("{}", &shown.join(", ")), - rest - ), + "{}: {}{}", + console_format!("{}", pkg_name), + console_format!("{}", &shown.join(", ")), + rest, ); } Err(_) => { console_writeln!( console, - &format!( - "{}: (could not fetch from Packagist)", - console_format!("{}", pkg_name) - ), + "{}: (could not fetch from Packagist)", + console_format!("{}", pkg_name), ); } } -- cgit v1.3.1