Rewrite your app in Rust, piece by piece, checked against the original at every step. Works with TypeScript, JavaScript, Python, Go, Ruby, Java, C, PHP, C#, Kotlin, PowerShell and Swift.
Rust agent-skillsclaude-codeclicode-migrationcodexffigolangjavajavascriptmcpmigrationnapiperformanceportingpyo3pythonrewriterubyrusttypescript
//! What each journal entry becomes: refused by the original, already recorded, explained by the
//! registry, or outside the registry. Refusal comes first: an entry is "recorded" or
//! "explained" only if the rule that links the cases (`Lens`, so `read_case` and `place`) says
//! so, and when in doubt (a rule that refuses, an unread site, an `unknown` registry) it is
//! outside.
use std::collections::BTreeSet;
use serde::Serialize;
use super::entry::{Entry, Identity};
use crate::obligations::Ledger;
use crate::obligations::evidence::{CliProbe, HttpProbe, Lens};
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "class", rename_all = "kebab-case")]
pub(crate) enum Class {
/// The original refused it (non-zero exit, response outside 2xx or absent): it says nothing
/// about what the program must do.
Refused,
/// A recorded case has the same entry.
Recorded,
/// The registry reads it: a command or a route the original declares. `gains` are the
/// obligations without a case that a case of this entry would move to `matched`.
Explained { by: String, gains: Vec<String> },
/// Neither recorded nor explained: served by the original, and nothing that was read covers it.
Outside { why: String },
}
/// A journal entry and what was concluded about it.
#[derive(Clone, Debug)]
pub(crate) struct Judged {
pub id: String,
pub entry: Entry,
pub class: Class,
}
impl Judged {
/// Does it block, as long as no waiver covers it? Only an entry outside the registry blocks.
pub fn blocks(&self) -> bool {
matches!(self.class, Class::Outside { .. })
}
/// The name of the case that `--inputs` gives it.
pub fn case_name(&self) -> String {
format!("fallback-{}", self.entry.digest())
}
}
/// Judges each entry. `journal` says where they come from: those of a journal that the original
/// refused are counted without blocking; those of the stored file (`stored`) do not go through
/// this refusal, because a hand may have written any status there and nothing is waived in the
/// person's place. `uncovered` are the obligations without a case, to compute what a case would
/// gain.
pub(crate) fn judge(
lens: &Lens,
ledger: &Ledger,
uncovered: &BTreeSet<String>,
entries: Vec<Entry>,
journal: bool,
) -> Vec<Judged> {
let mut out: Vec<Judged> = entries
.into_iter()
.map(|entry| Judged {
id: entry.id(),
class: class_of(lens, &entry, journal),
entry,
})
.collect();
let mut clis = Vec::new();
let mut https = Vec::new();
for j in &out {
if !matches!(j.class, Class::Explained { .. }) {
continue;
}
let name = j.case_name();
match &j.entry.identity {
Identity::Cli { args, .. } => clis.push(CliProbe {
name,
args: args.clone(),
}),
Identity::Http { method, path, .. } => https.push(HttpProbe {
name,
method: method.clone(),
target: path.clone(),
}),
// Never explained (see `class_of`): nothing to probe.
Identity::Skipped { .. } | Identity::Truncated { .. } => {}
}
}
if uncovered.is_empty() || (clis.is_empty() && https.is_empty()) {
return out;
}
let gained = lens.gains(ledger, uncovered, &clis, &https);
for j in &mut out {
let name = j.case_name();
if let Class::Explained { gains, .. } = &mut j.class
&& let Some(found) = gained.get(&name)
{
let mut found = found.clone();
found.sort();
found.dedup();
*gains = found;
}
}
out
}
fn class_of(lens: &Lens, entry: &Entry, journal: bool) -> Class {
// What is known only by its size is neither refused by the original (what it answered is
// unknown), nor recorded, nor explained: outside, and only a waiver covers it.
match &entry.identity {
Identity::Skipped { bytes, .. } => {
return Class::Outside {
why: format!(
"a fallback of {bytes} bytes was too long to be journaled: nothing is known of it but its size; run again with a fresh journal, or waive it"
),
};
}
Identity::Truncated { bytes, .. } => {
return Class::Outside {
why: format!(
"the journal reached its bound after {bytes} bytes: later fallbacks were not written; run again with a fresh journal, or waive it"
),
};
}
_ => {}
}
if journal && !entry.served() {
return Class::Refused;
}
match &entry.identity {
Identity::Cli { lossy: true, .. } => Class::Outside {
why: "an argument is not UTF-8: the line shown is not the line that ran, so no case can be matched to it or recorded from it".to_string(),
},
Identity::Cli { args, stdin, .. } => {
if lens.cli_recorded(args, stdin.as_ref()) {
return Class::Recorded;
}
match lens.cli_explained(args) {
Ok(()) => Class::Explained {
by: "a command line the registry reads".to_string(),
gains: Vec::new(),
},
Err(why) => Class::Outside { why },
}
}
Identity::Http { method, path, body } => {
if lens.http_recorded(method, path, body.as_ref()) {
return Class::Recorded;
}
match lens.http_explained(method, path) {
Ok(route) => Class::Explained {
by: format!("route {route}"),
gains: Vec::new(),
},
Err(why) => Class::Outside { why },
}
}
Identity::Skipped { .. } | Identity::Truncated { .. } => Class::Outside {
why: "nothing is known of it".to_string(),
},
}
}