1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::shared::models::internal::ParsedConfig;
use crate::shared::{FILE_DIR_ANNOTATION, FILE_EXEC_PATH_ANNOTATION, FILE_PATH_ANNOTATION};
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::collections::BTreeMap;

use derive_builder::Builder;
use strum::EnumString;

mod internal;
mod v1alpha;

pub mod prelude {
    pub use super::internal::prelude::*;
    pub use super::ScopeModel;
    pub use super::{ModelMetadata, ModelMetadataBuilder, ModelRoot, ModelRootBuilder};
}

#[derive(Debug, PartialEq, EnumString)]
#[strum(ascii_case_insensitive)]
pub enum KnownApiVersion {
    #[strum(serialize = "scope.github.com/v1alpha")]
    ScopeV1Alpha,
    #[strum(default)]
    UnknownApiVersion(String),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Builder)]
#[builder(setter(into))]
pub struct ModelMetadata {
    pub name: String,
    #[serde(default)]
    pub annotations: BTreeMap<String, String>,
    #[serde(default)]
    pub labels: BTreeMap<String, String>,
}

impl ModelMetadata {
    fn file_path(&self) -> String {
        self.annotations
            .get(FILE_PATH_ANNOTATION)
            .cloned()
            .unwrap_or_else(|| "unknown".to_string())
    }

    fn containing_dir(&self) -> String {
        self.annotations
            .get(FILE_DIR_ANNOTATION)
            .cloned()
            .unwrap_or_else(|| "unknown".to_string())
    }

    fn exec_path(&self) -> String {
        self.annotations
            .get(FILE_EXEC_PATH_ANNOTATION)
            .cloned()
            .unwrap_or_else(|| std::env::var("PATH").unwrap_or_else(|_| "".to_string()))
    }
}

pub trait ScopeModel {
    fn name(&self) -> &str;
    fn kind(&self) -> &str;
    fn full_name(&self) -> String {
        format!("{}/{}", self.kind(), self.name())
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Builder)]
#[builder(setter(into))]
#[serde(rename_all = "camelCase")]
pub struct ModelRoot<V> {
    pub api_version: String,
    pub kind: String,
    pub metadata: ModelMetadata,
    pub spec: V,
}

impl<V> ModelRoot<V> {
    pub fn with_spec<T>(&self, spec: T) -> ModelRoot<T> {
        ModelRoot {
            api_version: self.api_version.clone(),
            kind: self.kind.clone(),
            metadata: self.metadata.clone(),
            spec,
        }
    }

    pub fn file_path(&self) -> String {
        self.metadata.file_path()
    }
    pub fn containing_dir(&self) -> String {
        self.metadata.containing_dir()
    }

    pub fn exec_path(&self) -> String {
        self.metadata.exec_path()
    }
}

impl<V> ScopeModel for ModelRoot<V> {
    fn name(&self) -> &str {
        &self.metadata.name
    }

    fn kind(&self) -> &str {
        &self.kind
    }
}

impl TryFrom<&ModelRoot<Value>> for ParsedConfig {
    type Error = anyhow::Error;

    fn try_from(root: &ModelRoot<Value>) -> Result<Self, Self::Error> {
        let api_version: &str = &root.api_version.trim().to_ascii_lowercase();
        let api_versions = KnownApiVersion::try_from(api_version)
            .unwrap_or_else(|_| KnownApiVersion::UnknownApiVersion(api_version.to_string()));

        match api_versions {
            KnownApiVersion::ScopeV1Alpha => Ok(v1alpha::parse_v1_alpha1(root)?),
            KnownApiVersion::UnknownApiVersion(_) => {
                Err(anyhow!("Unable to parse {}", api_version))
            }
        }
    }
}

#[cfg(test)]
pub(crate) fn parse_models_from_string(
    file_path: &std::path::Path,
    input: &str,
) -> anyhow::Result<Vec<ParsedConfig>> {
    use serde_yaml::Deserializer;

    let mut models = Vec::new();
    for doc in Deserializer::from_str(input) {
        if let Some(parsed_model) = crate::shared::config_load::parse_model(doc, file_path) {
            models.push(parsed_model.try_into()?)
        }
    }

    Ok(models)
}