use std::collections::HashMap;
use anyhow::{bail, Context};
use bytesize::ByteSize;
use super::StringWebcIdent;
#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Clone, Debug)]
pub struct AppConfigV1 {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub app_id: Option<String>,
pub package: StringWebcIdent,
#[serde(default, skip_serializing_if = "HashMap::is_empty")]
pub env: HashMap<String, String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub cli_args: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub capabilities: Option<AppConfigCapabilityMapV1>,
}
impl AppConfigV1 {
pub const KIND: &'static str = "wasmer.io/App.v0";
pub const CANONICAL_FILE_NAME: &'static str = "app.yaml";
pub fn to_yaml_value(self) -> Result<serde_yaml::Value, serde_yaml::Error> {
let obj = match serde_yaml::to_value(self)? {
serde_yaml::Value::Mapping(m) => m,
_ => unreachable!(),
};
let mut m = serde_yaml::Mapping::new();
m.insert("kind".into(), Self::KIND.into());
for (k, v) in obj.into_iter() {
m.insert(k, v);
}
Ok(m.into())
}
pub fn to_yaml(self) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(&self.to_yaml_value()?)
}
pub fn parse_yaml(value: &str) -> Result<Self, anyhow::Error> {
let raw = serde_yaml::from_str::<serde_yaml::Value>(value).context("invalid yaml")?;
let kind = raw
.get("kind")
.context("invalid app config: no 'kind' field found")?
.as_str()
.context("invalid app config: 'kind' field is not a string")?;
match kind {
Self::KIND => {}
other => {
bail!(
"invalid app config: unspported kind '{}', expected {}",
other,
Self::KIND
);
}
}
let data = serde_yaml::from_value(raw).context("could not deserialize app config")?;
Ok(data)
}
}
#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Clone, Debug)]
pub struct AppConfigCapabilityMapV1 {
#[serde(skip_serializing_if = "Option::is_none")]
pub memory: Option<AppConfigCapabilityMemoryV1>,
}
#[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema, Clone, Debug)]
pub struct AppConfigCapabilityMemoryV1 {
#[schemars(with = "Option<String>")]
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<ByteSize>,
}