use wasm_bindgen::prelude::*;
use serde_json::Value;
use crate::{parsers, query, LogEntry};
#[derive(serde::Serialize)]
struct WasmResult {
is_match: bool,
parsed_log: Option<Value>, error: Option<String>, }
#[wasm_bindgen]
pub fn run_query(log_line: &str, query: &str) -> String {
let entry = parsers::parse_log_line(log_line);
match entry {
LogEntry::Structured(value) => {
match query::evaluate(&value, log_line, query) {
Ok(is_match) => {
let result = WasmResult {
is_match,
parsed_log: Some(value),
error: None,
};
serde_json::to_string(&result).unwrap_or_default()
},
Err(e) => {
let result = WasmResult {
is_match: false,
parsed_log: Some(value), error: Some(format!("Query Error: {}", e)),
};
serde_json::to_string(&result).unwrap_or_default()
}
}
},
LogEntry::Unstructured(_) => {
let result = WasmResult {
is_match: false,
parsed_log: None,
error: Some("Could not parse log structure. Is it valid JSON or Logfmt?".to_string()),
};
serde_json::to_string(&result).unwrap_or_default()
}
}
}