use std::collections::BTreeMap;
use serde::Deserialize;
use crate::error::{Error, Result};
use crate::source::NormalizedUrl;
use super::{Source, Target};
#[derive(Debug, Clone)]
pub struct TransitiveManifest {
pub sources: BTreeMap<String, Source>,
pub targets: BTreeMap<String, Target>,
hooks: Option<toml::Value>,
}
#[derive(Debug, Deserialize)]
struct ManifestGraph {
#[serde(default)]
sources: BTreeMap<String, Source>,
#[serde(default)]
targets: BTreeMap<String, Target>,
}
impl TransitiveManifest {
pub fn parse(text: &str) -> Result<Self> {
let document: toml::Value =
toml::from_str(text).map_err(|e| Error::Config(e.to_string()))?;
let hooks = collect_opaque_hooks(&document);
let graph: ManifestGraph = document
.try_into()
.map_err(|e| Error::Config(e.to_string()))?;
Ok(Self {
sources: graph.sources,
targets: graph.targets,
hooks,
})
}
#[must_use]
pub fn hooks(&self) -> Option<&toml::Value> {
self.hooks.as_ref()
}
}
fn collect_opaque_hooks(document: &toml::Value) -> Option<toml::Value> {
let targets = document.get("targets")?.as_table()?;
let mut retained = toml::value::Table::new();
for (name, target) in targets {
if let Some(hooks) = target.get("hooks") {
retained.insert(name.clone(), hooks.clone());
}
}
if retained.is_empty() {
None
} else {
Some(toml::Value::Table(retained))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct FetchNode {
url: String,
r#ref: String,
commit: String,
}
impl FetchNode {
#[must_use]
pub fn new(url: &str, r#ref: &str, commit: &str) -> Self {
Self {
url: NormalizedUrl::parse(url).as_str().to_owned(),
r#ref: r#ref.to_owned(),
commit: commit.to_owned(),
}
}
#[must_use]
pub fn commit(&self) -> &str {
&self.commit
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Instance {
parent: String,
source_name: String,
anchor_target: String,
fetch_node: FetchNode,
}
impl Instance {
#[must_use]
pub fn new(
parent: &str,
source_name: &str,
anchor_target: &str,
fetch_node: FetchNode,
) -> Self {
Self {
parent: parent.to_owned(),
source_name: source_name.to_owned(),
anchor_target: anchor_target.to_owned(),
fetch_node,
}
}
#[must_use]
pub fn fetch_node(&self) -> &FetchNode {
&self.fetch_node
}
#[must_use]
pub fn stable_key(&self) -> String {
let mut hasher = blake3::Hasher::new();
for field in [
self.parent.as_str(),
self.source_name.as_str(),
self.anchor_target.as_str(),
self.fetch_node.url.as_str(),
self.fetch_node.r#ref.as_str(),
self.fetch_node.commit.as_str(),
] {
hasher.update(&(field.len() as u64).to_le_bytes());
hasher.update(field.as_bytes());
}
hasher.finalize().to_hex()[..16].to_owned()
}
}