-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
use super::super::command::*; | ||
|
||
#[non_exhaustive] | ||
#[derive(Deserialize, Serialize)] | ||
#[serde(untagged)] | ||
pub enum CommandConfigInput { | ||
Command(String), | ||
ProgramAndArgs(Vec<String>), | ||
CommandConfig(CommandConfig), | ||
} | ||
|
||
impl Into<CommandConfig> for CommandConfigInput { | ||
fn into(self) -> CommandConfig { | ||
match self { | ||
CommandConfigInput::Command(script) => CommandConfig::from_script(&script), | ||
CommandConfigInput::ProgramAndArgs(mut names) => { | ||
let program = if names.is_empty() { | ||
String::new() | ||
} else { | ||
names.remove(0) | ||
}; | ||
CommandConfig::from_program_args( | ||
program, | ||
if names.is_empty() { None } else { Some(names) }, | ||
) | ||
} | ||
CommandConfigInput::CommandConfig(config) => config, | ||
} | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
#[derive(Deserialize, Serialize)] | ||
#[serde(untagged)] | ||
pub enum CommandConfigsInput { | ||
Commands(Vec<CommandConfigInput>), | ||
LabeledCommands(HashMap<String, Option<CommandConfigInput>>), | ||
} | ||
|
||
impl Into<Vec<CommandConfig>> for CommandConfigsInput { | ||
fn into(self) -> Vec<CommandConfig> { | ||
match self { | ||
CommandConfigsInput::Commands(commands) => { | ||
commands.into_iter().map(Into::into).collect() | ||
} | ||
CommandConfigsInput::LabeledCommands(map) => map | ||
.into_iter() | ||
.map(|(label, command)| match command { | ||
Some(command) => { | ||
let mut command: CommandConfig = command.into(); | ||
command.label = Some(label); | ||
|
||
command | ||
} | ||
None => CommandConfig::from_program_args(label, None), | ||
}) | ||
.collect(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod command; | ||
mod run; | ||
|
||
pub use command::*; | ||
pub use run::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::cmp; | ||
|
||
use super::super::{run::*, CommandConfig}; | ||
use super::CommandConfigsInput; | ||
|
||
#[non_exhaustive] | ||
#[derive(Deserialize, Serialize)] | ||
pub struct RunConfigInput { | ||
pub commands: CommandConfigsInput, | ||
pub max_label_length: Option<usize>, | ||
} | ||
|
||
impl Into<RunConfig> for RunConfigInput { | ||
fn into(self) -> RunConfig { | ||
let Self { | ||
commands, | ||
max_label_length, | ||
} = self; | ||
|
||
let commands: Vec<CommandConfig> = commands.into(); | ||
|
||
let real_max_label_length = commands | ||
.iter() | ||
.map(|cmd| cmd.label_length()) | ||
.max() | ||
.unwrap_or(0); | ||
|
||
let max_label_length = match max_label_length { | ||
Some(0) | None => real_max_label_length, | ||
Some(v) => cmp::min(v, real_max_label_length), | ||
}; | ||
|
||
RunConfig { | ||
commands, | ||
max_label_length, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
mod command; | ||
mod run; | ||
mod input; | ||
pub use command::*; | ||
pub use run::*; | ||
pub use input::*; |