Skip to content

Commit

Permalink
fix: Improve type-safety for Step
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 11, 2024
1 parent b1e06f4 commit 790765a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 24 deletions.
6 changes: 3 additions & 3 deletions workspace/gh-workflow/src/cargo.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use derive_setters::Setters;

use crate::toolchain::Version;
use crate::StepValue;
use crate::{Run, Step};

#[derive(Setters)]
#[setters(strip_option, into)]
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Cargo {
}
}

impl From<Cargo> for StepValue {
impl From<Cargo> for Step<Run> {
fn from(value: Cargo) -> Self {
let mut command = vec!["cargo".to_string()];

Expand All @@ -55,7 +55,7 @@ impl From<Cargo> for StepValue {
}
}

let mut step = StepValue::run(command.join(" "));
let mut step = Step::run(command.join(" "));

if let Some(id) = value.id {
step = step.id(id);
Expand Down
6 changes: 5 additions & 1 deletion workspace/gh-workflow/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Event {
// TODO: add all more events
}

#[derive(Debug, Setters, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub struct Push {
branches: Vec<String>,
Expand All @@ -26,6 +26,10 @@ impl Push {
self.branches.push(branch.to_string());
self
}

pub fn branches(self, branches: Vec<String>) -> Self {
Push { branches }
}
}

impl From<Push> for Event {
Expand Down
4 changes: 4 additions & 0 deletions workspace/gh-workflow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ pub use cargo::*;
pub use event::*;
pub use rust_flag::*;
pub use workflow::*;

mod private {
pub trait Sealed {}
}
8 changes: 4 additions & 4 deletions workspace/gh-workflow/src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::{Display, Formatter};

use derive_setters::Setters;

use crate::{Input, RustFlags, StepValue};
use crate::{Input, RustFlags, Step, Use};

#[derive(Clone)]
pub enum Version {
Expand Down Expand Up @@ -191,10 +191,10 @@ impl Toolchain {
}
}

impl From<Toolchain> for StepValue {
impl From<Toolchain> for Step<Use> {
fn from(value: Toolchain) -> Self {
let mut step = StepValue::uses("actions-rust-lang", "setup-rust-toolchain", 1)
.name("Setup Rust Toolchain");
let mut step =
Step::uses("actions-rust-lang", "setup-rust-toolchain", 1).name("Setup Rust Toolchain");

let toolchain = value
.toolchain
Expand Down
57 changes: 41 additions & 16 deletions workspace/gh-workflow/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_json::Value;

use crate::error::Result;
use crate::generate::Generate;
use crate::Event;
use crate::{private, Event};

#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(transparent)]
Expand Down Expand Up @@ -175,15 +175,17 @@ impl Job {
}
}

pub fn add_step<S: Into<StepValue>>(mut self, step: S) -> Self {
pub fn add_step<S: Into<Step<T>>, T: StepType>(mut self, step: S) -> Self {
let mut steps = self.steps.unwrap_or_default();
steps.push(step.into());
let step: Step<T> = step.into();
let step: StepValue = T::to_value(step);
steps.push(step);
self.steps = Some(steps);
self
}
}

#[derive(Debug, Setters, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(transparent)]
pub struct Step<A> {
value: StepValue,
Expand All @@ -209,6 +211,26 @@ pub struct Use;
#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub struct Run;

/// A trait to convert Step<Run> and Step<Use> to StepValue.
pub trait StepType: Sized + private::Sealed {
fn to_value(s: Step<Self>) -> StepValue;
}

impl private::Sealed for Run {}
impl private::Sealed for Use {}

impl StepType for Run {
fn to_value(s: Step<Self>) -> StepValue {
s.into()
}
}

impl StepType for Use {
fn to_value(s: Step<Self>) -> StepValue {
s.into()
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Default)]
#[serde(transparent)]
pub struct Env(IndexMap<String, Value>);
Expand Down Expand Up @@ -297,18 +319,6 @@ impl StepValue {
..Default::default()
}
}

pub fn add_with<I: Into<Input>>(mut self, new_with: I) -> Self {
let mut with = self.with.unwrap_or_default();
with.merge(new_with.into());
if with.0.is_empty() {
self.with = None;
} else {
self.with = Some(with);
}

self
}
}

impl Step<Run> {
Expand All @@ -325,9 +335,24 @@ impl Step<Use> {
}
}

/// Creates a step pointing to the default Github's Checkout Action
/// See: <https://github.com/actions/checkout>
pub fn checkout() -> Step<Use> {
Step::uses("actions", "checkout", 4).name("Checkout Code")
}

/// Add a new input to the step.
pub fn add_with<I: Into<Input>>(mut self, new_with: I) -> Self {
let mut with = self.value.with.unwrap_or_default();
with.merge(new_with.into());
if with.0.is_empty() {
self.value.with = None;
} else {
self.value.with = Some(with);
}

self
}
}

impl<S1: ToString, S2: ToString> From<(S1, S2)> for Input {
Expand Down

0 comments on commit 790765a

Please sign in to comment.