Skip to main content

compose_validatr/
configs.rs

1//! Config fields and validation
2
3use crate::compose::Compose;
4use serde::{Deserialize, Serialize};
5
6use crate::{compose::Validate, errors::ValidationErrors};
7
8/// Represents the top level [Config](https://docs.docker.com/compose/compose-file/08-configs/) element
9#[derive(Debug, Clone, Deserialize, Serialize)]
10pub struct Config {
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub file: Option<String>,
13
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub external: Option<bool>,
16
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub name: Option<String>,
19}
20
21impl Validate for Config {
22    fn validate(&self, _: &Compose, _: &mut ValidationErrors) {
23        // Nothing to validate
24        // Not interested in validating the existence of files on host
25        ()
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use serde_yaml;
33
34    #[test]
35    fn test_deserialize_configs() {
36        let yaml = r#"
37          file: "path/to/config1"
38          external: true
39          name: "named_config"
40        "#;
41
42        let configs: Config = serde_yaml::from_str(yaml).unwrap();
43
44        assert_eq!(configs.file.unwrap(), "path/to/config1".to_string());
45        assert_eq!(configs.external.unwrap(), true);
46        assert_eq!(configs.name.unwrap(), "named_config".to_string());
47    }
48}