// 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 WorkflowRunStepBlockType {
    StartDocument,
    StartJson,
    Note,
    Parse,
    Edit,
    Extract,
    Split,
    Classifier,
    Conditional,
    ApiCall,
    Function,
    WhileLoop,
    ForEach,
    MergeDicts,
    WhileLoopSentinelStart,
    WhileLoopSentinelEnd,
    ForEachSentinelStart,
    ForEachSentinelEnd,
    /// 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 WorkflowRunStepBlockType {
    /// 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::StartDocument => "start_document",
            Self::StartJson => "start_json",
            Self::Note => "note",
            Self::Parse => "parse",
            Self::Edit => "edit",
            Self::Extract => "extract",
            Self::Split => "split",
            Self::Classifier => "classifier",
            Self::Conditional => "conditional",
            Self::ApiCall => "api_call",
            Self::Function => "function",
            Self::WhileLoop => "while_loop",
            Self::ForEach => "for_each",
            Self::MergeDicts => "merge_dicts",
            Self::WhileLoopSentinelStart => "while_loop_sentinel_start",
            Self::WhileLoopSentinelEnd => "while_loop_sentinel_end",
            Self::ForEachSentinelStart => "for_each_sentinel_start",
            Self::ForEachSentinelEnd => "for_each_sentinel_end",
            Self::Unknown(s) => s.as_str(),
        }
    }
}

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

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

impl FromStr for WorkflowRunStepBlockType {
    type Err = std::convert::Infallible;
    #[allow(deprecated)]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(match s {
            "start_document" => Self::StartDocument,
            "start_json" => Self::StartJson,
            "note" => Self::Note,
            "parse" => Self::Parse,
            "edit" => Self::Edit,
            "extract" => Self::Extract,
            "split" => Self::Split,
            "classifier" => Self::Classifier,
            "conditional" => Self::Conditional,
            "api_call" => Self::ApiCall,
            "function" => Self::Function,
            "while_loop" => Self::WhileLoop,
            "for_each" => Self::ForEach,
            "merge_dicts" => Self::MergeDicts,
            "while_loop_sentinel_start" => Self::WhileLoopSentinelStart,
            "while_loop_sentinel_end" => Self::WhileLoopSentinelEnd,
            "for_each_sentinel_start" => Self::ForEachSentinelStart,
            "for_each_sentinel_end" => Self::ForEachSentinelEnd,
            other => Self::Unknown(other.to_string()),
        })
    }
}

impl From<String> for WorkflowRunStepBlockType {
    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 WorkflowRunStepBlockType {
    fn from(s: &str) -> Self {
        Self::from_str(s).unwrap_or_else(|_| Self::Unknown(s.to_string()))
    }
}

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

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