diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-09-22 23:02:42 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-09-23 23:44:14 +0200 |
commit | f406f507387af20fbc9b1c2612e3f6219a6907c9 (patch) | |
tree | 8776b509ec5c9554a0bb0aa6803e694279130a52 | |
parent | 88ef3e8312f5062cc923d000046bc9a8c58d6b6e (diff) | |
download | rebel-f406f507387af20fbc9b1c2612e3f6219a6907c9.tar rebel-f406f507387af20fbc9b1c2612e3f6219a6907c9.zip |
Store task arguments as an enum
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/args.rs | 42 | ||||
-rw-r--r-- | src/context.rs | 33 | ||||
-rw-r--r-- | src/executor.rs | 17 | ||||
-rw-r--r-- | src/task.rs | 10 |
6 files changed, 79 insertions, 36 deletions
@@ -246,6 +246,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" [[package]] +name = "enum-kinds" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e40a16955681d469ab3da85aaa6b42ff656b3c67b52e1d8d3dd36afe97fd462" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "fake-simd" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -781,6 +792,7 @@ name = "rebel" version = "0.1.0" dependencies = [ "clap", + "enum-kinds", "handlebars", "hex", "ipc-channel", @@ -9,6 +9,7 @@ edition = "2018" [dependencies] clap = "3.0.0-beta.2" +enum-kinds = "0.5.1" handlebars = "4.1.3" hex = { version = "0.4.3", features = ["std", "serde"] } ipc-channel = { git = "https://github.com/servo/ipc-channel.git" } diff --git a/src/args.rs b/src/args.rs index fcc0791..c42059b 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,15 +1,34 @@ -use serde::{Deserialize, Serialize}; use std::{collections::HashMap, hash, rc::Rc}; -#[derive(Debug, Serialize)] -pub struct Platform<'a> { - pub gnu_triplet: &'a str, - pub karch: &'a str, - pub prefix: &'a str, +use enum_kinds::EnumKind; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, PartialEq, Eq, EnumKind)] +#[serde(untagged)] +#[enum_kind(ArgType, derive(Deserialize), serde(rename_all = "snake_case"))] +pub enum Arg { + String(String), + Platform { + gnu_triplet: String, + karch: String, + prefix: String, + }, +} + +impl From<&str> for Arg { + fn from(value: &str) -> Self { + Arg::String(value.to_string()) + } +} + +impl From<String> for Arg { + fn from(value: String) -> Self { + Arg::String(value) + } } -#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)] -pub struct TaskArgs(pub HashMap<String, Rc<serde_json::Value>>); +#[derive(Clone, Debug, Serialize, PartialEq, Eq)] +pub struct TaskArgs(pub HashMap<String, Rc<Arg>>); impl hash::Hash for TaskArgs { fn hash<H: hash::Hasher>(&self, _state: &mut H) { @@ -19,9 +38,6 @@ impl hash::Hash for TaskArgs { } } -pub fn arg<T: Serialize>(key: &str, value: T) -> (String, Rc<serde_json::Value>) { - ( - key.to_string(), - Rc::new(serde_json::to_value(value).unwrap()), - ) +pub fn arg<A: Into<Arg>>(key: &str, value: A) -> (String, Rc<Arg>) { + (key.to_string(), Rc::new(value.into())) } diff --git a/src/context.rs b/src/context.rs index bf4f8c4..59c9f8a 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,7 +10,7 @@ use std::{ use serde::Serialize; use crate::{ - args::{self, arg, TaskArgs}, + args::{arg, Arg, TaskArgs}, task::*, types::TaskID, }; @@ -37,29 +37,28 @@ pub struct OutputRef<'ctx> { pub output: &'ctx str, } -const BUILD_PLATFORM: args::Platform = args::Platform { - gnu_triplet: "x86_64-linux-gnu", - karch: "x86", - prefix: "/opt/toolchain", -}; -const HOST_PLATFORM: args::Platform = args::Platform { - gnu_triplet: "aarch64-linux-gnu", - karch: "arm64", - prefix: "/usr", -}; - #[derive(Debug)] pub struct Context { tasks: HashMap<TaskID, TaskDef>, - default_args: HashMap<String, Rc<serde_json::Value>>, + default_args: HashMap<String, Rc<Arg>>, } impl Context { pub fn new(tasks: HashMap<TaskID, TaskDef>) -> Self { - let default_args = [arg("build", BUILD_PLATFORM), arg("host", HOST_PLATFORM)] - .iter() - .cloned() - .collect(); + let build_platform: Arg = Arg::Platform { + gnu_triplet: "x86_64-linux-gnu".to_string(), + karch: "x86".to_string(), + prefix: "/opt/toolchain".to_string(), + }; + let host_platform: Arg = Arg::Platform { + gnu_triplet: "aarch64-linux-gnu".to_string(), + karch: "arm64".to_string(), + prefix: "/usr".to_string(), + }; + + let default_args = + IntoIterator::into_iter([arg("build", build_platform), arg("host", host_platform)]) + .collect(); Context { tasks, diff --git a/src/executor.rs b/src/executor.rs index 156bf0f..cc7e089 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -27,7 +27,7 @@ fn input_hash(task: &runner::TaskInput) -> InputHash { #[derive(Clone, Debug, Deserialize, Serialize)] struct TaskMeta { pub id: TaskID, - pub args: TaskArgs, + pub args: HashMap<String, serde_json::Value>, pub inherit: Vec<InputHash>, pub depends: HashMap<DependencyHash, Dependency>, pub input_hash: InputHash, @@ -69,7 +69,13 @@ pub struct Executor<'ctx> { impl<'ctx> Executor<'ctx> { pub fn new(ctx: &'ctx Context, taskset: HashSet<TaskRef<'ctx>>) -> Result<Self> { - let base_args = TaskArgs(BASE_ARGS.iter().map(|(k, v)| args::arg(k, v)).collect()); + let base_args = TaskArgs( + BASE_ARGS + .iter() + .copied() + .map(|(k, v)| args::arg(k, v)) + .collect(), + ); let mut exc = Executor { ctx, @@ -201,7 +207,12 @@ impl<'ctx> Executor<'ctx> { let meta = TaskMeta { id: task_ref.id.clone(), - args: task_ref.args.clone(), + args: task_ref + .args + .0 + .iter() + .map(|(k, v)| (k.clone(), serde_json::to_value(v).unwrap())) + .collect(), inherit: task.input.inherit, depends: task.input.depends, input_hash, diff --git a/src/task.rs b/src/task.rs index 85ff620..6428985 100644 --- a/src/task.rs +++ b/src/task.rs @@ -1,7 +1,11 @@ -use serde::Deserialize; use std::collections::{HashMap, HashSet}; -use crate::types::{StringHash, TaskID}; +use serde::Deserialize; + +use crate::{ + args::ArgType, + types::{StringHash, TaskID}, +}; #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Hash)] pub struct Fetch { @@ -40,7 +44,7 @@ pub struct Action { #[derive(Clone, Debug, Deserialize)] pub struct TaskDef { #[serde(default)] - pub args: HashMap<String, String>, + pub args: HashMap<String, ArgType>, #[serde(default)] pub inherit: Option<InheritDep>, #[serde(default)] |