Skip to content

Instantly share code, notes, and snippets.

@jwilm
Created April 2, 2016 20:42
Show Gist options
  • Save jwilm/30b62746a8f9dcd910cd470e2836a7cc to your computer and use it in GitHub Desktop.
Save jwilm/30b62746a8f9dcd910cd470e2836a7cc to your computer and use it in GitHub Desktop.
pagerduty eventtype serialize
#[derive(Debug)]
enum EventType {
/// Trigger a new event
Trigger,
/// Acknowledge events cause the referenced incident to enter the acknowledged state. While an
/// incident is acknowledged, it won't generate any additional notifications, even if it
/// receives new trigger events. Your monitoring tools should send PagerDuty an acknowledge
/// event when they know someone is presently working on the problem.
Acknowledge,
/// Resolve events cause the referenced incident to enter the resolved state. Once an incident
/// is resolved, it won't generate any additional notifications. New trigger events with the
/// same incident_key as a resolved incident won't re-open the incident. Instead, a new incident
/// will be created. Your monitoring tools should send PagerDuty a resolve event when the
/// problem that caused the initial trigger event has been fixed.
Resolve,
}
impl EventType {
fn as_str(&self) -> &'static str {
match *self {
EventType::Trigger => "trigger",
EventType::Acknowledge => "acknowledge",
EventType::Resolve => "resolve",
}
}
}
#[derive(Debug)]
pub struct EventTypeParseError;
impl ::std::error::Error for EventTypeParseError {
fn cause(&self) -> Option<&::std::error::Error> {
None
}
fn description(&self) -> &str {
"provided str is not a valid EventType"
}
}
impl ::std::fmt::Display for EventTypeParseError {
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
write!(f, "provided str is not a valid EventType")
}
}
impl FromStr for EventType {
type Err = EventTypeParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"trigger" => EventType::Trigger,
"acknowledge" => EventType::Acknowledge,
"resolve" => EventType::Resolve,
_ => return Err(EventTypeParseError)
})
}
}
impl ::serde::ser::Serialize for EventType {
fn serialize<__S>(&self, serializer: &mut __S) -> Result<(), __S::Error>
where __S: ::serde::ser::Serializer
{
serializer.serialize_str(self.as_str())
}
}
impl ::serde::de::Deserialize for EventType {
/// Deserialize this value given this `Deserializer`.
fn deserialize<D>(deserializer: &mut D) -> Result<Self, D::Error>
where D: ::serde::de::Deserializer
{
use std::marker::PhantomData;
struct VariantVisitor<__D> {
_marker: PhantomData<__D>,
}
impl<__D> ::serde::de::Visitor for VariantVisitor<__D>
where __D: ::serde::de::Deserializer
{
type Value = EventType;
fn visit_str<E>(&mut self, value: &str) -> Result<EventType, E>
where E: ::serde::de::Error
{
FromStr::from_str(value).map_err(|_| E::invalid_value("not an EventType"))
}
}
deserializer.deserialize_str(VariantVisitor::<D>{ _marker: PhantomData })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment