starlane_core/config/
app.rs

1use crate::resource::{ResourceKind, ResourceAddress};
2use crate::cache::{Cacheable, Data};
3use std::collections::HashMap;
4use crate::resource::ArtifactKind;
5use crate::artifact::ArtifactRef;
6use crate::resource::config::{ResourceConfig, Parser};
7use std::sync::Arc;
8use crate::error::Error;
9use crate::config::app::yaml::AppConfigYaml;
10use std::str::FromStr;
11use std::convert::TryInto;
12use starlane_resources::ResourcePath;
13
14pub struct AppConfig {
15    artifact: ResourcePath,
16    pub main: ArtifactRef
17}
18
19impl Cacheable for AppConfig {
20    fn artifact(&self) -> ArtifactRef {
21        ArtifactRef {
22            path: self.artifact.clone(),
23            kind: ArtifactKind::AppConfig,
24        }
25    }
26
27    fn references(&self) -> Vec<ArtifactRef> {
28        vec![]
29    }
30}
31
32impl ResourceConfig for AppConfigParser {
33    fn kind(&self) -> ResourceKind {
34        ResourceKind::App
35    }
36}
37
38pub struct AppConfigParser;
39
40impl AppConfigParser {
41    pub fn new() -> Self {
42        Self {}
43    }
44}
45
46impl Parser<AppConfig> for AppConfigParser {
47    fn parse(&self, artifact: ArtifactRef, _data: Data) -> Result<Arc<AppConfig>, Error> {
48
49        let data = String::from_utf8((*_data).clone() )?;
50        let yaml: AppConfigYaml = serde_yaml::from_str( data.as_str() )?;
51
52        let address: ResourcePath = artifact.path.clone();
53        let bundle_address = address.parent().ok_or::<Error>("expected artifact to have bundle parent".into())?;
54
55        let main = yaml.spec.main.replace("{bundle}", bundle_address.to_string().as_str() );
56        let main = ResourcePath::from_str(main.as_str() )?;
57        let main = ArtifactRef::new(main.try_into()?,ArtifactKind::MechtronConfig );
58
59        Ok(Arc::new(AppConfig {
60            artifact: artifact.path,
61            main
62        }))
63    }
64}
65
66mod yaml {
67    use serde::{Serialize,Deserialize};
68
69    #[derive(Clone, Serialize, Deserialize)]
70    pub struct AppConfigYaml {
71        pub kind: String,
72        pub spec: SpecYaml
73    }
74
75    #[derive(Clone, Serialize, Deserialize)]
76    pub struct SpecYaml {
77        pub main: String
78    }
79}
80
81