/
2_order_expect.rs
58 lines (47 loc) · 1.61 KB
/
2_order_expect.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use chrono::NaiveDate;
use std::{env, fmt, fs};
#[derive(Debug)]
struct UserCommand {
product: String,
quantity: u32,
delivery_date: NaiveDate,
}
fn main() {
let file_name = env::args()
.nth(1)
.expect("Please provide a file name as a command-line argument.");
let content = fs::read_to_string(&file_name).expect("Error reading the file");
let mut commands: Vec<UserCommand> = Vec::new();
for line in content.lines() {
let mut parts = line.split_whitespace();
let product = parts
.next()
.expect("Missing product information")
.to_string();
let quant = parts.next().expect("Missing quantity information");
let quantity = quant
.trim()
.parse::<u32>()
.expect("Invalid quantity format, expecting integer");
let d_date = parts.next().expect("Missing delivery date information");
let delivery_date = NaiveDate::parse_from_str(d_date.trim(), "%d.%m.%Y")
.expect("Invalid date format, should be %d.%m.%Y");
let command = UserCommand {
product,
quantity,
delivery_date,
};
commands.push(command);
}
println!("\nYour command was processed and it is ready for delivery. The ordered items:\n");
commands.iter().for_each(|cmd| println!(" * {} ", cmd));
}
impl fmt::Display for UserCommand {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{} {} - to be delivered on {}",
self.quantity, self.product, self.delivery_date
)
}
}