gcloud_ctx/
properties.rs

1use crate::Error;
2use serde::{Deserialize, Serialize};
3use serde_ini::{Serializer, Writer};
4use std::io::{Read, Write};
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
7/// Configuration properties
8pub struct Properties {
9    /// Core properties
10    #[serde(skip_serializing_if = "Option::is_none")]
11    core: Option<CoreProperties>,
12
13    /// Compute properties
14    #[serde(skip_serializing_if = "Option::is_none")]
15    compute: Option<ComputeProperties>,
16}
17
18impl Properties {
19    /// Deserialise properties from the given reader
20    pub fn from_reader<R: Read>(reader: R) -> Result<Self, Error> {
21        let properties = serde_ini::de::from_read(reader)?;
22        Ok(properties)
23    }
24
25    /// Serialise the properties to the given writer
26    pub fn to_writer<W: Write>(&self, writer: W) -> Result<(), Error> {
27        let mut ser = Serializer::new(Writer::new(writer, serde_ini::LineEnding::Linefeed));
28        self.serialize(&mut ser)?;
29
30        Ok(())
31    }
32}
33
34#[derive(Serialize, Deserialize, Debug, Clone, Default)]
35/// Supported properties in the core section
36struct CoreProperties {
37    /// `core/project` setting
38    #[serde(skip_serializing_if = "Option::is_none")]
39    project: Option<String>,
40
41    /// `core/account` setting
42    #[serde(skip_serializing_if = "Option::is_none")]
43    account: Option<String>,
44}
45
46#[derive(Serialize, Deserialize, Debug, Clone, Default)]
47/// Supported properties in the compute section
48struct ComputeProperties {
49    /// `compute/zone` setting - default compute zone
50    #[serde(skip_serializing_if = "Option::is_none")]
51    zone: Option<String>,
52
53    /// `compute/region` setting - default compute region
54    #[serde(skip_serializing_if = "Option::is_none")]
55    region: Option<String>,
56}
57
58#[derive(Debug, Default)]
59/// Properties builder
60pub struct PropertiesBuilder {
61    /// core/project setting
62    project: Option<String>,
63
64    /// core/account setting
65    account: Option<String>,
66
67    /// compute/zone setting
68    zone: Option<String>,
69
70    /// compute/region setting
71    region: Option<String>,
72}
73
74impl PropertiesBuilder {
75    /// Build the properties
76    pub fn build(&self) -> Properties {
77        let core = if self.project.is_some() || self.account.is_some() {
78            Some(CoreProperties {
79                project: self.project.clone(),
80                account: self.account.clone(),
81            })
82        } else {
83            None
84        };
85
86        let compute = if self.zone.is_some() || self.region.is_some() {
87            Some(ComputeProperties {
88                zone: self.zone.clone(),
89                region: self.region.clone(),
90            })
91        } else {
92            None
93        };
94
95        Properties { core, compute }
96    }
97
98    /// Set the project property
99    pub fn project(&mut self, project: &str) -> &mut Self {
100        self.project = Some(project.to_owned());
101        self
102    }
103
104    /// Set the account property
105    pub fn account(&mut self, account: &str) -> &mut Self {
106        self.account = Some(account.to_owned());
107        self
108    }
109
110    /// Set the zone property
111    pub fn zone(&mut self, zone: &str) -> &mut Self {
112        self.zone = Some(zone.to_owned());
113        self
114    }
115
116    /// Set the region property
117    pub fn region(&mut self, region: &str) -> &mut Self {
118        self.region = Some(region.to_owned());
119        self
120    }
121}