1use chrono::NaiveDateTime as DateTime;
4use enum_as_inner::EnumAsInner;
5
6pub mod log;
7pub mod world;
8
9pub use log::Log;
10pub use world::{Instance, InstanceLog, InstanceLogList};
11
12#[derive(Debug, EnumAsInner)]
13pub enum LogEnum {
14 Log(Log),
15 Warning { date: DateTime, msg: Vec<String> },
16 Error { date: DateTime, msg: Vec<String> },
17 Exception { date: DateTime, msg: Vec<String> },
18 Unknown(String),
19}
20
21pub fn from_str(s: &str) -> Result<Vec<LogEnum>, ()> {
22 let mut ret = Vec::<LogEnum>::new();
23 for log in s.split("\n\r\n") {
24 let log = LogEnum::from_str(log);
25
26 if let Ok(log) = log {
27 ret.push(log);
29 }
30 }
31
32 Ok(ret)
33}
34
35impl LogEnum {
36 pub fn from_str(s: &str) -> Result<Self, ()> {
37 if s.chars().nth(31) != Some('-') {
38 return Err(());
40 }
41
42 let date = s[0..19].to_string();
43 let date = DateTime::parse_from_str(&date, "%Y.%m.%d %H:%M:%S").unwrap();
44 let typ = &s[20..31].split_whitespace().next().unwrap();
47 let content = &s[34..];
48
49 let (mtyp, msg) = if content.chars().nth(0) == Some('[') {
50 let n = content.find("]").unwrap();
51 (Some(&content[1..n]), &content[n + 2..])
52 } else {
53 (None, content)
54 };
55
56 let mut msg: Vec<String> = msg
57 .lines()
58 .map(|s| s.trim_start_matches(' ').to_string())
59 .collect();
60 msg.retain(|s| !s.is_empty());
61 let msg = msg;
62
63 let log = match typ {
64 &"Log" => LogEnum::Log(Log::new(date, mtyp, msg)),
65 &"Warning" => LogEnum::Warning { date, msg },
66 &"Error" => LogEnum::Error { date, msg },
67 &"Exception" => LogEnum::Exception { date, msg },
68 _ => LogEnum::Unknown(typ.to_string()),
69 };
70
71 Ok(log)
72 }
73}