tc_core/
docker.rs

1use std::{collections::HashMap, io::Read};
2use Container;
3use Image;
4
5/// Defines the minimum API required for interacting with the Docker daemon.
6pub trait Docker
7where
8    Self: Sized,
9{
10    fn run<I: Image>(&self, image: I) -> Container<Self, I>;
11    fn logs(&self, id: &str) -> Logs;
12    fn ports(&self, id: &str) -> Ports;
13    fn rm(&self, id: &str);
14    fn stop(&self, id: &str);
15}
16
17/// The exposed ports of a running container.
18#[derive(Debug, PartialEq, Default)]
19pub struct Ports {
20    mapping: HashMap<u32, u32>,
21}
22
23impl Ports {
24    /// Registers the mapping of an exposed port.
25    pub fn add_mapping(&mut self, internal: u32, host: u32) -> &mut Self {
26        debug!("Registering port mapping: {} -> {}", internal, host);
27
28        self.mapping.insert(internal, host);
29
30        self
31    }
32
33    /// Returns the host port for the given internal port.
34    pub fn map_to_host_port(&self, internal_port: u32) -> Option<u32> {
35        self.mapping.get(&internal_port).map(|p| p.clone())
36    }
37}
38
39/// Log streams of running container (stdout & stderr).
40#[derive(DebugStub)]
41pub struct Logs {
42    #[debug_stub = "stream"]
43    pub stdout: Box<Read>,
44    #[debug_stub = "stream"]
45    pub stderr: Box<Read>,
46}