use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::{ComponentId, Result};
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[non_exhaustive]
pub struct ProviderDescription {
#[serde(default)]
pub(crate) id: ComponentId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) image_ref: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) name: Option<String>,
#[serde(default)]
pub(crate) revision: i32,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) annotations: Option<BTreeMap<String, String>>,
}
impl ProviderDescription {
pub fn id(&self) -> &str {
&self.id
}
pub fn image_ref(&self) -> Option<&str> {
self.image_ref.as_deref()
}
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
pub fn revision(&self) -> i32 {
self.revision
}
pub fn annotations(&self) -> Option<&BTreeMap<String, String>> {
self.annotations.as_ref()
}
#[must_use]
pub fn builder() -> ProviderDescriptionBuilder {
ProviderDescriptionBuilder::default()
}
}
#[derive(Clone, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub struct ProviderDescriptionBuilder {
id: Option<ComponentId>,
image_ref: Option<String>,
name: Option<String>,
revision: Option<i32>,
annotations: Option<BTreeMap<String, String>>,
}
impl ProviderDescriptionBuilder {
#[must_use]
pub fn id(mut self, v: &str) -> Self {
self.id = Some(v.into());
self
}
#[must_use]
pub fn image_ref(mut self, v: &str) -> Self {
self.image_ref = Some(v.into());
self
}
#[must_use]
pub fn name(mut self, v: &str) -> Self {
self.name = Some(v.into());
self
}
#[must_use]
pub fn revision(mut self, v: i32) -> Self {
self.revision = Some(v);
self
}
#[must_use]
pub fn annotations(mut self, v: impl Into<BTreeMap<String, String>>) -> Self {
self.annotations = Some(v.into());
self
}
pub fn build(self) -> Result<ProviderDescription> {
Ok(ProviderDescription {
id: self.id.ok_or_else(|| "id is required".to_string())?,
image_ref: self.image_ref,
name: self.name,
revision: self.revision.unwrap_or_default(),
annotations: self.annotations,
})
}
}
#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use super::ProviderDescription;
#[test]
fn provider_description_builder() {
assert_eq!(
ProviderDescription {
id: "id".into(),
image_ref: Some("ref".into()),
name: Some("name".into()),
annotations: Some(BTreeMap::from([("a".into(), "b".into())])),
revision: 0,
},
ProviderDescription::builder()
.id("id")
.image_ref("ref")
.name("name")
.annotations(BTreeMap::from([("a".into(), "b".into())]))
.revision(0)
.build()
.unwrap()
)
}
}