use std::path::PathBuf;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Format {
Human,
Machine,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceKind {
Git,
Hg,
Svn,
Cvs,
}
impl SourceKind {
#[must_use]
pub fn label(self) -> &'static str {
match self {
Self::Git => "git",
Self::Hg => "hg",
Self::Svn => "svn",
Self::Cvs => "cvs",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Command {
Decode {
kind: SourceKind,
source: PathBuf,
out: PathBuf,
infer_renames: bool,
reconstruct_refs: bool,
spill_dir: Option<PathBuf>,
format: Format,
},
Inspect {
artifact: PathBuf,
atoms: bool,
format: Format,
},
Verify {
artifact: PathBuf,
against_source: Option<PathBuf>,
spill_dir: Option<PathBuf>,
format: Format,
},
Version,
Help,
}
pub const USAGE: &str = "\
brygge — carry version-control history into an intermediate representation (IR).
USAGE:
brygge decode <git|hg|svn|cvs> <source> --out <artifact> [--infer-renames | --reconstruct-refs] [--spill-dir <DIR>] [--format human|machine]
brygge inspect <artifact> [--atoms] [--format human|machine]
brygge verify <artifact> [--against-source <source> [--spill-dir <DIR>]] [--format human|machine]
brygge --version | --help | <command> --help
COMMANDS:
decode read a source into an IR artifact (git, hg, svn, or cvs in this build). --out is required.
For svn, <source> is a repository directory (dumped read-only via `svnadmin dump`) or a
dumpfile; for cvs, a local repository directory of RCS ,v files. --infer-renames (git) and
--reconstruct-refs (svn/cvs) each turn on an opt-in, always-marked derived layer; giving either
to a source kind it does not apply to is a usage error. File content is kept on disk while
decoding, in a private file removed when brygge exits — --spill-dir <DIR> chooses where (it
must already exist); the default is --out's own directory. The option never changes the
artifact; a RAM-backed directory (tmpfs) moves the memory cost rather than removing it
inspect print the fidelity report an artifact alone reproduces; --atoms adds the per-atom
listing: epistemic status, source ids, derived parameters, and the loss boundary
verify the honesty checks any reader can run with no source (always); with --against-source, also
re-derive from the source and confirm correspondence — the two results are reported
separately, never merged. The re-derive's own file content is kept on disk the same way
decode's is; --spill-dir <DIR> chooses where (it must already exist, and only applies with
--against-source); the default is the artifact's own directory
VOCABULARY: artifact, source, stated, derived, dropped, flagged, refused, Unverifiable.";
fn usage_error(command: &str, problem: impl std::fmt::Display) -> String {
format!("{problem}\n\nrun `brygge {command} --help` for usage")
}
fn inapplicable_option(option: &str, kinds: &str, kind: SourceKind) -> String {
format!("--{option} applies to {kinds}, not to {}", kind.label())
}
pub fn parse(args: &[String]) -> Result<Command, String> {
let mut it = args.iter();
let Some(first) = it.next() else {
return Ok(Command::Help);
};
match first.as_str() {
"--version" | "-V" => Ok(Command::Version),
"--help" | "-h" => Ok(Command::Help),
"decode" => parse_decode(args.get(1..).unwrap_or(&[])),
"inspect" => parse_inspect(args.get(1..).unwrap_or(&[])),
"verify" => parse_verify(args.get(1..).unwrap_or(&[])),
"encode" => Err(
"'encode' is not available yet: the prikk encoder follows prikk's import foundations \
(see ROADMAP)"
.to_string(),
),
other => Err(format!(
"unknown command '{other}'\n\nrun `brygge --help` for usage"
)),
}
}
fn wants_help(args: &[String]) -> bool {
args.iter().any(|a| a == "--help" || a == "-h")
}
fn parse_format(rest: &mut std::slice::Iter<'_, String>, command: &str) -> Result<Format, String> {
match rest.next().map(String::as_str) {
Some("human") => Ok(Format::Human),
Some("machine") => Ok(Format::Machine),
Some(other) => Err(usage_error(
command,
format!("--format expects 'human' or 'machine', got '{other}'"),
)),
None => Err(usage_error(
command,
"--format needs a value ('human' or 'machine')",
)),
}
}
fn need_value(
rest: &mut std::slice::Iter<'_, String>,
flag: &str,
command: &str,
) -> Result<PathBuf, String> {
rest.next()
.map(PathBuf::from)
.ok_or_else(|| usage_error(command, format!("{flag} needs a value")))
}
fn parse_decode(args: &[String]) -> Result<Command, String> {
if wants_help(args) {
return Ok(Command::Help);
}
let mut it = args.iter();
let kind = match it.next().map(String::as_str) {
Some("git") => SourceKind::Git,
Some("hg") => SourceKind::Hg,
Some("svn") => SourceKind::Svn,
Some("cvs") => SourceKind::Cvs,
Some(other) => {
return Err(usage_error(
"decode",
format!(
"source kind '{other}' is not supported (git, hg, svn, or cvs in this build)"
),
));
}
None => {
return Err(usage_error(
"decode",
"decode needs a source kind and a source: decode <git|hg|svn|cvs> <source> --out <artifact>",
));
}
};
let mut source: Option<PathBuf> = None;
let mut out: Option<PathBuf> = None;
let mut out_seen = false;
let mut infer_renames = false;
let mut infer_renames_seen = false;
let mut reconstruct_refs = false;
let mut reconstruct_refs_seen = false;
let mut spill_dir: Option<PathBuf> = None;
let mut spill_dir_seen = false;
let mut format = Format::Human;
let mut format_seen = false;
while let Some(arg) = it.next() {
match arg.as_str() {
"--out" => {
if out_seen {
return Err(usage_error("decode", "--out given more than once"));
}
out_seen = true;
out = Some(need_value(&mut it, "--out", "decode")?);
}
"--spill-dir" => {
if spill_dir_seen {
return Err(usage_error("decode", "--spill-dir given more than once"));
}
spill_dir_seen = true;
spill_dir = Some(need_value(&mut it, "--spill-dir", "decode")?);
}
"--infer-renames" => {
if infer_renames_seen {
return Err(usage_error(
"decode",
"--infer-renames given more than once",
));
}
infer_renames_seen = true;
infer_renames = true;
}
"--reconstruct-refs" => {
if reconstruct_refs_seen {
return Err(usage_error(
"decode",
"--reconstruct-refs given more than once",
));
}
reconstruct_refs_seen = true;
reconstruct_refs = true;
}
"--format" => {
if format_seen {
return Err(usage_error("decode", "--format given more than once"));
}
format_seen = true;
format = parse_format(&mut it, "decode")?;
}
other if other.starts_with('-') => {
return Err(usage_error("decode", format!("unknown flag '{other}'")));
}
other => {
if source.is_some() {
return Err(usage_error(
"decode",
format!("unexpected extra argument '{other}'"),
));
}
source = Some(PathBuf::from(other));
}
}
}
let source = source
.ok_or_else(|| usage_error("decode", "decode needs a source: <git|hg|svn|cvs> <source>"))?;
let out = out.ok_or_else(|| usage_error("decode", "decode needs --out <artifact>"))?;
if infer_renames && !matches!(kind, SourceKind::Git) {
return Err(usage_error(
"decode",
inapplicable_option("infer-renames", "git", kind),
));
}
if reconstruct_refs && !matches!(kind, SourceKind::Svn | SourceKind::Cvs) {
return Err(usage_error(
"decode",
inapplicable_option("reconstruct-refs", "svn, cvs", kind),
));
}
Ok(Command::Decode {
kind,
source,
out,
infer_renames,
reconstruct_refs,
spill_dir,
format,
})
}
fn parse_inspect(args: &[String]) -> Result<Command, String> {
if wants_help(args) {
return Ok(Command::Help);
}
let mut it = args.iter();
let mut artifact: Option<PathBuf> = None;
let mut atoms = false;
let mut atoms_seen = false;
let mut format = Format::Human;
let mut format_seen = false;
while let Some(arg) = it.next() {
match arg.as_str() {
"--atoms" => {
if atoms_seen {
return Err(usage_error("inspect", "--atoms given more than once"));
}
atoms_seen = true;
atoms = true;
}
"--format" => {
if format_seen {
return Err(usage_error("inspect", "--format given more than once"));
}
format_seen = true;
format = parse_format(&mut it, "inspect")?;
}
other if other.starts_with('-') => {
return Err(usage_error("inspect", format!("unknown flag '{other}'")));
}
other => {
if artifact.is_some() {
return Err(usage_error(
"inspect",
format!("unexpected extra argument '{other}'"),
));
}
artifact = Some(PathBuf::from(other));
}
}
}
let artifact = artifact.ok_or_else(|| usage_error("inspect", "inspect needs an <artifact>"))?;
Ok(Command::Inspect {
artifact,
atoms,
format,
})
}
fn parse_verify(args: &[String]) -> Result<Command, String> {
if wants_help(args) {
return Ok(Command::Help);
}
let mut it = args.iter();
let mut artifact: Option<PathBuf> = None;
let mut against_source: Option<PathBuf> = None;
let mut against_source_seen = false;
let mut spill_dir: Option<PathBuf> = None;
let mut spill_dir_seen = false;
let mut format = Format::Human;
let mut format_seen = false;
while let Some(arg) = it.next() {
match arg.as_str() {
"--against-source" => {
if against_source_seen {
return Err(usage_error(
"verify",
"--against-source given more than once",
));
}
against_source_seen = true;
against_source = Some(need_value(&mut it, "--against-source", "verify")?);
}
"--spill-dir" => {
if spill_dir_seen {
return Err(usage_error("verify", "--spill-dir given more than once"));
}
spill_dir_seen = true;
spill_dir = Some(need_value(&mut it, "--spill-dir", "verify")?);
}
"--format" => {
if format_seen {
return Err(usage_error("verify", "--format given more than once"));
}
format_seen = true;
format = parse_format(&mut it, "verify")?;
}
other if other.starts_with('-') => {
return Err(usage_error("verify", format!("unknown flag '{other}'")));
}
other => {
if artifact.is_some() {
return Err(usage_error(
"verify",
format!("unexpected extra argument '{other}'"),
));
}
artifact = Some(PathBuf::from(other));
}
}
}
let artifact = artifact.ok_or_else(|| usage_error("verify", "verify needs an <artifact>"))?;
if spill_dir.is_some() && against_source.is_none() {
return Err(usage_error(
"verify",
"--spill-dir only applies with --against-source",
));
}
Ok(Command::Verify {
artifact,
against_source,
spill_dir,
format,
})
}
#[cfg(test)]
mod tests;