use serde::Deserialize;
use std::fmt::Write;
use std::path::Path;
pub const LOCK_VERSION: u32 = 1;
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct Lock {
pub version: u32,
#[serde(default)]
pub aeneas: String,
#[serde(default)]
pub charon: String,
#[serde(default)]
pub model: Vec<Model>,
}
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct Model {
pub rust: String,
pub lean: String,
#[serde(default)]
pub instances: Vec<String>,
#[serde(default)]
pub items: Vec<LockItem>,
#[serde(default)]
pub obligation: Vec<Obligation>,
}
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct LockItem {
pub rust: String,
pub hash: String,
}
#[derive(Debug, Clone, Default, Deserialize, PartialEq)]
pub struct Obligation {
pub label: String,
pub spec: String,
#[serde(default)]
pub spec_block: String,
#[serde(default)]
pub spec_hash: String,
pub hash: String,
pub status: String,
#[serde(default)]
pub axioms: Vec<String>,
#[serde(default)]
pub proof: String,
}
impl Obligation {
pub fn statement(&self, model: &str) -> String {
format!("{model}.obligation.{}", self.label)
}
pub fn theorem(&self, model: &str) -> String {
format!("{model}.proof.{}", self.label)
}
}
fn quote(s: &str) -> String {
let mut out = String::from("\"");
for c in s.chars() {
match c {
'"' => out.push_str("\\\""),
'\\' => out.push_str("\\\\"),
'\n' => out.push_str("\\n"),
c => out.push(c),
}
}
out.push('"');
out
}
impl Lock {
pub fn read(path: &Path) -> Result<Lock, String> {
let text = match std::fs::read_to_string(path) {
Ok(t) => t,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
return Ok(Lock {
version: LOCK_VERSION,
..Lock::default()
})
}
Err(e) => return Err(format!("{}: {e}", path.display())),
};
let lock: Lock = toml::from_str(&text).map_err(|e| format!("{}: {e}", path.display()))?;
if lock.version != LOCK_VERSION {
return Err(format!(
"{}: version {} is not supported (this leanfran writes version {LOCK_VERSION})",
path.display(),
lock.version
));
}
Ok(lock)
}
pub fn model(&self, rust: &str) -> Option<&Model> {
self.model.iter().find(|m| m.rust == rust)
}
pub fn to_toml(&self) -> String {
let mut s = String::new();
let _ = writeln!(s, "version = {}", self.version);
let _ = writeln!(s, "aeneas = {}", quote(&self.aeneas));
let _ = writeln!(s, "charon = {}", quote(&self.charon));
for m in &self.model {
let _ = writeln!(s, "\n[[model]]");
let _ = writeln!(s, "rust = {}", quote(&m.rust));
let _ = writeln!(s, "lean = {}", quote(&m.lean));
if !m.instances.is_empty() {
let ts: Vec<String> = m.instances.iter().map(|t| quote(t)).collect();
let _ = writeln!(s, "instances = [{}]", ts.join(", "));
}
let _ = writeln!(s, "items = [");
for i in &m.items {
let _ = writeln!(
s,
" {{ rust = {}, hash = {} }},",
quote(&i.rust),
quote(&i.hash)
);
}
let _ = writeln!(s, "]");
for o in &m.obligation {
let _ = writeln!(s, "\n[[model.obligation]]");
let _ = writeln!(s, "label = {}", quote(&o.label));
let _ = writeln!(s, "spec = {}", quote(&o.spec));
let _ = writeln!(s, "spec_block = {}", quote(&o.spec_block));
let _ = writeln!(s, "spec_hash = {}", quote(&o.spec_hash));
let _ = writeln!(s, "hash = {}", quote(&o.hash));
let _ = writeln!(s, "status = {}", quote(&o.status));
let axioms: Vec<String> = o.axioms.iter().map(|a| quote(a)).collect();
let _ = writeln!(s, "axioms = [{}]", axioms.join(", "));
let _ = writeln!(s, "proof = {}", quote(&o.proof));
}
}
s
}
}