starlane_core/config/
mechtron.rs

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