Verified Commit a99419b8 authored by Ole Martin Ruud's avatar Ole Martin Ruud
Browse files

Improve way of selecting specific options

parent 18aa87f4
......@@ -217,18 +217,6 @@ impl App {
.for_each(|area| eprintln!(" - {} ({})", area.name, area.id));
eprintln!("#############");
}
if option_type == OptionType::Buildings || option_type == OptionType::All {
eprintln!("BUILDINGS:");
options.buildings.iter().for_each(|building| {
eprintln!(
" - {} ({}) [{}]",
building.name,
building.id,
building.area.as_ref().unwrap_or(&"-".into()),
)
});
eprintln!("#############");
}
if option_type == OptionType::RoomTypes || option_type == OptionType::All {
eprintln!("ROOMTYPES:");
options
......@@ -245,6 +233,53 @@ impl App {
.for_each(|equipment| eprintln!(" - {} ({})", equipment.name, equipment.id));
eprintln!("#############");
}
if let OptionType::Buildings { in_area } = option_type {
let area_opt = in_area.and_then(|in_area| {
options
.areas
.iter()
.find(|area| area.id == in_area)
.or_else(|| {
error!(
"Invalid area code {}, defaulting to all buildings",
&in_area
);
None
})
});
if let Some(area) = area_opt {
eprintln!("BUILDINGS IN AREA {} ({}):", area.name, area.id);
options
.buildings
.iter()
.filter(|building| building.area.as_ref() == Some(&area.id))
.for_each(|building| eprintln!(" - {} ({})", building.name, building.id));
eprintln!("#############");
} else {
eprintln!("BUILDINGS:");
options.buildings.iter().for_each(|building| {
eprintln!(
" - {} ({}) [{}]",
building.name,
building.id,
building.area.as_ref().unwrap_or(&"-".into())
)
});
eprintln!("#############");
}
} else if option_type == OptionType::All {
eprintln!("BUILDINGS:");
options.buildings.iter().for_each(|building| {
eprintln!(
" - {} ({}) [{}]",
building.name,
building.id,
building.area.as_ref().unwrap_or(&"-".into())
)
});
eprintln!("#############");
}
Ok(())
}
......
......@@ -85,13 +85,25 @@ fn parse_hour(s: &str) -> chrono::ParseResult<chrono::NaiveTime> {
chrono::NaiveTime::parse_from_str(s, HOUR_FORMAT)
}
#[derive(Copy, Clone, Debug, PartialEq, EnumString, IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
#[derive(StructOpt, Clone, PartialEq, Debug)]
#[structopt(rename_all = "snake_case")]
pub enum OptionType {
#[structopt(about = "Show area options")]
Areas,
Buildings,
#[structopt(about = "Show building options")]
Buildings {
#[structopt(
short = "a",
long,
help = "Only show buildings in the following area code"
)]
in_area: Option<String>,
},
#[structopt(about = "Show equipment options")]
Equipment,
#[structopt(about = "Show room type options")]
RoomTypes,
#[structopt(about = "Show all options")]
All,
}
......@@ -106,7 +118,7 @@ enum Command {
help = "Ensures that options are fetched regardless of existing cache"
)]
fresh: bool,
#[structopt(help = "The type of option to query", default_value = OptionType::All.into())]
#[structopt(subcommand)]
option_type: OptionType,
},
#[structopt(about = "Query the service for avaliable rooms")]
......@@ -166,8 +178,8 @@ fn run() -> Result<()> {
let app = App { config, client };
use crate::Command::*;
match &opts.command {
Options { fresh, option_type } => app.options(*fresh, *option_type),
match opts.command {
Options { fresh, option_type } => app.options(fresh, option_type),
Query {
building,
area,
......@@ -226,15 +238,15 @@ fn run() -> Result<()> {
.day(day.format(DATE_FORMAT).to_string())
.hour(hour.format(HOUR_FORMAT).to_string())
.duration(duration.format(HOUR_FORMAT).to_string())
.room(room)
.room(&room)
.build()
.expect("Unreachable: Builder has all required fields specified");
app.order_room(name, extra_notes.as_ref().map(|s| &**s), &order_room)?;
app.order_room(&name, extra_notes.as_ref().map(|s| &**s), &order_room)?;
eprintln!(
"Successfully ordered room {} at {} {} for {}",
room,
&room,
hour.format(HOUR_FORMAT),
day.format(DATE_FORMAT),
duration.format(HOUR_FORMAT)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment