From 42e667a5086fa4a6557ef7a64e40b833a93f3124 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 2 Apr 2024 22:41:05 +0200 Subject: driver: parse: introduce TaskID and TaskArgs struct Make the parser code a bit clearer by not passing around tuples of strs. --- crates/driver/src/context.rs | 8 ++++---- crates/driver/src/parse.rs | 30 ++++++++++++++++++------------ 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/crates/driver/src/context.rs b/crates/driver/src/context.rs index 9674e5f..47259b3 100644 --- a/crates/driver/src/context.rs +++ b/crates/driver/src/context.rs @@ -357,8 +357,8 @@ impl Context { pub fn parse(&self, s: &str) -> error::Result<(TaskRef, TaskFlags)> { let (parsed, flags) = parse::parse_task_with_flags(s).context("Invalid task syntax")?; - let recipe = parsed.recipe.to_string(); - let task = parsed.task.to_string(); + let recipe = parsed.id.recipe.to_string(); + let task = parsed.id.task.to_string(); let id = TaskID { recipe, task }; let (ctx_id, _) = self @@ -368,7 +368,7 @@ impl Context { let mut args = self.globals.clone(); - if let Some(host) = parsed.host { + if let Some(host) = parsed.args.host { let plat = self .platforms .get(host) @@ -376,7 +376,7 @@ impl Context { args.set("host", Some(plat)); args.set("target", Some(plat)); } - if let Some(target) = parsed.target { + if let Some(target) = parsed.args.target { let plat = self .platforms .get(target) diff --git a/crates/driver/src/parse.rs b/crates/driver/src/parse.rs index aad9360..df540fc 100644 --- a/crates/driver/src/parse.rs +++ b/crates/driver/src/parse.rs @@ -6,13 +6,23 @@ use nom::{ }; #[derive(Debug, Clone, Copy)] -pub struct Task<'a> { +pub struct TaskID<'a> { pub recipe: &'a str, pub task: &'a str, +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct TaskArgs<'a> { pub host: Option<&'a str>, pub target: Option<&'a str>, } +#[derive(Debug, Clone, Copy)] +pub struct Task<'a> { + pub id: TaskID<'a>, + pub args: TaskArgs<'a>, +} + #[derive(Debug, Clone, Copy)] pub struct TaskFlags { pub force_run: bool, @@ -26,11 +36,11 @@ fn name(input: &str) -> IResult<&str, &str> { take_while1(is_name_char)(input) } -fn task_id(input: &str) -> IResult<&str, (&str, &str)> { +fn task_id(input: &str) -> IResult<&str, TaskID> { let (input, recipe) = name(input)?; let (input, _) = tag(":")(input)?; let (input, task) = name(input)?; - Ok((input, (recipe, task))) + Ok((input, TaskID { recipe, task })) } fn task_arg_target(input: &str) -> IResult<&str, &str> { @@ -39,27 +49,23 @@ fn task_arg_target(input: &str) -> IResult<&str, &str> { Ok((input, target)) } -fn task_args(input: &str) -> IResult<&str, (Option<&str>, Option<&str>)> { +fn task_args(input: &str) -> IResult<&str, TaskArgs> { let (input, _) = tag("/")(input)?; let (input, host) = opt(name)(input)?; let (input, target) = opt(task_arg_target)(input)?; - Ok((input, (host, target))) + Ok((input, TaskArgs { host, target })) } fn task(input: &str) -> IResult<&str, Task> { - let (input, (recipe, task)) = task_id(input)?; + let (input, id) = task_id(input)?; let (input, args) = opt(task_args)(input)?; - let (host, target) = args.unwrap_or_default(); - Ok(( input, Task { - recipe, - task, - host, - target, + id, + args: args.unwrap_or_default(), }, )) } -- cgit v1.2.3