From faabce806f4831090f765a2b86971c7372798936 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Mon, 9 Nov 2026 11:37:43 +0800 Subject: [PATCH 1/3] comline fix: apply the language's fixes, and move a package to another edition core#102. `comline fix` applies the fixes core attaches to its edition diagnostics: a config.idp without specification_version gets the line, and each use of a word the package's edition deprecates or removes is replaced when it has a replacement (others are listed to change by hand). `--edition N` checks and fixes the schemas under edition N and moves config.idp to it, unless a removed word without a replacement is left. `--dry-run` lists the fixes without writing. The edition-change line in build and diff now says it bumps nothing by itself, matching core. --- Cargo.lock | 2 - Cargo.toml | 2 +- src/changes.rs | 2 +- src/cli.rs | 26 +++++ src/commands/fix.rs | 234 ++++++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + src/main.rs | 1 + 7 files changed, 264 insertions(+), 4 deletions(-) create mode 100644 src/commands/fix.rs diff --git a/Cargo.lock b/Cargo.lock index 7cbb4b7..cc08ee3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -599,7 +599,6 @@ dependencies = [ [[package]] name = "comline-core" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=d11d82b95c5dfa0c72476eb0947772533b7ce7e5#d11d82b95c5dfa0c72476eb0947772533b7ce7e5" dependencies = [ "ariadne", "blake3", @@ -628,7 +627,6 @@ dependencies = [ [[package]] name = "comline-core-stdlib" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=d11d82b95c5dfa0c72476eb0947772533b7ce7e5#d11d82b95c5dfa0c72476eb0947772533b7ce7e5" [[package]] name = "comline-doc-html" diff --git a/Cargo.toml b/Cargo.toml index 0556590..c9b95a5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,7 @@ eyre = "0.6.8" # needs real I/O (git fetch, filesystem), so it's off by default for # `comline-core`'s wasm32 consumers (the playground editor, the language # server); the CLI is the one consumer that actually runs `comline build`. -comline-core = { git = "https://github.com/ComlineProject/core", rev = "d11d82b95c5dfa0c72476eb0947772533b7ce7e5", features = ["deps"] } +comline-core = { git = "https://github.com/ComlineProject/core", rev = "b933209052b06b328ee3da3355665faa0787063d", features = ["deps"] } # All three pinned to revs that agree on one `comline-codegen`, and bumped # together: comline-rust and comline-typescript's codegen crates consume # `comline-codegen`'s types, so the rev below must be byte-identical to the diff --git a/src/changes.rs b/src/changes.rs index e46a309..d606516 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -56,7 +56,7 @@ fn config_line(change: &ConfigChange) -> String { ConfigChange::SpecificationVersionChanged { old, new } => mark( "🔄", "~", - format!("Changed `specification_version`: {old} {arrow} {new}"), + format!("Moved to edition {new} (from {old}); no bump by itself, the schema changes decide"), ), ConfigChange::DependencyRemoved { project } => { mark("❌", "-", format!("Removed dependency `{project}`")) diff --git a/src/cli.rs b/src/cli.rs index 36f60c4..6c3dfa5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -84,6 +84,32 @@ first error found. Unlike `build`, this never writes to `.comline/` and never \ bumps the version, so it is safe to run in editors and pre-commit hooks.")] Check, + /// Apply the fixes the language asks for, or move to another edition + #[command(long_about = "\ +Apply the fixes Comline attaches to what it reports about the language: add a \ +missing `specification_version` to `config.idp`, and replace each use of a \ +keyword or type the package's edition deprecates or removes, when it has a \ +replacement. A use without one is listed to change by hand. + +With `--edition N`, the schemas are checked and fixed under edition N, and \ +`config.idp` moves to it, unless N removes a word the schemas still use that \ +has no replacement. Moving to a new edition doesn't bump the version by \ +itself; whatever it changes in the compiled schema does. + +Examples: + comline fix + comline fix --dry-run + comline fix --edition 2")] + Fix { + /// Move the package to this edition, fixing what it deprecates or removes + #[arg(long, value_name = "N")] + edition: Option, + + /// List the fixes without writing them + #[arg(long)] + dry_run: bool, + }, + /// Show what each protocol speaks on the wire and which guarantees it keeps #[command(long_about = "\ For each protocol, the format, framing and handshake it resolves to, their \ diff --git a/src/commands/fix.rs b/src/commands/fix.rs new file mode 100644 index 0000000..70f64ad --- /dev/null +++ b/src/commands/fix.rs @@ -0,0 +1,234 @@ +//! `comline fix` — apply the fixes core attaches to diagnostics, and move a +//! package to another edition (core#102). +//! +//! The fixes are the ones the language itself asks for: a `config.idp` +//! without an edition gets `specification_version`, and every use of a word +//! the edition deprecates or removes is replaced, when it has a +//! replacement. A use with no replacement is listed for the author to +//! change by hand. With `--edition N`, the schemas are checked under edition +//! N, fixed, and `config.idp` moves to N, unless a removed word is left +//! that has no replacement. + +use std::path::{Path, PathBuf}; + +use comline_core::diagnostics::{Diagnostic, Fix}; +use comline_core::edition::{self, Edition}; +use comline_core::package::config::idl::grammar::{self as config_grammar, Key}; +use comline_core::schema::idl::vocabulary; +use miette::{IntoDiagnostic, Result, WrapErr}; + +use crate::commands::ensure_project; +use crate::ui; + +const CONFIG: &str = "config.idp"; + +pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { + ensure_project(work_dir)?; + ui::step(format!( + "{}{}", + if dry_run { "Checking what to fix" } else { "Fixing" }, + ui::at_path(work_dir) + )); + + let config_path = work_dir.join(CONFIG); + let config_source = read(&config_path)?; + let config = config_grammar::parse(&config_source) + .map_err(|_| miette::miette!("{CONFIG} doesn't parse; `comline check` shows where"))?; + + // The package's edition, after fixing a missing or unknown one. + let mut config_out = config_source.clone(); + let mut fixed = 0usize; + let current = match edition::check_config(&config_source, &config) { + Ok(current) => current, + Err(diagnostic) => { + let fix = diagnostic.fix.as_ref().expect("core's edition errors carry a fix"); + ui::detail(format!("{CONFIG}: {}", fix.title)); + config_out = fix.apply(&config_out); + fixed += 1; + Edition::LATEST + } + }; + + let target = match to { + None => current, + Some(n) => { + let target = Edition::from_number(n).ok_or_else(|| { + miette::miette!("edition {n} isn't one this Comline knows; it knows {}", Edition::known()) + })?; + if target < current { + return Err(miette::miette!( + "the package is on edition {current}; moving back to {target} isn't supported" + )); + } + target + } + }; + + // Each schema under the target edition: what it deprecates or removes. + let mut writes: Vec<(PathBuf, String)> = Vec::new(); + let mut by_hand: Vec<(PathBuf, String, Diagnostic)> = Vec::new(); + for path in schema_files(&work_dir.join("src"))? { + let source = read(&path)?; + let findings = edition::deprecations(&source, target, vocabulary::DEPRECATIONS); + let (fixable, rest): (Vec<_>, Vec<_>) = findings.into_iter().partition(|d| d.fix.is_some()); + if !fixable.is_empty() { + let fixes: Vec<&Fix> = fixable.iter().filter_map(|d| d.fix.as_ref()).collect(); + for fix in &fixes { + ui::detail(format!("{}: {}", ui::shown_path(work_dir, &path), fix.title)); + } + fixed += fixes.len(); + writes.push((path.clone(), apply_all(&source, &fixes))); + } + by_hand.extend(rest.into_iter().map(|d| (path.clone(), source.clone(), d))); + } + + let blocked = by_hand.iter().any(|(_, _, d)| d.code == edition::REMOVED); + for (path, source, d) in &by_hand { + let rendered = comline_core::diagnostics::render(d, &ui::shown_path(work_dir, path), source); + if d.code == edition::REMOVED { ui::error(rendered) } else { ui::warn(rendered) } + } + + let moving = target != current; + if moving && !blocked { + config_out = set_edition(&config_out, target)?; + } + + if !dry_run { + if config_out != config_source { + write(&config_path, &config_out)?; + } + for (path, source) in &writes { + write(path, source)?; + } + } + + let verb = if dry_run { "would apply" } else { "applied" }; + let fixes = |n: usize| if n == 1 { "1 fix".to_string() } else { format!("{n} fixes") }; + match (moving, blocked) { + (true, true) => { + return Err(miette::miette!( + "edition {target} removes words used above that have no replacement; change them by \ + hand, then run `comline fix --edition {target}` again (the package stays on edition \ + {current}{})", + if fixed > 0 { format!("; {verb} {} meanwhile", fixes(fixed)) } else { String::new() } + )); + } + (true, false) => ui::success(format!( + "{} edition {current} {} {target}{}", + if dry_run { "Would move from" } else { "Moved from" }, + ui::arrow(), + if fixed > 0 { format!(", {verb} {}", fixes(fixed)) } else { String::new() } + )), + (false, _) if fixed == 0 && by_hand.is_empty() => ui::success("Nothing to fix"), + (false, _) => ui::success(format!( + "{} {}{}", + if dry_run { "Would apply" } else { "Applied" }, + fixes(fixed), + if by_hand.is_empty() { String::new() } else { format!(", {} to change by hand", by_hand.len()) } + )), + } + if blocked { + return Err(miette::miette!("the schemas use words their edition removed; change them by hand")); + } + Ok(()) +} + +/// `source` with every fix applied. Fixes from one schema never overlap: +/// each replaces one token. +fn apply_all(source: &str, fixes: &[&Fix]) -> String { + let merged = Fix { + title: String::new(), + edits: fixes.iter().flat_map(|f| f.edits.iter().cloned()).collect(), + }; + merged.apply(source) +} + +/// `config.idp` with `specification_version` set to `edition`. The line is +/// there: [`edition::check_config`] passed or its fix added it. +fn set_edition(source: &str, edition: Edition) -> Result { + let config = config_grammar::parse(source) + .map_err(|_| miette::miette!("{CONFIG} doesn't parse after fixing it"))?; + let assignment = config + .assignments + .iter() + .find(|a| matches!(&a.value.key, Key::Identifier(i) if i.value == "specification_version")) + .ok_or_else(|| miette::miette!("{CONFIG} has no `specification_version`"))?; + let (start, end) = (assignment.span.0, assignment.span.1); + let written = &source[start..end]; + let Some(eq) = written.find('=') else { + return Err(miette::miette!("{CONFIG}'s `specification_version` has no `=`")); + }; + let value_start = start + eq + 1 + (written[eq + 1..].len() - written[eq + 1..].trim_start().len()); + let value_end = start + written.trim_end().len(); + let fix = Fix::replace("", (value_start, value_end), edition.to_string()); + Ok(fix.apply(source)) +} + +/// Every `.ids` under `root`, sorted, so the output is stable. +fn schema_files(root: &Path) -> Result> { + let mut files = Vec::new(); + let mut dirs = vec![root.to_path_buf()]; + while let Some(dir) = dirs.pop() { + let Ok(entries) = std::fs::read_dir(&dir) else { continue }; + for entry in entries { + let path = entry.into_diagnostic()?.path(); + if path.is_dir() { + dirs.push(path); + } else if path.extension().is_some_and(|e| e == "ids") { + files.push(path); + } + } + } + files.sort(); + Ok(files) +} + +fn read(path: &Path) -> Result { + std::fs::read_to_string(path).into_diagnostic().wrap_err_with(|| format!("reading {}", path.display())) +} + +fn write(path: &Path, source: &str) -> Result<()> { + std::fs::write(path, source).into_diagnostic().wrap_err_with(|| format!("writing {}", path.display())) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn set_edition_replaces_only_the_number() { + let source = "congregation app\nspecification_version = 1 // the edition\nid = \"x\"\n"; + let out = set_edition(source, Edition::E1).unwrap(); + assert_eq!(out, source); + } + + #[test] + fn apply_all_applies_every_fix_in_one_file() { + // As two deprecations in one schema would: each replaces one token. + let source = "struct S {\n a: u8\n b: u8\n}\n"; + let at = |from: usize| { + let start = from + source[from..].find("u8").unwrap(); + (start, start + 2) + }; + let (a, b) = (at(0), at(at(0).1)); + let first = Fix::replace("a", a, "byte"); + let second = Fix::replace("b", b, "byte"); + assert_eq!(apply_all(source, &[&first, &second]), "struct S {\n a: byte\n b: byte\n}\n"); + } + + #[test] + fn schema_files_finds_nested_schemas_in_order() { + let dir = tempfile::tempdir().unwrap(); + let src = dir.path().join("src"); + std::fs::create_dir_all(src.join("a/b")).unwrap(); + for f in ["z.ids", "a/b/c.ids", "a/notes.md"] { + std::fs::write(src.join(f), "").unwrap(); + } + let found: Vec<_> = schema_files(&src) + .unwrap() + .iter() + .map(|p| p.strip_prefix(&src).unwrap().to_string_lossy().replace('\\', "/")) + .collect(); + assert_eq!(found, ["a/b/c.ids", "z.ids"]); + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 9e93298..03edb7b 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -10,6 +10,7 @@ pub mod completions; pub mod conformance; pub mod diff; pub mod doc; +pub mod fix; pub mod generate; pub mod inspect; pub mod new; diff --git a/src/main.rs b/src/main.rs index 555858f..4578223 100644 --- a/src/main.rs +++ b/src/main.rs @@ -121,6 +121,7 @@ fn run(cli: Cli) -> Result<()> { commands::build::run(&work_dir, watch, reconcile.as_deref()) } Commands::Check => commands::check::run(&work_dir), + Commands::Fix { edition, dry_run } => commands::fix::run(&work_dir, edition, dry_run), Commands::Inspect { json } => commands::inspect::run(&work_dir, json), Commands::Doc { out, From 9cd9a1554f48b4b2a5cc7fda7c6cbb0c82d81a43 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Mon, 9 Nov 2026 11:44:10 +0800 Subject: [PATCH 2/3] Pin core, codegen, the doc renderers and both generators at the editions cascade core b933209 (core#217), generation bb89a51, comline-rust de0b8e5, comline-typescript 9952c5a. --- Cargo.lock | 12 +++++++----- Cargo.toml | 10 +++++----- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cc08ee3..ecc5b3a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -565,7 +565,7 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=55d3fdc064ae3216f313975ce08071f3e5f32d3f#55d3fdc064ae3216f313975ce08071f3e5f32d3f" +source = "git+https://github.com/ComlineProject/generation?rev=bb89a516af2fdfa28ce7b9c137b53c1478ef4807#bb89a516af2fdfa28ce7b9c137b53c1478ef4807" dependencies = [ "comline-core", "eyre", @@ -579,7 +579,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=be90c80b97266025c4a4b69588f26cff1ca51e06#be90c80b97266025c4a4b69588f26cff1ca51e06" +source = "git+https://github.com/ComlineProject/comline-rust?rev=de0b8e51665bbf56f24e23a6f0164044428a9e61#de0b8e51665bbf56f24e23a6f0164044428a9e61" dependencies = [ "comline-codegen", "comline-core", @@ -589,7 +589,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=02ededc33c5473762c774757c69f5dfdef40626a#02ededc33c5473762c774757c69f5dfdef40626a" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=9952c5ad7400c4005b30e892f6a5e9f974799a48#9952c5ad7400c4005b30e892f6a5e9f974799a48" dependencies = [ "comline-codegen", "comline-core", @@ -599,6 +599,7 @@ dependencies = [ [[package]] name = "comline-core" version = "0.1.0" +source = "git+https://github.com/ComlineProject/core?rev=b933209052b06b328ee3da3355665faa0787063d#b933209052b06b328ee3da3355665faa0787063d" dependencies = [ "ariadne", "blake3", @@ -627,11 +628,12 @@ dependencies = [ [[package]] name = "comline-core-stdlib" version = "0.1.0" +source = "git+https://github.com/ComlineProject/core?rev=b933209052b06b328ee3da3355665faa0787063d#b933209052b06b328ee3da3355665faa0787063d" [[package]] name = "comline-doc-html" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=55d3fdc064ae3216f313975ce08071f3e5f32d3f#55d3fdc064ae3216f313975ce08071f3e5f32d3f" +source = "git+https://github.com/ComlineProject/generation?rev=bb89a516af2fdfa28ce7b9c137b53c1478ef4807#bb89a516af2fdfa28ce7b9c137b53c1478ef4807" dependencies = [ "comline-codegen", "eyre", @@ -642,7 +644,7 @@ dependencies = [ [[package]] name = "comline-doc-markdown" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=55d3fdc064ae3216f313975ce08071f3e5f32d3f#55d3fdc064ae3216f313975ce08071f3e5f32d3f" +source = "git+https://github.com/ComlineProject/generation?rev=bb89a516af2fdfa28ce7b9c137b53c1478ef4807#bb89a516af2fdfa28ce7b9c137b53c1478ef4807" dependencies = [ "comline-codegen", "eyre", diff --git a/Cargo.toml b/Cargo.toml index c9b95a5..75b9a9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,14 +38,14 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "b9332090 # `comline-codegen`'s types, so the rev below must be byte-identical to the # one *they* pin - Cargo only unifies a git dependency across identical rev # strings, and two copies of `GenRequest` are two incompatible types. -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "55d3fdc064ae3216f313975ce08071f3e5f32d3f" } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "bb89a516af2fdfa28ce7b9c137b53c1478ef4807" } # The official Markdown and HTML documentation renderers (`comline doc`), # linked in like the official generators. Same generation rev as # `comline-codegen`. -comline-doc-markdown = { git = "https://github.com/ComlineProject/generation", rev = "55d3fdc064ae3216f313975ce08071f3e5f32d3f" } -comline-doc-html = { git = "https://github.com/ComlineProject/generation", rev = "55d3fdc064ae3216f313975ce08071f3e5f32d3f" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "be90c80b97266025c4a4b69588f26cff1ca51e06", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "02ededc33c5473762c774757c69f5dfdef40626a", optional = true } +comline-doc-markdown = { git = "https://github.com/ComlineProject/generation", rev = "bb89a516af2fdfa28ce7b9c137b53c1478ef4807" } +comline-doc-html = { git = "https://github.com/ComlineProject/generation", rev = "bb89a516af2fdfa28ce7b9c137b53c1478ef4807" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "de0b8e51665bbf56f24e23a6f0164044428a9e61", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "9952c5ad7400c4005b30e892f6a5e9f974799a48", optional = true } # `comline conformance`: the conformance runner (corpus loading, judging, # profile proofs) and its case format, a public spec. Pinned by git rev. comline-runtime-conformance = { git = "https://github.com/ComlineProject/runtime", rev = "cf5e06780793d8e71b0ec1133da8fd6c0e42b8e9" } From e3bfd247d4e1a21650f01857db420056cec56fc9 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Mon, 9 Nov 2026 11:52:50 +0800 Subject: [PATCH 3/3] rustfmt --- src/changes.rs | 4 +- src/commands/fix.rs | 92 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/src/changes.rs b/src/changes.rs index d606516..ed04f8b 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -56,7 +56,9 @@ fn config_line(change: &ConfigChange) -> String { ConfigChange::SpecificationVersionChanged { old, new } => mark( "🔄", "~", - format!("Moved to edition {new} (from {old}); no bump by itself, the schema changes decide"), + format!( + "Moved to edition {new} (from {old}); no bump by itself, the schema changes decide" + ), ), ConfigChange::DependencyRemoved { project } => { mark("❌", "-", format!("Removed dependency `{project}`")) diff --git a/src/commands/fix.rs b/src/commands/fix.rs index 70f64ad..3b8bd85 100644 --- a/src/commands/fix.rs +++ b/src/commands/fix.rs @@ -26,7 +26,11 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { ensure_project(work_dir)?; ui::step(format!( "{}{}", - if dry_run { "Checking what to fix" } else { "Fixing" }, + if dry_run { + "Checking what to fix" + } else { + "Fixing" + }, ui::at_path(work_dir) )); @@ -41,7 +45,10 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { let current = match edition::check_config(&config_source, &config) { Ok(current) => current, Err(diagnostic) => { - let fix = diagnostic.fix.as_ref().expect("core's edition errors carry a fix"); + let fix = diagnostic + .fix + .as_ref() + .expect("core's edition errors carry a fix"); ui::detail(format!("{CONFIG}: {}", fix.title)); config_out = fix.apply(&config_out); fixed += 1; @@ -53,7 +60,10 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { None => current, Some(n) => { let target = Edition::from_number(n).ok_or_else(|| { - miette::miette!("edition {n} isn't one this Comline knows; it knows {}", Edition::known()) + miette::miette!( + "edition {n} isn't one this Comline knows; it knows {}", + Edition::known() + ) })?; if target < current { return Err(miette::miette!( @@ -74,7 +84,11 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { if !fixable.is_empty() { let fixes: Vec<&Fix> = fixable.iter().filter_map(|d| d.fix.as_ref()).collect(); for fix in &fixes { - ui::detail(format!("{}: {}", ui::shown_path(work_dir, &path), fix.title)); + ui::detail(format!( + "{}: {}", + ui::shown_path(work_dir, &path), + fix.title + )); } fixed += fixes.len(); writes.push((path.clone(), apply_all(&source, &fixes))); @@ -84,8 +98,13 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { let blocked = by_hand.iter().any(|(_, _, d)| d.code == edition::REMOVED); for (path, source, d) in &by_hand { - let rendered = comline_core::diagnostics::render(d, &ui::shown_path(work_dir, path), source); - if d.code == edition::REMOVED { ui::error(rendered) } else { ui::warn(rendered) } + let rendered = + comline_core::diagnostics::render(d, &ui::shown_path(work_dir, path), source); + if d.code == edition::REMOVED { + ui::error(rendered) + } else { + ui::warn(rendered) + } } let moving = target != current; @@ -103,7 +122,13 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { } let verb = if dry_run { "would apply" } else { "applied" }; - let fixes = |n: usize| if n == 1 { "1 fix".to_string() } else { format!("{n} fixes") }; + let fixes = |n: usize| { + if n == 1 { + "1 fix".to_string() + } else { + format!("{n} fixes") + } + }; match (moving, blocked) { (true, true) => { return Err(miette::miette!( @@ -115,20 +140,34 @@ pub fn run(work_dir: &Path, to: Option, dry_run: bool) -> Result<()> { } (true, false) => ui::success(format!( "{} edition {current} {} {target}{}", - if dry_run { "Would move from" } else { "Moved from" }, + if dry_run { + "Would move from" + } else { + "Moved from" + }, ui::arrow(), - if fixed > 0 { format!(", {verb} {}", fixes(fixed)) } else { String::new() } + if fixed > 0 { + format!(", {verb} {}", fixes(fixed)) + } else { + String::new() + } )), (false, _) if fixed == 0 && by_hand.is_empty() => ui::success("Nothing to fix"), (false, _) => ui::success(format!( "{} {}{}", if dry_run { "Would apply" } else { "Applied" }, fixes(fixed), - if by_hand.is_empty() { String::new() } else { format!(", {} to change by hand", by_hand.len()) } + if by_hand.is_empty() { + String::new() + } else { + format!(", {} to change by hand", by_hand.len()) + } )), } if blocked { - return Err(miette::miette!("the schemas use words their edition removed; change them by hand")); + return Err(miette::miette!( + "the schemas use words their edition removed; change them by hand" + )); } Ok(()) } @@ -156,9 +195,12 @@ fn set_edition(source: &str, edition: Edition) -> Result { let (start, end) = (assignment.span.0, assignment.span.1); let written = &source[start..end]; let Some(eq) = written.find('=') else { - return Err(miette::miette!("{CONFIG}'s `specification_version` has no `=`")); + return Err(miette::miette!( + "{CONFIG}'s `specification_version` has no `=`" + )); }; - let value_start = start + eq + 1 + (written[eq + 1..].len() - written[eq + 1..].trim_start().len()); + let value_start = + start + eq + 1 + (written[eq + 1..].len() - written[eq + 1..].trim_start().len()); let value_end = start + written.trim_end().len(); let fix = Fix::replace("", (value_start, value_end), edition.to_string()); Ok(fix.apply(source)) @@ -169,7 +211,9 @@ fn schema_files(root: &Path) -> Result> { let mut files = Vec::new(); let mut dirs = vec![root.to_path_buf()]; while let Some(dir) = dirs.pop() { - let Ok(entries) = std::fs::read_dir(&dir) else { continue }; + let Ok(entries) = std::fs::read_dir(&dir) else { + continue; + }; for entry in entries { let path = entry.into_diagnostic()?.path(); if path.is_dir() { @@ -184,11 +228,15 @@ fn schema_files(root: &Path) -> Result> { } fn read(path: &Path) -> Result { - std::fs::read_to_string(path).into_diagnostic().wrap_err_with(|| format!("reading {}", path.display())) + std::fs::read_to_string(path) + .into_diagnostic() + .wrap_err_with(|| format!("reading {}", path.display())) } fn write(path: &Path, source: &str) -> Result<()> { - std::fs::write(path, source).into_diagnostic().wrap_err_with(|| format!("writing {}", path.display())) + std::fs::write(path, source) + .into_diagnostic() + .wrap_err_with(|| format!("writing {}", path.display())) } #[cfg(test)] @@ -213,7 +261,10 @@ mod tests { let (a, b) = (at(0), at(at(0).1)); let first = Fix::replace("a", a, "byte"); let second = Fix::replace("b", b, "byte"); - assert_eq!(apply_all(source, &[&first, &second]), "struct S {\n a: byte\n b: byte\n}\n"); + assert_eq!( + apply_all(source, &[&first, &second]), + "struct S {\n a: byte\n b: byte\n}\n" + ); } #[test] @@ -227,7 +278,12 @@ mod tests { let found: Vec<_> = schema_files(&src) .unwrap() .iter() - .map(|p| p.strip_prefix(&src).unwrap().to_string_lossy().replace('\\', "/")) + .map(|p| { + p.strip_prefix(&src) + .unwrap() + .to_string_lossy() + .replace('\\', "/") + }) .collect(); assert_eq!(found, ["a/b/c.ids", "z.ids"]); }