use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::error::ErrorCode;
use crate::model::StoreModel;
use crate::model::clients::known_client;
use crate::model::ids::{self, IdKind};
use crate::model::validate::{Diagnostics, at, join};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ApplyTrigger {
Instant,
Review,
Restore,
Rollback,
Pull,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ApplyOutcome {
Ok,
Partial,
Blocked,
Failed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "kebab-case")]
pub enum Scope {
User,
Project,
ProjectLocal,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(title = "Surjection apply record (store/machines/<machine-id>.applied.toml)")]
pub struct AppliedRecord {
#[schemars(extend("const" = 1))]
pub schema: i64,
#[schemars(extend("pattern" = ids::ULID_PATTERN))]
pub machine_id: String,
#[schemars(extend("pattern" = ids::ULID_PATTERN))]
pub apply_id: String,
#[schemars(with = "String", extend("format" = "date-time"))]
pub time: toml::value::Datetime,
pub store_revision: String,
pub trigger: ApplyTrigger,
pub outcome: ApplyOutcome,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub targets: Vec<AppliedTarget>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl StoreModel for AppliedRecord {
const SCHEMA_NAME: &'static str = "applied";
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct AppliedTarget {
pub client: String,
pub scope: Scope,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project_id: Option<String>,
pub file: String,
pub create: u32,
pub update: u32,
pub remove: u32,
pub disable: u32,
pub outcome: ApplyOutcome,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl AppliedRecord {
pub fn validate(&self, file_id: Option<&str>) -> Diagnostics {
let mut d = Diagnostics::new();
d.check_named_id(IdKind::Machine, &self.machine_id, "machine_id", file_id);
d.check_id(IdKind::Apply, &self.apply_id, "apply_id");
let complete = self.time.date.is_some() && self.time.time.is_some();
if !complete || self.time.offset.is_none() {
d.error(
ErrorCode::InvalidValue,
"time",
"`time` must be an offset date-time such as 2026-01-01T00:00:00Z",
);
}
if !ids::is_commit_id(&self.store_revision) {
d.error(
ErrorCode::InvalidValue,
"store_revision",
format!("{:?} is not a git commit id", self.store_revision),
);
}
for (index, target) in self.targets.iter().enumerate() {
let path = at("targets", index);
if known_client(&target.client).is_none() {
d.warning(
ErrorCode::UnknownClient,
join(&path, "client"),
format!("unknown client {:?}; kept as written", target.client),
);
}
match (target.scope, &target.project_id) {
(Scope::Project | Scope::ProjectLocal, None) => d.error(
ErrorCode::MissingField,
join(&path, "project_id"),
"project targets need `project_id`",
),
(_, Some(id)) => {
d.check_id(IdKind::Project, id, &join(&path, "project_id"));
}
(Scope::User, None) => {}
}
if !has_placeholder_root(&target.file) {
d.error(
ErrorCode::InvalidPath,
join(&path, "file"),
format!(
"{:?} must start with `~/`, `<P>/`, `%NAME%/` or `$NAME/`",
target.file
),
);
}
}
d
}
}
fn has_placeholder_root(file: &str) -> bool {
if ["~/", "<P>/"].iter().any(|root| file.starts_with(root)) {
return true;
}
let var = |name: &str| ids::is_valid(IdKind::EnvVar, name);
if let Some(rest) = file.strip_prefix('%')
&& let Some((name, _)) = rest.split_once("%/")
{
return var(name);
}
file.strip_prefix('$')
.and_then(|rest| rest.split_once('/'))
.is_some_and(|(name, _)| var(name))
}