`decoder`: Refactor path extraction
use log::{Log, Record};
+use serde::Serialize;
use serde_json::{json, Value as JsonValue};
use std::io::{self, Write};
@@ -20,7 +21,7 @@ impl Log for JsonLogger {
}
if let Some(record) = DefmtRecord::new(record) {
- let (krate, modules, function, is_method) = extract_path(record.module_path());
+ let path = self::Path::new(record.module_path());
let level = match record.is_println() {
false => record.level().as_str(),
true => "PRINTLN",
@@ -42,18 +43,14 @@ impl Log for JsonLogger {
"line": record.line(),
"column": "TODO",
},
- "path": {
- "crate": krate,
- "modules": modules,
- "function": function,
- "is_method": is_method,
- },
+ "path": path,
"target_timestamp": record.timestamp(),
}),
)
.ok();
writeln!(sink).ok();
- } else { /* TODO: handle host logs */
+ } else {
+ // TODO: handle host logs
}
}
@@ -68,30 +65,27 @@ impl JsonLogger {
}
}
-fn extract_path(
- module_path: Option<&str>,
-) -> (
- Option<String>,
- Option<Vec<String>>,
- Option<String>,
- Option<bool>,
-) {
- let module_path = if let Some(a) = module_path {
- a.split("::").collect::<Vec<_>>()
- } else {
- return (None, None, None, None);
- };
-
- let idx = module_path.len() - 1;
+#[derive(Serialize)]
+struct Path {
+ krate: String,
+ modules: Vec<String>,
+ function: String,
+}
- let krate = module_path[..1][0].to_string();
- let modules = module_path[1..idx]
- .iter()
- .map(|b| b.to_string())
- .collect::<Vec<_>>();
- let function = module_path[idx..][0].to_string();
+impl Path {
+ fn new(module_path: Option<&str>) -> Option<Self> {
+ let mut path = module_path?.split("::").collect::<Vec<_>>();
- let is_method = modules.iter().any(|m| m.starts_with("{impl#"));
+ // there need to be at least two elements, the crate and the function
+ if path.len() < 2 {
+ return None;
+ };
- (Some(krate), Some(modules), Some(function), Some(is_method))
+ let function = path.pop()?.to_string();
+ Some(Self {
+ krate: path[..1][0].to_string(),
+ modules: path[1..].iter().map(|a| a.to_string()).collect(),
+ function,
+ })
+ }
}
