xvc_pipeline/pipeline/
schema.rs

1use std::{ffi::OsStr, path::Path};
2
3use crate::error::{Error, Result};
4use crate::pipeline::deps::XvcDependency;
5use crate::pipeline::XvcOutput;
6use serde::{Deserialize, Serialize};
7use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
8use xvc_core::XvcPath;
9
10use super::XvcStepInvalidate;
11
12#[derive(Debug, Clone, Eq, PartialEq, EnumString, Display, IntoStaticStr, VariantNames)]
13#[strum(serialize_all = "lowercase")]
14pub enum XvcSchemaSerializationFormat {
15    Json,
16    Yaml,
17    // Turned off because of UnsupportedType error
18    // TOML,
19}
20
21impl XvcSchemaSerializationFormat {
22    fn from_extension(ext: &OsStr) -> Result<Self> {
23        match ext.to_str().unwrap_or("") {
24            "json" | "JSON" => Ok(Self::Json),
25            "yaml" | "yml" => Ok(Self::Yaml),
26            // "toml" | "tom" | "tml" => Ok(Self::TOML),
27            _ => Err(Error::CannotInferFormatFromExtension {
28                extension: ext.into(),
29            }),
30        }
31    }
32
33    pub fn from_path(path: &Path) -> Result<Self> {
34        Self::from_extension(path.extension().unwrap_or_else(|| OsStr::new("")))
35    }
36}
37
38/// Defines the user editable pipeline schema used in `xvc pipeline export` and
39/// `xvc pipeline import` commands.
40#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
41pub struct XvcPipelineSchema {
42    /// Version of the schema, currently 1.
43    pub version: i32,
44    /// Name of the pipeline.
45    /// Note that this can also be specified in CLI with `--name` flag and it
46    /// supersedes this value.
47    pub name: String,
48    /// Path to the pipeline root directory.
49    pub workdir: XvcPath,
50    /// List of steps in the pipeline.
51    pub steps: Vec<XvcStepSchema>,
52}
53
54/// User editable pipeline step schema used in `xvc pipeline export` and `xvc
55/// pipeline import` commands.
56#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
57pub struct XvcStepSchema {
58    /// Name of the step.
59    pub name: String,
60    /// Command to run in the step.
61    pub command: String,
62    /// When we consider the step as changed?
63    pub invalidate: XvcStepInvalidate,
64    /// List of dependencies of the step.
65    /// These do not require a separate schema.
66    pub dependencies: Vec<XvcDependency>,
67    /// List of outputs of the step.
68    /// These do not require a separate schema.
69    pub outputs: Vec<XvcOutput>,
70}