1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
extern crate serde;
#[cfg_attr(test, macro_use)]
extern crate serde_json;
extern crate url;
#[macro_use]
extern crate serde_derive;
extern crate serde_urlencoded;
extern crate serde_yaml;
#[macro_use]
extern crate failure;
extern crate base64;
extern crate futures;
extern crate hyper;
extern crate hyper_tls;
extern crate native_tls;
extern crate openssl;
extern crate tokio_core;
#[macro_use]
extern crate log;
pub mod api;
pub mod client;
mod groupversion;
mod serde_base64;
mod unstructured;
use std::borrow::Cow;
use std::slice;
use api::meta::v1::{ListMeta, ObjectMeta};
pub use groupversion::*;
pub use unstructured::*;
pub trait Metadata {
fn api_version(&self) -> &str;
fn kind(&self) -> &str;
fn metadata(&self) -> Cow<ObjectMeta>;
}
pub trait List<T> {
fn listmeta(&self) -> Cow<ListMeta>;
fn items(&self) -> &[T];
fn items_mut(&mut self) -> &mut [T];
fn into_items(self) -> Vec<T>;
}
impl<'a, T> IntoIterator for &'a List<T> {
type Item = &'a T;
type IntoIter = slice::Iter<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.items().into_iter()
}
}
impl<'a, T> IntoIterator for &'a mut List<T> {
type Item = &'a mut T;
type IntoIter = slice::IterMut<'a, T>;
fn into_iter(self) -> Self::IntoIter {
self.items_mut().iter_mut()
}
}
#[cfg(test)]
mod tests {
use super::Metadata;
use serde_json::{self, Value};
fn pod_json() -> Value {
json!({
"apiVersion": "v1",
"kind": "Pod",
"metadata": {
"name": "pod-example",
},
"spec": {
"containers": [
{
"image": "busybox",
"command": ["echo"],
"args": ["Hello world"],
},
],
},
})
}
#[test]
fn untyped() {
let j = pod_json();
assert_eq!(j.kind(), "Pod");
assert_eq!(j.api_version(), "v1");
assert_eq!(j.metadata().name.as_ref().unwrap(), "pod-example");
}
#[test]
fn typed() {
use api::core::v1::Pod;
let pod: Pod = serde_json::from_value(pod_json()).unwrap();
assert_eq!(pod.spec.containers[0].image, Some("busybox".into()));
}
}