diff options
Diffstat (limited to 'src/volume.rs')
-rw-r--r-- | src/volume.rs | 79 |
1 files changed, 77 insertions, 2 deletions
diff --git a/src/volume.rs b/src/volume.rs index 1a9048a..8290e2b 100644 --- a/src/volume.rs +++ b/src/volume.rs @@ -2,11 +2,17 @@ //! //! API Reference: <https://docs.docker.com/engine/api/v1.41/#tag/Volume> +use std::{ + collections::{BTreeMap, HashMap}, + hash::Hash, +}; + use hyper::Body; +use serde::Serialize; +use serde_json::{json, Value}; use crate::{ - builder::VolumeCreateOptions, - errors::Result, + errors::{Error, Result}, rep::{Volume as VolumeRep, VolumeCreateInfo, Volumes as VolumesRep}, Docker, }; @@ -83,3 +89,72 @@ impl<'docker> Volume<'docker> { Ok(()) } } + +/// Interface for creating volumes +#[derive(Serialize, Debug)] +pub struct VolumeCreateOptions { + params: HashMap<&'static str, Value>, +} + +impl VolumeCreateOptions { + /// serialize options as a string. returns None if no options are defined + pub fn serialize(&self) -> Result<String> { + serde_json::to_string(&self.params).map_err(Error::from) + } + + pub fn parse_from<'a, K, V>( + &self, + params: &'a HashMap<K, V>, + body: &mut BTreeMap<String, Value>, + ) where + &'a HashMap<K, V>: IntoIterator, + K: ToString + Eq + Hash, + V: Serialize, + { + for (k, v) in params.iter() { + let key = k.to_string(); + let value = serde_json::to_value(v).unwrap(); + + body.insert(key, value); + } + } + + /// return a new instance of a builder for options + pub fn builder() -> VolumeCreateOptionsBuilder { + VolumeCreateOptionsBuilder::new() + } +} + +#[derive(Default)] +pub struct VolumeCreateOptionsBuilder { + params: HashMap<&'static str, Value>, +} + +impl VolumeCreateOptionsBuilder { + pub(crate) fn new() -> Self { + let params = HashMap::new(); + VolumeCreateOptionsBuilder { params } + } + + pub fn name( + &mut self, + name: &str, + ) -> &mut Self { + self.params.insert("Name", json!(name)); + self + } + + pub fn labels( + &mut self, + labels: &HashMap<&str, &str>, + ) -> &mut Self { + self.params.insert("Labels", json!(labels)); + self + } + + pub fn build(&self) -> VolumeCreateOptions { + VolumeCreateOptions { + params: self.params.clone(), + } + } +} |