|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use std::fmt::Write as _; |
| 5 | +use std::fs; |
| 6 | +use std::path::PathBuf; |
| 7 | + |
| 8 | +use anyhow::Context; |
| 9 | +use anyhow::bail; |
| 10 | +use serde::Deserialize; |
| 11 | +use xshell::Shell; |
| 12 | +use xshell::cmd; |
| 13 | + |
| 14 | +/// Marker lines delimiting the generated registry section of the spec. Everything between |
| 15 | +/// them is owned by this generator; hand edits there are overwritten. |
| 16 | +static BEGIN_MARKER: &str = "<!-- BEGIN generated edition registry: edit the declarations in \ |
| 17 | + `vortex/src/editions/` and run `cargo xtask generate-editions-docs` -->"; |
| 18 | +static END_MARKER: &str = "<!-- END generated edition registry -->"; |
| 19 | + |
| 20 | +/// The manifest emitted by `cargo run -p vortex --example editions_manifest`. |
| 21 | +#[derive(Deserialize)] |
| 22 | +struct Manifest { |
| 23 | + editions: Vec<ManifestEdition>, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Deserialize)] |
| 27 | +struct ManifestEdition { |
| 28 | + id: String, |
| 29 | + /// Absent for draft editions. |
| 30 | + min_vortex_version: Option<String>, |
| 31 | + /// The encodings that join the family at this edition. |
| 32 | + added: Vec<String>, |
| 33 | + /// The edition's full computed encoding set. |
| 34 | + encodings: Vec<String>, |
| 35 | +} |
| 36 | + |
| 37 | +impl ManifestEdition { |
| 38 | + fn status(&self) -> &'static str { |
| 39 | + match self.min_vortex_version { |
| 40 | + Some(_) => "frozen", |
| 41 | + None => "draft", |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + fn min_reader(&self) -> &str { |
| 46 | + self.min_vortex_version.as_deref().unwrap_or("—") |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Regenerate the edition registry section of `docs/specs/editions.md` from the edition |
| 51 | +/// declarations in the `vortex` crate, via a TOML manifest written under `target/`. |
| 52 | +pub fn generate_editions_docs(profile: Option<String>) -> anyhow::Result<()> { |
| 53 | + let sh = Shell::new()?; |
| 54 | + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(".."); |
| 55 | + sh.change_dir(&root); |
| 56 | + |
| 57 | + let manifest_path = root.join("target/editions-manifest.toml"); |
| 58 | + let profile_args: Vec<String> = profile |
| 59 | + .into_iter() |
| 60 | + .flat_map(|p| ["--profile".to_string(), p]) |
| 61 | + .collect(); |
| 62 | + cmd!( |
| 63 | + sh, |
| 64 | + "cargo run {profile_args...} -p vortex --example editions_manifest -- {manifest_path}" |
| 65 | + ) |
| 66 | + .run()?; |
| 67 | + |
| 68 | + let manifest: Manifest = toml::from_str(&fs::read_to_string(&manifest_path)?) |
| 69 | + .context("parsing the editions manifest")?; |
| 70 | + |
| 71 | + let spec_path = root.join("docs/specs/editions.md"); |
| 72 | + let spec = fs::read_to_string(&spec_path)?; |
| 73 | + let (Some(begin), Some(end)) = (spec.find(BEGIN_MARKER), spec.find(END_MARKER)) else { |
| 74 | + bail!( |
| 75 | + "{} must contain the generated-registry BEGIN/END marker comments", |
| 76 | + spec_path.display() |
| 77 | + ); |
| 78 | + }; |
| 79 | + if end < begin { |
| 80 | + bail!( |
| 81 | + "generated-registry markers are out of order in {}", |
| 82 | + spec_path.display() |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + let mut spliced = String::new(); |
| 87 | + spliced.push_str(&spec[..begin + BEGIN_MARKER.len()]); |
| 88 | + spliced.push_str("\n\n"); |
| 89 | + spliced.push_str(&render(&manifest)?); |
| 90 | + spliced.push('\n'); |
| 91 | + spliced.push_str(&spec[end..]); |
| 92 | + fs::write(&spec_path, spliced)?; |
| 93 | + |
| 94 | + println!( |
| 95 | + "regenerated the edition registry in {}", |
| 96 | + spec_path.display() |
| 97 | + ); |
| 98 | + Ok(()) |
| 99 | +} |
| 100 | + |
| 101 | +/// Render the registry markdown that goes between the markers. |
| 102 | +fn render(manifest: &Manifest) -> anyhow::Result<String> { |
| 103 | + let mut md = String::new(); |
| 104 | + |
| 105 | + writeln!( |
| 106 | + md, |
| 107 | + "### Editions\n\n\ |
| 108 | + | Edition | Status | Minimum Vortex reader | Encodings |\n\ |
| 109 | + | --- | --- | --- | --- |" |
| 110 | + )?; |
| 111 | + for edition in &manifest.editions { |
| 112 | + writeln!( |
| 113 | + md, |
| 114 | + "| `{}` | {} | {} | {} |", |
| 115 | + edition.id, |
| 116 | + edition.status(), |
| 117 | + edition.min_reader(), |
| 118 | + edition.encodings.len(), |
| 119 | + )?; |
| 120 | + } |
| 121 | + |
| 122 | + writeln!( |
| 123 | + md, |
| 124 | + "\nA draft edition is still being assembled: its encoding set may change and it \ |
| 125 | + carries no compatibility guarantee until it is frozen.\n\n\ |
| 126 | + ### Encoding index\n\n\ |
| 127 | + Each encoding lists the edition it first joined; it is a member of every later \ |
| 128 | + edition of the same family. The minimum Vortex reader is the one declared by that \ |
| 129 | + edition.\n\n\ |
| 130 | + | Encoding | Since edition | Minimum Vortex reader |\n\ |
| 131 | + | --- | --- | --- |" |
| 132 | + )?; |
| 133 | + let mut rows: Vec<(&String, &ManifestEdition)> = manifest |
| 134 | + .editions |
| 135 | + .iter() |
| 136 | + .flat_map(|edition| { |
| 137 | + edition |
| 138 | + .added |
| 139 | + .iter() |
| 140 | + .map(move |encoding| (encoding, edition)) |
| 141 | + }) |
| 142 | + .collect(); |
| 143 | + // An encoding joins exactly one edition, so its id alone is a total order. |
| 144 | + rows.sort_by(|(a, _), (b, _)| a.cmp(b)); |
| 145 | + for (encoding, edition) in rows { |
| 146 | + writeln!( |
| 147 | + md, |
| 148 | + "| `{}` | `{}` | {} |", |
| 149 | + encoding, |
| 150 | + edition.id, |
| 151 | + edition.min_reader(), |
| 152 | + )?; |
| 153 | + } |
| 154 | + |
| 155 | + Ok(md) |
| 156 | +} |
0 commit comments