use serde_json::{Map, Value, json};
use super::Backup;
pub(super) const MANIFEST: &str = "manifest.json";
const FORMAT: u64 = 1;
pub(super) const MOST_BYTES: u64 = 64 << 20;
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct Manifest {
pub(super) created: i64,
pub(super) files: Vec<Listed>,
pub(super) key_left_out: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Listed {
pub(super) name: String,
pub(super) engine: &'static str,
pub(super) size: u64,
pub(super) sha256: String,
}
impl Manifest {
pub(super) fn to_json(&self) -> String {
let files: Vec<Value> = self
.files
.iter()
.map(|file| json!({ "name": file.name, "engine": file.engine, "size": file.size, "sha256": file.sha256 }))
.collect();
let manifest =
json!({ "format": FORMAT, "created": self.created, "keyLeftOut": self.key_left_out, "files": files });
format!("{manifest:#}\n")
}
pub(super) fn read(text: &[u8]) -> Result<Manifest, String> {
let value: Value = serde_json::from_slice(text).map_err(|error| format!("its manifest is no JSON: {error}"))?;
let map = value.as_object().ok_or("its manifest is no object")?;
match map.get("format").and_then(Value::as_u64) {
Some(FORMAT) => {}
Some(format) => return Err(format!("format {format}, where this version reads {FORMAT}")),
None => return Err("its manifest names no format".to_owned()),
}
let created = map.get("created").and_then(Value::as_i64).ok_or("its manifest has no time")?;
let key_left_out = map.get("keyLeftOut").and_then(Value::as_bool).unwrap_or(false);
let files = map.get("files").and_then(Value::as_array).ok_or("its manifest lists no files")?;
let files = files.iter().map(listed).collect::<Result<Vec<_>, String>>()?;
Ok(Manifest { created, files, key_left_out })
}
pub(super) fn summary(&self) -> Backup {
Backup {
files: self.files.len(),
bytes: self.files.iter().map(|file| file.size).sum(),
key_left_out: self.key_left_out,
}
}
}
fn listed(value: &Value) -> Result<Listed, String> {
let map: &Map<String, Value> = value.as_object().ok_or("its manifest lists a file that is no object")?;
let text =
|field: &str| map.get(field).and_then(Value::as_str).ok_or(format!("a file of its manifest has no {field}"));
let name = text("name")?.to_owned();
let size = map.get("size").and_then(Value::as_u64).ok_or(format!("{name}: no size in its manifest"))?;
let sha256 = text("sha256")?.to_owned();
if sha256.len() != 64 || !sha256.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return Err(format!("{name}: a SHA-256 in its manifest that is not one"));
}
let engine = engine(text("engine")?);
Ok(Listed { name, engine, size, sha256: sha256.to_ascii_lowercase() })
}
fn engine(name: &str) -> &'static str {
const KNOWN: [&str; 10] = ["kv", "jobs", "sql", "blobs", "records", "metrics", "search", "program", "key", "other"];
KNOWN.into_iter().find(|known| *known == name).unwrap_or("other")
}