-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample.rs
50 lines (42 loc) · 1.28 KB
/
example.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
/// A struct for the example
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct AStruct {
/// an integer
integer: i32,
/// a char
character: char,
}
/// * `n` - a number, used as a size
/// * `list` - a list of structs
fn example(n: i32, list: Vec<AStruct>) {
/* TODO In a real life scenario, you will describe here what you want the
end user to do with this generated code */
}
fn main() {
let mut buffer = String::new();
let n = read_line(&mut buffer)
.parse()
.expect("invalid `N` parameter");
let list = (0..n)
.map(|_| read_line(&mut buffer).parse())
.collect::<Result<_, _>>()
.expect("invalid `list` parameter");
example(n, list);
}
fn read_line(buffer: &mut String) -> &str {
buffer.clear();
std::io::stdin()
.read_line(buffer)
.expect("impossible to read a new line");
buffer.trim_end()
}
impl std::str::FromStr for AStruct {
type Err = Box<dyn std::error::Error>;
fn from_str(line: &str) -> Result<Self, Self::Err> {
let mut line = line.split_whitespace();
Ok(Self {
integer: line.next().ok_or("missing `integer`")?.parse()?,
character: line.next().ok_or("missing `character`")?.parse()?,
})
}
}