corewars_core/load_file/
metadata.rs1use std::fmt;
5
6#[derive(Debug, Default, PartialEq)]
8pub struct Metadata {
9 pub redcode: Option<String>,
12
13 pub name: Option<String>,
15
16 pub author: Option<String>,
18
19 pub date: Option<String>,
21
22 pub version: Option<String>,
24
25 pub strategy: Option<String>,
28
29 pub assertion: Option<String>,
31}
32
33impl Metadata {
34 pub fn parse_line(&mut self, line: &str) -> String {
37 let split_line: Vec<&str> = line.splitn(2, ';').map(|p| p.trim()).collect();
38
39 if split_line.len() > 1 {
40 let split_comment: Vec<&str> = split_line[1].splitn(2, char::is_whitespace).collect();
41 let value = Some(
42 split_comment
43 .get(1)
44 .map_or_else(String::new, |s| s.trim().to_owned()),
45 );
46
47 let directive = split_comment[0].to_lowercase();
48 match directive.as_ref() {
49 "redcode" => self.redcode = value,
50 "name" => self.name = value,
51 "author" => self.author = value,
52 "date" => self.date = value,
53 "version" => self.version = value,
54 "strategy" => self.strategy = value,
55 "assert" => self.assertion = value,
56 _ => (),
57 }
58 }
59
60 split_line[0].trim().to_string()
61 }
62}
63
64impl fmt::Display for Metadata {
65 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
66 for (field, name) in &[
67 (&self.redcode, "redcode"),
68 (&self.name, "name"),
69 (&self.author, "author"),
70 (&self.version, "version"),
71 (&self.date, "date"),
72 (&self.strategy, "strategy"),
73 (&self.assertion, "assert"),
74 ] {
75 if let Some(value) = field.as_deref() {
76 if value.is_empty() {
77 writeln!(formatter, ";{}", name)?;
78 } else {
79 writeln!(formatter, ";{} {}", name, value)?;
80 }
81 }
82 }
83 Ok(())
84 }
85}
86
87