///|
fn decode_dependency(
id : String,
map : Map[String, Json],
platform : &Platform,
) -> Rule raise DoctorError {
let name = text_field(map, "package")
let base = dependency_rule(id, name, text_field(map, "constraint"))
let source = optional_text(map, "source", "")
if map.get("snapshot") is None {
if source != "" {
raise DoctorError("source requires a Moon tree snapshot")
}
return base
}
let path = text_field(map, "snapshot")
if !safe_relative(path) {
raise DoctorError("unsafe dependency snapshot path")
}
{
..base,
execute: ctx => {
let observed = read_moon_dependency(platform.read_text(path, 65536), name) catch {
err => {
if @async.is_being_cancelled() {
raise err
}
return finding(
Error,
"dependency_snapshot",
"Cannot read or validate Moon tree snapshot",
remediation="Export a successful moon tree --json result to a root-relative file",
)
}
}
match observed {
None =>
finding(
Failed,
"dependency_missing",
"Dependency is absent from the Moon graph's direct imports",
)
Some((version, kind)) => {
if source != "" && source != kind {
return finding(
Failed,
"dependency_source",
"Dependency source kind differs from the expected source",
remediation="Check the module import and the resolved dependency source",
)
}
let facts = ctx.facts.copy()
facts["dependency." + name] = version
(base.execute)({ ..ctx, facts, })
}
}
},
}
}
///|
/// Read a direct dependency from a successful Moon module graph export.
pub fn read_moon_dependency(
content : String,
name : String,
) -> (String, String)? raise DoctorError {
validate_input(content)
let data = @json.parse(
content,
max_nesting_depth=32,
reject_duplicate_keys=true,
) catch {
_ => raise DoctorError("invalid Moon dependency JSON")
}
let map = object(data)
if int_field(map, "version", 0, 1, 1) != 1 ||
text_field(map, "status") != "success" {
raise DoctorError("unsupported or failed Moon dependency graph")
}
let modules = array(required(map, "modules"))
if modules.is_empty() {
raise DoctorError("missing Moon graph modules")
}
let root = int_field(map, "root", -1, 0, modules.length() - 1)
if root < 0 {
raise DoctorError("missing Moon graph root")
}
let mut seen : (String, String)? = None
for value in array(required(map, "edges")) {
let edge = object(value)
let from = int_field(edge, "from", -1, 0, modules.length() - 1)
if from < 0 {
raise DoctorError("missing Moon edge origin")
}
if from == root && text_field(edge, "name") == name {
let to = int_field(edge, "to", -1, 0, modules.length() - 1)
if to < 0 {
raise DoctorError("missing Moon edge target")
}
let module_ = object(modules[to])
if text_field(module_, "name") != name {
raise DoctorError("Moon edge name differs from its target")
}
let version = text_field(module_, "version")
Version::parse(version) |> ignore
let source = text_field(object(required(module_, "source")), "kind")
if source.is_empty() {
raise DoctorError("missing dependency source kind")
}
if seen is Some(previous) && previous != (version, source) {
raise DoctorError("conflicting resolved dependencies")
}
seen = Some((version, source))
}
}
seen
}