1use std::collections::HashMap;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum Error {
6 #[error("No Key Given")]
7 NoKeyGiven,
8}
9
10pub fn interpret(code: &str) -> Result<HashMap<&str, &str>, Error> {
11 let mut hashmap: HashMap<&str, &str> = HashMap::new();
12 for line in code.lines() {
13 if line.is_empty() {
14 continue;
15 };
16 match line.split_once(':') {
17 Some((key, value)) => {
18 if key.is_empty() {
19 return Err(Error::NoKeyGiven);
20 }
21
22 hashmap.insert(key, value);
23 }
24
25 None => {
26 return Err(Error::NoKeyGiven);
27 }
28 };
29 }
30
31 Ok(hashmap)
32}
33
34#[cfg(test)]
35mod tests {
36 use crate::interpret;
37
38 #[test]
39 fn parsing_works() {
40 let config =
41 interpret("rust_is_awesome:true\ndeno_is_uncool:false\nname_of_cute_crab:Ferris!")
42 .unwrap();
43
44 assert_eq!(config.get("rust_is_awesome").unwrap(), &"true");
45 assert_eq!(config.get("deno_is_uncool").unwrap(), &"false");
46 assert_eq!(config.get("name_of_cute_crab").unwrap(), &"Ferris!");
47 }
48}