buildkit_frontend/options/
common.rs

1use std::collections::HashMap;
2use std::fmt;
3
4use buildkit_proto::moby::buildkit::v1::frontend::CacheOptionsEntry as CacheOptionsEntryProto;
5use serde::de::{self, Deserializer, SeqAccess, Visitor};
6use serde::Deserialize;
7
8#[derive(Clone, Debug, Deserialize, PartialEq)]
9pub struct CacheOptionsEntry {
10    #[serde(rename = "Type")]
11    pub cache_type: CacheType,
12
13    #[serde(rename = "Attrs")]
14    pub attrs: HashMap<String, String>,
15}
16
17#[derive(Clone, Debug, Deserialize, PartialEq)]
18#[serde(rename_all = "lowercase")]
19pub enum CacheType {
20    Local,
21    Registry,
22    Inline,
23}
24
25impl CacheOptionsEntry {
26    pub fn from_legacy_list<'de, D>(deserializer: D) -> Result<Vec<Self>, D::Error>
27    where
28        D: Deserializer<'de>,
29    {
30        struct LegacyVisitor;
31
32        impl<'de> Visitor<'de> for LegacyVisitor {
33            type Value = Vec<String>;
34
35            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
36                formatter.write_str("sequence")
37            }
38
39            fn visit_seq<M>(self, map: M) -> Result<Self::Value, M::Error>
40            where
41                M: SeqAccess<'de>,
42            {
43                Deserialize::deserialize(de::value::SeqAccessDeserializer::new(map))
44            }
45        }
46
47        let legacy_refs = deserializer.deserialize_seq(LegacyVisitor)?;
48        let new_refs_iter = legacy_refs.into_iter().map(|reference| CacheOptionsEntry {
49            cache_type: CacheType::Registry,
50            attrs: vec![(String::from("ref"), reference)].into_iter().collect(),
51        });
52
53        Ok(new_refs_iter.collect())
54    }
55}
56
57impl Into<CacheOptionsEntryProto> for CacheOptionsEntry {
58    fn into(self) -> CacheOptionsEntryProto {
59        CacheOptionsEntryProto {
60            r#type: self.cache_type.into(),
61            attrs: self.attrs,
62        }
63    }
64}
65
66impl Into<String> for CacheType {
67    fn into(self) -> String {
68        match self {
69            CacheType::Local => "local".into(),
70            CacheType::Registry => "registry".into(),
71            CacheType::Inline => "inline".into(),
72        }
73    }
74}