Skip to main content

zlayer_types/spec/
mod.rs

1//! `ZLayer` V1 Service Specification
2//!
3//! This crate provides types for parsing and validating `ZLayer` deployment specifications.
4
5pub mod error;
6pub mod types;
7pub mod validate;
8
9pub use error::*;
10pub use types::*;
11pub use validate::*;
12
13use validator::Validate;
14
15/// Parse a deployment spec from YAML string
16///
17/// # Errors
18///
19/// Returns `SpecError` if parsing or validation fails.
20pub fn from_yaml_str(yaml: &str) -> Result<DeploymentSpec, SpecError> {
21    let spec: DeploymentSpec = serde_yaml::from_str(yaml)?;
22
23    // Run validator crate validation
24    spec.validate().map_err(|e| {
25        SpecError::Validation(ValidationError {
26            kind: ValidationErrorKind::Generic {
27                message: e.to_string(),
28            },
29            path: String::new(),
30        })
31    })?;
32
33    // Cross-field validation
34    validate_dependencies(&spec)?;
35    validate_swarm(&spec)?;
36    validate_unique_service_endpoints(&spec)?;
37    validate_cron_schedules(&spec)?;
38    validate_tunnels(&spec)?;
39    validate_wasm_configs(&spec)?;
40
41    Ok(spec)
42}
43
44/// Parse a deployment spec from YAML file
45///
46/// # Errors
47///
48/// Returns `SpecError` if the file cannot be read, or parsing/validation fails.
49pub fn from_yaml_file(path: &std::path::Path) -> Result<DeploymentSpec, SpecError> {
50    let content = std::fs::read_to_string(path)?;
51    from_yaml_str(&content)
52}