// Code generated by oagen. DO NOT EDIT.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorTerminalStage {
    InputCollection,
    RegistryLookup,
    DocumentFetch,
    Execution,
    OutputStorage,
    Routing,
    HistoryPayload,
    /// Wire value not recognized by this SDK version. The original
    /// string is preserved verbatim. WorkOS may add new enum values
    /// server-side; matching on this variant lets callers handle
    /// forward-compatible values without panicking.
    Unknown(String),
}

impl ErrorTerminalStage {
    /// Canonical wire string for this value. For [`Self::Unknown`] returns the
    /// original wire value as received from the API.
    #[allow(deprecated)]
    pub fn as_str(&self) -> &str {
        match self {
            Self::InputCollection => "input_collection",
            Self::RegistryLookup => "registry_lookup",
            Self::DocumentFetch => "document_fetch",
            Self::Execution => "execution",
            Self::OutputStorage => "output_storage",
            Self::Routing => "routing",
            Self::HistoryPayload => "history_payload",
            Self::Unknown(s) => s.as_str(),
        }
    }
}

impl fmt::Display for ErrorTerminalStage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl AsRef<str> for ErrorTerminalStage {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl FromStr for ErrorTerminalStage {
    type Err = std::convert::Infallible;
    #[allow(deprecated)]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "input_collection" => Self::InputCollection,
            "registry_lookup" => Self::RegistryLookup,
            "document_fetch" => Self::DocumentFetch,
            "execution" => Self::Execution,
            "output_storage" => Self::OutputStorage,
            "routing" => Self::Routing,
            "history_payload" => Self::HistoryPayload,
            other => Self::Unknown(other.to_string()),
        })
    }
}

impl From<String> for ErrorTerminalStage {
    fn from(s: String) -> Self {
        // Reuse the original `String` allocation in the fallback branch.
        match Self::from_str(&s) {
            Ok(Self::Unknown(_)) => Self::Unknown(s),
            Ok(other) => other,
        }
    }
}

impl From<&str> for ErrorTerminalStage {
    fn from(s: &str) -> Self {
        Self::from_str(s).unwrap_or_else(|_| Self::Unknown(s.to_string()))
    }
}

impl Serialize for ErrorTerminalStage {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for ErrorTerminalStage {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = String::deserialize(deserializer)?;
        Ok(Self::from(s))
    }
}
