Skip to content

Commit

Permalink
feat(lib): config inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
EqualMa committed Sep 4, 2021
1 parent 779ffb2 commit 0f3dc3c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
63 changes: 63 additions & 0 deletions runcc/src/config/input/command.rs
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(),
}
}
}
5 changes: 5 additions & 0 deletions runcc/src/config/input/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod command;
mod run;

pub use command::*;
pub use run::*;
39 changes: 39 additions & 0 deletions runcc/src/config/input/run.rs
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,
}
}
}
2 changes: 2 additions & 0 deletions runcc/src/config/mod.rs
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::*;

0 comments on commit 0f3dc3c

Please sign in to comment.