Skip to content

Commit

Permalink
v0.1.4 | CLI args QoL fixes | String -> Box<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Sep 18, 2023
1 parent 46f36bd commit 0c85bdc
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 141 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "when3meet"
version = "0.1.3"
version = "0.1.4"
edition = "2021"
authors = ["Garrett Ladley <garrett.ladley14@gmail.com>"]
license = "GPL-3.0-only"
Expand Down
12 changes: 6 additions & 6 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ use url::Url;
#[command(author, version, about, long_about = None)]
pub struct Args {
/// The people required at the meeting. If not provided, assumed to be all people.
#[arg(short, long, required = false, value_parser, num_args = 1..)]
pub required_people: Option<Vec<String>>,
#[arg(short, long, value_parser, num_args = 1..)]
pub required_people: Vec<String>,

/// Flexible naming. Perform case insensitive contains based matching.
#[arg(short, long, required = false)]
pub flexible_naming: Option<bool>,
/// Perform case insensitive contains based matching on required people.
#[arg(short, long, requires("required_people"))]
pub flexible_naming: bool,

/// The URL to the when2meet page.
#[arg(short, long)]
pub when2meet_url: Url,

/// The output file path. If not provided, it will be printed to stdout.
#[arg(short, long, required = false)]
#[arg(short, long)]
pub output_file_path: Option<std::path::PathBuf>,
}
2 changes: 1 addition & 1 deletion src/fetch_availability/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub enum FetchError {
#[derive(Error, Debug, PartialEq)]
pub enum ProcessResultError {
#[error("Failed to get next part of availability matrix: {section}")]
AvailMatrixNoNext { section: String },
AvailMatrixNoNext { section: Box<str> },
#[error("Failed to parse timestamp from availability matrix: {timestamp}")]
AvailMatrixFailedTimestampParse { timestamp: String },
}
Expand Down
36 changes: 18 additions & 18 deletions src/fetch_availability/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub struct Slot {

#[derive(Debug, PartialEq)]
pub struct Person {
pub name: String,
pub name: Box<str>,
pub available: bool,
}

Expand Down Expand Up @@ -104,15 +104,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand All @@ -123,15 +123,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand All @@ -142,15 +142,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: true,
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand All @@ -171,15 +171,15 @@ mod tests {
.with_timezone(&Utc),
people: vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand All @@ -190,15 +190,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: true,
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false,
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand All @@ -219,11 +219,11 @@ mod tests {
end_time: Utc::now() + Duration::hours(2),
people: vec![
Person {
name: String::from("Muneer"),
name: "Muneer".to_string().into_boxed_str(),
available: true,
},
Person {
name: String::from("Garrett"),
name: "Garrett".to_string().into_boxed_str(),
available: false,
},
],
Expand Down
70 changes: 38 additions & 32 deletions src/fetch_availability/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ pub fn parse_when2meet(url: &Url) -> Result<Vec<Slot>, ParseWhen2MeetError> {
}

fn process_names_and_matrix(
names: Vec<String>,
avail_matrix: Vec<String>,
names: Vec<Box<str>>,
avail_matrix: Vec<Box<str>>,
) -> Result<Vec<Slot>, ProcessResultError> {
let mut slots = Vec::new();

Expand All @@ -101,7 +101,7 @@ fn process_names_and_matrix(
.iter()
.zip(parts)
.map(|(name, available)| Person {
name: name.to_string(),
name: name.to_string().into_boxed_str(),
available: available == "1",
})
.collect();
Expand Down Expand Up @@ -132,14 +132,17 @@ fn fetch_people_names(tab: &Arc<Tab>) -> Result<String, FetchError> {
Ok(raw_names)
}

fn parse_people_names_from_result(raw_names: String) -> Result<Vec<String>, ParseError> {
fn parse_people_names_from_result(raw_names: String) -> Result<Vec<Box<str>>, ParseError> {
if raw_names.len() <= 2 {
return Err(ParseError::EmptyRaw);
}

let names = &raw_names[1..raw_names.len() - 1];

Ok(names.split(',').map(|x| x.to_string()).collect())
Ok(names
.split(',')
.map(|x| x.to_string().into_boxed_str())
.collect())
}

fn fetch_avail_matrix(tab: &Arc<Tab>) -> Result<String, FetchError> {
Expand All @@ -164,14 +167,17 @@ fn fetch_avail_matrix(tab: &Arc<Tab>) -> Result<String, FetchError> {
Ok(raw_avail_matrix)
}

fn parse_avail_matrix_from_result(raw_avail_matrix: String) -> Result<Vec<String>, ParseError> {
fn parse_avail_matrix_from_result(raw_avail_matrix: String) -> Result<Vec<Box<str>>, ParseError> {
if raw_avail_matrix.len() <= 2 {
return Err(ParseError::EmptyRaw);
}

let avail_matrix = &raw_avail_matrix[1..raw_avail_matrix.len() - 1];

Ok(avail_matrix.split('|').map(|x| x.to_string()).collect())
Ok(avail_matrix
.split('|')
.map(|x| x.to_string().into_boxed_str())
.collect())
}

#[cfg(test)]
Expand All @@ -195,9 +201,9 @@ mod tests {
let avail_matrix = avail_matrix.unwrap();

assert!(avail_matrix.len() == 3);
assert!(avail_matrix[0] == "1693746000,0,0,0");
assert!(avail_matrix[1] == "1693746900,1,0,0");
assert!(avail_matrix[2] == "1693747800,0,1,0");
assert!(avail_matrix[0] == "1693746000,0,0,0".to_string().into_boxed_str());
assert!(avail_matrix[1] == "1693746900,1,0,0".to_string().into_boxed_str());
assert!(avail_matrix[2] == "1693747800,0,1,0".to_string().into_boxed_str());
}

#[test]
Expand All @@ -224,9 +230,9 @@ mod tests {
let names = names.unwrap();

assert!(names.len() == 3);
assert!(names[0] == "Muneer");
assert!(names[1] == "Brian");
assert!(names[2] == "Garrett");
assert!(names[0] == "Muneer".to_string().into_boxed_str());
assert!(names[1] == "Brian".to_string().into_boxed_str());
assert!(names[2] == "Garrett".to_string().into_boxed_str());
}

#[test]
Expand All @@ -245,15 +251,15 @@ mod tests {
#[test]
fn test_process_names_and_matrix_valid() {
let names = vec![
"Muneer".to_string(),
"Brian".to_string(),
"Garrett".to_string(),
"Muneer".to_string().into_boxed_str(),
"Brian".to_string().into_boxed_str(),
"Garrett".to_string().into_boxed_str(),
];

let avail_matrix = vec![
"1693746000,0,0,0".to_string(),
"1693746900,1,0,0".to_string(),
"1693747800,0,1,0".to_string(),
"1693746000,0,0,0".to_string().into_boxed_str(),
"1693746900,1,0,0".to_string().into_boxed_str(),
"1693747800,0,1,0".to_string().into_boxed_str(),
];

let slots = process_names_and_matrix(names, avail_matrix);
Expand All @@ -271,15 +277,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: false
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false
}
]
Expand All @@ -293,15 +299,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: true
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: false
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false
}
]
Expand All @@ -315,15 +321,15 @@ mod tests {
.with_timezone(&Utc),
vec![
Person {
name: "Muneer".to_string(),
name: "Muneer".to_string().into_boxed_str(),
available: false
},
Person {
name: "Brian".to_string(),
name: "Brian".to_string().into_boxed_str(),
available: true
},
Person {
name: "Garrett".to_string(),
name: "Garrett".to_string().into_boxed_str(),
available: false
}
]
Expand All @@ -334,12 +340,12 @@ mod tests {
#[test]
fn test_process_names_and_matrix_bad_timestamp() {
let names = vec![
"Muneer".to_string(),
"Brian".to_string(),
"Garrett".to_string(),
"Muneer".to_string().into_boxed_str(),
"Brian".to_string().into_boxed_str(),
"Garrett".to_string().into_boxed_str(),
];

let avail_matrix = vec!["".to_string()];
let avail_matrix = vec!["".to_string().into_boxed_str()];

let slots = process_names_and_matrix(names, avail_matrix);

Expand Down
6 changes: 1 addition & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use when3meet::output::write_slots;
fn main() -> Result<()> {
let args = Args::parse();
let slots = parse_when2meet(&args.when2meet_url)?;
let slots = find_opt(
&slots,
&args.required_people.unwrap_or(vec![]),
&args.flexible_naming.unwrap_or(false),
);
let slots = find_opt(&slots, &args.required_people, &args.flexible_naming);

match args.output_file_path {
Some(path) => {
Expand Down
Loading

0 comments on commit 0c85bdc

Please sign in to comment.