use std::collections::BTreeMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::error::ErrorCode;
use crate::model::clients::ClientSelection;
use crate::model::ids::IdKind;
use crate::model::server::ServerBody;
use crate::model::validate::{Diagnostics, at, join};
use crate::model::{SCHEMA_VERSION, StoreModel};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[schemars(
title = "Surjection project manifest (.surjection/project.toml, .surjection/project.local.toml)"
)]
pub struct ProjectManifest {
#[schemars(extend("const" = 1))]
pub schema: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[schemars(extend("pattern" = crate::model::ids::SLUG_PATTERN))]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub defaults: Option<ProjectDefaults>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub servers: Option<BTreeMap<String, ProjectServer>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub skills: Option<ProjectSkills>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
impl StoreModel for ProjectManifest {
const SCHEMA_NAME: &'static str = "project";
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct ProjectDefaults {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clients: Option<ClientSelection>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct ProjectServer {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enabled: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clients: Option<ClientSelection>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub unset: Option<Vec<String>>,
#[serde(flatten)]
pub body: ServerBody,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct ProjectSkills {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub enable: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub share: Option<bool>,
#[serde(flatten)]
#[schemars(skip)]
pub extra: toml::Table,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ManifestLayer<'a> {
Shared,
Local { shared_id: Option<&'a str> },
}
impl ProjectManifest {
pub fn new(id: Option<String>) -> Self {
Self {
schema: SCHEMA_VERSION,
id,
name: None,
defaults: None,
servers: None,
skills: None,
extra: toml::Table::new(),
}
}
pub fn validate(&self, layer: ManifestLayer<'_>, dir_id: Option<&str>) -> Diagnostics {
let mut d = Diagnostics::new();
match (layer, self.id.as_deref()) {
(ManifestLayer::Shared, None) => {
d.error(ErrorCode::MissingField, "id", "project.toml needs `id`");
}
(ManifestLayer::Local { shared_id: None }, None) => d.error(
ErrorCode::MissingField,
"id",
"project.local.toml without a project.toml is a private project and needs `id`",
),
(ManifestLayer::Local { shared_id: Some(_) }, None) => {}
(ManifestLayer::Shared | ManifestLayer::Local { shared_id: None }, Some(id)) => {
d.check_named_id(IdKind::Project, id, "id", dir_id);
}
(
ManifestLayer::Local {
shared_id: Some(shared),
},
Some(id),
) => {
if d.check_id(IdKind::Project, id, "id") && id != shared {
d.warning(
ErrorCode::IdMismatch,
"id",
format!("id {id:?} differs from project.toml's {shared:?}, which wins"),
);
}
}
}
if let Some(clients) = self.defaults.as_ref().and_then(|x| x.clients.as_ref()) {
d.check_client_selection(clients, "defaults.clients");
}
let standalone = !matches!(layer, ManifestLayer::Local { shared_id: Some(_) });
for (name, server) in self.servers.iter().flatten() {
let path = join("servers", name);
d.check_id(IdKind::Server, name, &path);
server.validate_into(&path, standalone, &mut d);
}
if let Some(skills) = self
.skills
.as_ref()
.and_then(|skills| skills.enable.as_ref())
{
for (index, id) in skills.iter().enumerate() {
d.check_id(IdKind::Skill, id, &at("skills.enable", index));
}
}
d
}
}
impl ProjectServer {
pub fn is_suppression(&self) -> bool {
self.enabled == Some(false) && self.from.is_none() && self.body == ServerBody::default()
}
fn validate_into(&self, path: &str, standalone: bool, d: &mut Diagnostics) {
if let Some(from) = &self.from {
d.check_id(IdKind::Server, from, &join(path, "from"));
}
if let Some(clients) = &self.clients {
d.check_client_selection(clients, &join(path, "clients"));
}
for (index, entry) in self.unset.iter().flatten().enumerate() {
if entry.split('.').any(str::is_empty) {
d.error(
ErrorCode::InvalidValue,
at(&join(path, "unset"), index),
format!("invalid unset path {entry:?}; expected a dotted key such as env.FOO"),
);
}
}
let complete = standalone && self.from.is_none() && !self.is_suppression();
self.body.validate_into(path, complete, d);
}
}