//! The spec: populate's arguments as JSON, as docs/design.md describes them. use serde::Deserialize; use serde_json::{Map, Value}; use std::collections::BTreeMap; /// The version of the spec this populate reads. pub const VERSION: u64 = 1; /// The source types populate knows. pub const SOURCE_TYPES: &[&str] = &[ "derivation", "file", "flake", "flake-input", "git", "pass", "passage", "pipe", "symlink", ]; #[derive(Debug, Deserialize)] #[serde(deny_unknown_fields)] pub struct Spec { /// Checked before the rest, so it's only part of the shape here. #[allow(dead_code)] pub version: u64, #[serde(default)] pub backup: bool, #[serde(default)] pub force: bool, #[serde(default)] pub prune: bool, pub source: BTreeMap, pub target: Target, } #[derive(Clone, Debug, Deserialize)] #[serde(deny_unknown_fields, rename_all = "camelCase")] pub struct Target { #[serde(default)] pub user: Option, pub host: String, #[serde(default)] pub port: Option, pub path: String, #[serde(default)] pub sudo: bool, #[serde(default)] pub extra_options: Vec, /// Whether the target is the machine populate runs on, or null to /// decide that when it runs. #[serde(default)] pub local: Option, } /// A source: its type, and the settings of that type, under the type's name. #[derive(Debug)] pub struct Source { pub kind: String, pub settings: Map, } impl<'de> Deserialize<'de> for Source { fn deserialize>(deserializer: D) -> Result { use serde::de::Error; let mut object = Map::deserialize(deserializer)?; let kind = match object.remove("type") { Some(Value::String(kind)) => kind, Some(_) => return Err(D::Error::custom("type must be a string")), None => return Err(D::Error::missing_field("type")), }; if !SOURCE_TYPES.contains(&kind.as_str()) { return Err(D::Error::custom(format!("unknown source type {kind:?}"))); } let settings = match object.remove(&kind) { Some(Value::Object(settings)) => settings, Some(_) => return Err(D::Error::custom(format!("{kind} must be an object"))), None => return Err(D::Error::missing_field("settings named after the type")), }; if let Some(other) = object.keys().next() { return Err(D::Error::custom(format!( "unknown field {other:?} of a {kind} source" ))); } Ok(Source { kind, settings }) } } /// An input drawn from another source: the path of input names of the flake /// in the source drawing it, and the name of the source it's drawn from. #[derive(Debug, PartialEq)] pub struct Follows { pub path: Vec, pub source: String, } impl Source { /// The inputs this source draws from other sources, like krops' lib's /// flakeFollows. pub fn follows(&self) -> Vec { fn walk(prefix: &[String], inputs: &Map, out: &mut Vec) { for (name, input) in inputs { let mut path = prefix.to_vec(); path.push(name.clone()); if let Some(Value::String(source)) = input.get("follows") { out.push(Follows { path: path.clone(), source: source.clone(), }); } if let Some(Value::Object(inputs)) = input.get("inputs") { walk(&path, inputs, out); } } } let mut out = Vec::new(); if let Some(Value::Object(inputs)) = self.settings.get("inputs") { walk(&[], inputs, &mut out); } out } }