Skip to content

Commit

Permalink
impl serde for AnyStep
Browse files Browse the repository at this point in the history
  • Loading branch information
ssddOnTop committed Nov 7, 2024
1 parent 5572ce8 commit 1b46809
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions workspace/gh-workflow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod error;
mod rust_flag;
mod toolchain;
pub(crate) mod workflow;
mod serde;

pub use rust_flag::*;
pub use toolchain::*;
pub use workflow::*;
37 changes: 37 additions & 0 deletions workspace/gh-workflow/src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use crate::{AnyStep, Run, Step, Use};

impl Serialize for AnyStep {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
AnyStep::Run(step) => step.serialize(serializer),
AnyStep::Use(step) => step.serialize(serializer),
}
}
}

impl<'de> Deserialize<'de> for AnyStep {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
// Deserialize the input as a generic map first
let map = serde_yaml::Value::deserialize(deserializer)?;

// Check if the map contains the `run` or `uses` fields
if map.get("run").is_some() {
// If `run` is defined, deserialize it as `Step<Run>`
let step_run: Step<Run> = serde_yaml::from_value(map).map_err(de::Error::custom)?;
Ok(AnyStep::Run(step_run))
} else if map.get("uses").is_some() {
// If `uses` is defined, deserialize it as `Step<Use>`
let step_use: Step<Use> = serde_yaml::from_value(map).map_err(de::Error::custom)?;
Ok(AnyStep::Use(step_use))
} else {
Err(de::Error::custom("Expected either `run` or `uses` field"))
}
}
}
2 changes: 1 addition & 1 deletion workspace/gh-workflow/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ pub enum OneOrMany<T> {
Multiple(Vec<T>),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnyStep {
Run(Step<Run>),
Use(Step<Use>),
Expand Down

0 comments on commit 1b46809

Please sign in to comment.