Skip to content

Commit

Permalink
rename traits
Browse files Browse the repository at this point in the history
  • Loading branch information
tusharmath committed Nov 6, 2024
1 parent 9710db5 commit a88dc1a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions workspace/gh-workflow-gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fn main() {
let nightly = Job::new("Nightly")
.add_step(Step::uses("actions", "checkout", 4).name("Checkout Code"))
.add_step(
// TODO: Rust Tool Chain can be a separate struct
Step::uses("actions-rust-lang", "setup-rust-toolchain", 1)
.name("Setup Rust Toolchain")
.with(("toolchain", Version::Nightly)),
Expand Down
20 changes: 17 additions & 3 deletions workspace/gh-workflow/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub struct Step<T> {
pub uses: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[setters(skip)]
pub with: Option<IndexMap<String, Value>>,
with: Option<IndexMap<String, Value>>,
#[serde(skip_serializing_if = "Option::is_none")]
#[setters(skip)]
pub run: Option<String>,
Expand Down Expand Up @@ -394,7 +394,7 @@ impl Step<Use> {
}
}

pub fn with<K: SetEnv<Self>>(self, item: K) -> Self {
pub fn with<K: SetInput>(self, item: K) -> Self {
item.apply(self)
}

Expand All @@ -403,7 +403,7 @@ impl Step<Use> {
}
}

impl SetEnv<Step<Use>> for IndexMap<String, Value> {
impl SetInput for IndexMap<String, Value> {
fn apply(self, mut step: Step<Use>) -> Step<Use> {
let mut with = step.with.unwrap_or_default();
with.extend(self);
Expand All @@ -412,6 +412,15 @@ impl SetEnv<Step<Use>> for IndexMap<String, Value> {
}
}

impl<S1: Display, S2: Display> SetInput for (S1, S2) {
fn apply(self, mut step: Step<Use>) -> Step<Use> {
let mut with = step.with.unwrap_or_default();
with.insert(self.0.to_string(), Value::String(self.1.to_string()));
step.with = Some(with);
step
}
}

impl<S1: Display, S2: Display> SetEnv<Job> for (S1, S2) {
fn apply(self, mut value: Job) -> Job {
let mut index_map: IndexMap<String, String> = value.env.unwrap_or_default();
Expand Down Expand Up @@ -476,6 +485,11 @@ pub trait SetEvent {
fn apply(self, workflow: Workflow) -> Workflow;
}

/// Sets the input for a Step that uses another action
pub trait SetInput {
fn apply(self, step: Step<Use>) -> Step<Use>;
}

impl<S1: Display, S2: Display> SetEnv<Step<Use>> for (S1, S2) {
fn apply(self, mut step: Step<Use>) -> Step<Use> {
let mut index_map: IndexMap<String, Value> = step.with.unwrap_or_default();
Expand Down

0 comments on commit a88dc1a

Please sign in to comment.