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_unique_service_endpoints(&spec)?;
36    validate_cron_schedules(&spec)?;
37    validate_tunnels(&spec)?;
38    validate_wasm_configs(&spec)?;
39
40    Ok(spec)
41}
42
43/// Parse a deployment spec from YAML file
44///
45/// # Errors
46///
47/// Returns `SpecError` if the file cannot be read, or parsing/validation fails.
48pub fn from_yaml_file(path: &std::path::Path) -> Result<DeploymentSpec, SpecError> {
49    let content = std::fs::read_to_string(path)?;
50    from_yaml_str(&content)
51}