use crate::{
api::metadata::{ListMeta, Meta, ObjectMeta, TypeMeta},
error::ErrorResponse,
};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
#[derive(Deserialize, Serialize, Clone)]
#[serde(tag = "type", content = "object", rename_all = "UPPERCASE")]
pub enum WatchEvent<K>
where
K: Clone + Meta,
{
Added(K),
Modified(K),
Deleted(K),
Bookmark(K),
Error(ErrorResponse),
}
impl<K> Debug for WatchEvent<K>
where
K: Clone + Meta,
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match &self {
WatchEvent::Added(_) => write!(f, "Added event"),
WatchEvent::Modified(_) => write!(f, "Modified event"),
WatchEvent::Deleted(_) => write!(f, "Deleted event"),
WatchEvent::Bookmark(_) => write!(f, "Bookmark event"),
WatchEvent::Error(e) => write!(f, "Error event: {:?}", e),
}
}
}
#[derive(Deserialize, Serialize, Clone)]
pub struct Object<P, U>
where
P: Clone,
U: Clone,
{
#[serde(flatten)]
pub types: TypeMeta,
pub metadata: ObjectMeta,
pub spec: P,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<U>,
}
impl<P, U> Object<P, U>
where
P: Clone,
U: Clone,
{
pub fn new<K: k8s_openapi::Resource>(name: &str, spec: P) -> Self {
Self {
types: TypeMeta {
api_version: <K as k8s_openapi::Resource>::API_VERSION.to_string(),
kind: <K as k8s_openapi::Resource>::KIND.to_string(),
},
metadata: ObjectMeta {
name: Some(name.to_string()),
..Default::default()
},
spec,
status: None,
}
}
}
#[derive(Deserialize, Debug)]
pub struct ObjectList<T>
where
T: Clone,
{
pub metadata: ListMeta,
#[serde(bound(deserialize = "Vec<T>: Deserialize<'de>"))]
pub items: Vec<T>,
}
impl<T: Clone> ObjectList<T> {
pub fn iter<'a>(&'a self) -> impl Iterator<Item = &T> + 'a {
self.items.iter()
}
pub fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &mut T> + 'a {
self.items.iter_mut()
}
}
impl<T: Clone> IntoIterator for ObjectList<T> {
type IntoIter = ::std::vec::IntoIter<Self::Item>;
type Item = T;
fn into_iter(self) -> Self::IntoIter {
self.items.into_iter()
}
}
impl<'a, T: Clone> IntoIterator for &'a ObjectList<T> {
type IntoIter = ::std::slice::Iter<'a, T>;
type Item = &'a T;
fn into_iter(self) -> Self::IntoIter {
self.items.iter()
}
}
impl<'a, T: Clone> IntoIterator for &'a mut ObjectList<T> {
type IntoIter = ::std::slice::IterMut<'a, T>;
type Item = &'a mut T;
fn into_iter(self) -> Self::IntoIter {
self.items.iter_mut()
}
}