#
k8s_openapi/v1_35/apimachinery/pkg/apis/meta/v1/
watch_event.rs

    1// Generated from definition io.k8s.apimachinery.pkg.apis.meta.v1.WatchEvent
    2
    3/// Event represents a single event to a watched resource.
    4///
    5/// Object is:
    6///  * If Type is Added or Modified: the new state of the object.
    7///  * If Type is Deleted: the state of the object immediately before deletion.
    8///  * If Type is Error: *Status is recommended; other types may make sense
    9///    depending on context.
    10#[derive(Clone, Debug, PartialEq)]
    11pub enum WatchEvent<T> {
    12    Added(T),
    13    Deleted(T),
    14    Modified(T),
    15    Bookmark {
    16        annotations: std::collections::BTreeMap<std::string::String, std::string::String>,
    17        resource_version: std::string::String,
    18    },
    19    ErrorStatus(crate::apimachinery::pkg::apis::meta::v1::Status),
    20    ErrorOther(crate::apimachinery::pkg::runtime::RawExtension),
    21}
    22
    23impl<'de, T> crate::serde::Deserialize<'de> for WatchEvent<T> where T: crate::serde::Deserialize<'de> {
    24    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    25        #[allow(non_camel_case_types)]
    26        enum Field {
    27            Key_type,
    28            Key_object,
    29            Other,
    30        }
    31
    32        impl<'de> crate::serde::Deserialize<'de> for Field {
    33            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    34                struct Visitor;
    35
    36                impl crate::serde::de::Visitor<'_> for Visitor {
    37                    type Value = Field;
    38
    39                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    40                        f.write_str("field identifier")
    41                    }
    42
    43                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    44                        Ok(match v {
    45                            "type" => Field::Key_type,
    46                            "object" => Field::Key_object,
    47                            _ => Field::Other,
    48                        })
    49                    }
    50                }
    51
    52                deserializer.deserialize_identifier(Visitor)
    53            }
    54        }
    55
    56        enum WatchEventType {
    57            Added,
    58            Deleted,
    59            Modified,
    60            Bookmark,
    61            Error,
    62        }
    63
    64        impl<'de> crate::serde::Deserialize<'de> for WatchEventType {
    65            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    66                struct Visitor;
    67
    68                impl crate::serde::de::Visitor<'_> for Visitor {
    69                    type Value = WatchEventType;
    70
    71                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    72                        f.write_str("watch event type")
    73                    }
    74
    75                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    76                        Ok(match v {
    77                            "ADDED" => WatchEventType::Added,
    78                            "DELETED" => WatchEventType::Deleted,
    79                            "MODIFIED" => WatchEventType::Modified,
    80                            "BOOKMARK" => WatchEventType::Bookmark,
    81                            "ERROR" => WatchEventType::Error,
    82                            _ => return Err(crate::serde::de::Error::unknown_variant(
    83                                v,
    84                                &["ADDED", "DELETED", "MODIFIED", "BOOKMARK", "ERROR"],
    85                            )),
    86                        })
    87                    }
    88                }
    89
    90                deserializer.deserialize_identifier(Visitor)
    91            }
    92        }
    93
    94        struct Visitor<T>(core::marker::PhantomData<T>);
    95
    96        impl<'de, T> crate::serde::de::Visitor<'de> for Visitor<T> where T: crate::serde::Deserialize<'de> {
    97            type Value = WatchEvent<T>;
    98
    99            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    100                f.write_str("WatchEvent")
    101            }
    102
    103            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
    104                let mut value_type: Option<WatchEventType> = None;
    105                let mut value_object: Option<crate::serde_json::Value> = None;
    106
    107                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    108                    match key {
    109                        Field::Key_type => value_type = crate::serde::de::MapAccess::next_value(&mut map)?,
    110                        Field::Key_object => value_object = crate::serde::de::MapAccess::next_value(&mut map)?,
    111                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
    112                    }
    113                }
    114
    115                let value_type = value_type.ok_or_else(|| crate::serde::de::Error::missing_field("type"))?;
    116                let value_object = value_object.ok_or_else(|| crate::serde::de::Error::missing_field("object"))?;
    117
    118                Ok(match value_type {
    119                    WatchEventType::Added => {
    120                        WatchEvent::Added(crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?)
    121                    },
    122                    WatchEventType::Deleted => {
    123                        WatchEvent::Deleted(crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?)
    124                    },
    125                    WatchEventType::Modified => {
    126                        WatchEvent::Modified(crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?)
    127                    },
    128                    WatchEventType::Bookmark => {
    129                        let value: BookmarkObject<'static> = crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?;
    130                        WatchEvent::Bookmark {
    131                            annotations: value.metadata.annotations.into_owned(),
    132                            resource_version: value.metadata.resource_version.into_owned(),
    133                        }
    134                    },
    135                    WatchEventType::Error => {
    136                        if value_object.as_object().is_some_and(|object| object.get("kind").is_some_and(|kind| kind == "Status")) {
    137                            WatchEvent::ErrorStatus(crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?)
    138                        }
    139                        else {
    140                            WatchEvent::ErrorOther(crate::serde::Deserialize::deserialize(value_object).map_err(crate::serde::de::Error::custom)?)
    141                        }
    142                    },
    143                })
    144            }
    145        }
    146
    147        deserializer.deserialize_struct(
    148            "WatchEvent",
    149            &[
    150                "type",
    151                "object",
    152            ],
    153            Visitor(Default::default()),
    154        )
    155    }
    156}
    157
    158impl<T> crate::serde::Serialize for WatchEvent<T> where T: crate::serde::Serialize {
    159    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    160        let mut state = serializer.serialize_struct(
    161            "WatchEvent",
    162            2,
    163        )?;
    164        match self {
    165            WatchEvent::Added(object) => {
    166                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "ADDED")?;
    167                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    168            },
    169            WatchEvent::Deleted(object) => {
    170                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "DELETED")?;
    171                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    172            },
    173            WatchEvent::Modified(object) => {
    174                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "MODIFIED")?;
    175                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    176            },
    177            WatchEvent::Bookmark { annotations, resource_version } => {
    178                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "BOOKMARK")?;
    179                let object = BookmarkObject {
    180                    metadata: BookmarkObjectMeta {
    181                        annotations: std::borrow::Cow::Borrowed(annotations),
    182                        resource_version: std::borrow::Cow::Borrowed(&**resource_version),
    183                    },
    184                };
    185                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    186            },
    187            WatchEvent::ErrorStatus(object) => {
    188                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "ERROR")?;
    189                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    190            },
    191            WatchEvent::ErrorOther(object) => {
    192                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "type", "ERROR")?;
    193                crate::serde::ser::SerializeStruct::serialize_field(&mut state, "object", &object)?;
    194            },
    195        }
    196        crate::serde::ser::SerializeStruct::end(state)
    197    }
    198}
    199
    200#[derive(Debug, PartialEq)]
    201struct BookmarkObject<'a> {
    202    metadata: BookmarkObjectMeta<'a>,
    203}
    204
    205#[derive(Debug, PartialEq)]
    206struct BookmarkObjectMeta<'a> {
    207    annotations: std::borrow::Cow<'a, std::collections::BTreeMap<std::string::String, std::string::String>>,
    208    resource_version: std::borrow::Cow<'a, str>,
    209}
    210
    211impl<'de> crate::serde::Deserialize<'de> for BookmarkObject<'static> {
    212    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    213        #[allow(non_camel_case_types)]
    214        enum Field {
    215            Key_metadata,
    216            Other,
    217        }
    218
    219        impl<'de> crate::serde::Deserialize<'de> for Field {
    220            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    221                struct Visitor;
    222
    223                impl crate::serde::de::Visitor<'_> for Visitor {
    224                    type Value = Field;
    225
    226                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    227                        f.write_str("field identifier")
    228                    }
    229
    230                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    231                        Ok(match v {
    232                            "metadata" => Field::Key_metadata,
    233                            _ => Field::Other,
    234                        })
    235                    }
    236                }
    237
    238                deserializer.deserialize_identifier(Visitor)
    239            }
    240        }
    241
    242        struct Visitor;
    243
    244        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
    245            type Value = BookmarkObject<'static>;
    246
    247            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    248                f.write_str("BookmarkObject")
    249            }
    250
    251            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
    252                let mut value_metadata: Option<BookmarkObjectMeta<'static>> = None;
    253
    254                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    255                    match key {
    256                        Field::Key_metadata => value_metadata = crate::serde::de::MapAccess::next_value(&mut map)?,
    257                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
    258                    }
    259                }
    260
    261                Ok(BookmarkObject {
    262                    metadata: value_metadata.ok_or_else(|| crate::serde::de::Error::missing_field("metadata"))?,
    263                })
    264            }
    265        }
    266
    267        deserializer.deserialize_struct(
    268            "BookmarkObject",
    269            &[
    270                "annotations",
    271                "metadata",
    272            ],
    273            Visitor,
    274        )
    275    }
    276}
    277
    278impl<'de> crate::serde::Deserialize<'de> for BookmarkObjectMeta<'static> {
    279    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    280        #[allow(non_camel_case_types)]
    281        enum Field {
    282            Key_annotations,
    283            Key_resource_version,
    284            Other,
    285        }
    286
    287        impl<'de> crate::serde::Deserialize<'de> for Field {
    288            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: crate::serde::Deserializer<'de> {
    289                struct Visitor;
    290
    291                impl crate::serde::de::Visitor<'_> for Visitor {
    292                    type Value = Field;
    293
    294                    fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    295                        f.write_str("field identifier")
    296                    }
    297
    298                    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> where E: crate::serde::de::Error {
    299                        Ok(match v {
    300                            "annotations" => Field::Key_annotations,
    301                            "resourceVersion" => Field::Key_resource_version,
    302                            _ => Field::Other,
    303                        })
    304                    }
    305                }
    306
    307                deserializer.deserialize_identifier(Visitor)
    308            }
    309        }
    310
    311        struct Visitor;
    312
    313        impl<'de> crate::serde::de::Visitor<'de> for Visitor {
    314            type Value = BookmarkObjectMeta<'static>;
    315
    316            fn expecting(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    317                f.write_str("ObjectMeta")
    318            }
    319
    320            fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> where A: crate::serde::de::MapAccess<'de> {
    321                let mut value_annotations: Option<std::collections::BTreeMap<std::string::String, std::string::String>> = None;
    322                let mut value_resource_version: Option<std::string::String> = None;
    323
    324                while let Some(key) = crate::serde::de::MapAccess::next_key::<Field>(&mut map)? {
    325                    match key {
    326                        Field::Key_annotations => value_annotations = crate::serde::de::MapAccess::next_value(&mut map)?,
    327                        Field::Key_resource_version => value_resource_version = crate::serde::de::MapAccess::next_value(&mut map)?,
    328                        Field::Other => { let _: crate::serde::de::IgnoredAny = crate::serde::de::MapAccess::next_value(&mut map)?; },
    329                    }
    330                }
    331
    332                Ok(BookmarkObjectMeta {
    333                    annotations: std::borrow::Cow::Owned(value_annotations.unwrap_or_default()),
    334                    resource_version: std::borrow::Cow::Owned(value_resource_version.ok_or_else(|| crate::serde::de::Error::missing_field("resourceVersion"))?),
    335                })
    336            }
    337        }
    338
    339        deserializer.deserialize_struct(
    340            "ObjectMeta",
    341            &[
    342                "annotations",
    343                "resourceVersion",
    344            ],
    345            Visitor,
    346        )
    347    }
    348}
    349
    350impl crate::serde::Serialize for BookmarkObject<'_> {
    351    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    352        let mut state = serializer.serialize_struct(
    353            "BookmarkObject",
    354            1,
    355        )?;
    356        crate::serde::ser::SerializeStruct::serialize_field(&mut state, "metadata", &self.metadata)?;
    357        crate::serde::ser::SerializeStruct::end(state)
    358    }
    359}
    360
    361impl crate::serde::Serialize for BookmarkObjectMeta<'_> {
    362    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: crate::serde::Serializer {
    363        let mut state = serializer.serialize_struct(
    364            "ObjectMeta",
    365            2,
    366        )?;
    367        crate::serde::ser::SerializeStruct::serialize_field(&mut state, "annotations", &self.annotations)?;
    368        crate::serde::ser::SerializeStruct::serialize_field(&mut state, "resourceVersion", &self.resource_version)?;
    369        crate::serde::ser::SerializeStruct::end(state)
    370    }
    371}
    372
    373
    374#[cfg(feature = "schemars")]
    375impl<T> crate::schemars::JsonSchema for WatchEvent<T> {
    376    fn schema_name() -> std::borrow::Cow<'static, str> {
    377        "io.k8s.apimachinery.pkg.apis.meta.v1.WatchEvent".into()
    378    }
    379
    380    fn json_schema(__gen: &mut crate::schemars::SchemaGenerator) -> crate::schemars::Schema {
    381        crate::schemars::json_schema!({
    382            "description": "Event represents a single event to a watched resource.\n\nObject is:\n * If Type is Added or Modified: the new state of the object.\n * If Type is Deleted: the state of the object immediately before deletion.\n * If Type is Error: *Status is recommended; other types may make sense\n   depending on context.\n",
    383            "type": "object",
    384            "properties": {
    385                "object": (__gen.subschema_for::<crate::apimachinery::pkg::runtime::RawExtension>()),
    386                "type": {
    387                    "type": "string",
    388                },
    389            },
    390            "required": [
    391                "object",
    392                "type",
    393            ],
    394        })
    395    }
    396}