Record element start events and report them when the event_count feature is enabled.
This commit is contained in:
42
src/event_count/database.rs
Normal file
42
src/event_count/database.rs
Normal file
@@ -0,0 +1,42 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
|
||||
use super::EventType;
|
||||
use crate::parser::OrgSource;
|
||||
|
||||
#[derive(Debug, Eq, Hash, PartialEq)]
|
||||
struct EventKey {
|
||||
event_type: EventType,
|
||||
byte_offset: usize,
|
||||
}
|
||||
|
||||
pub(crate) type EventCount = usize;
|
||||
|
||||
static GLOBAL_DATA: Mutex<Option<HashMap<EventKey, EventCount>>> = Mutex::new(None);
|
||||
|
||||
pub(crate) fn record_event(event_type: EventType, input: OrgSource<'_>) {
|
||||
let mut db = GLOBAL_DATA.lock().unwrap();
|
||||
let db = db.get_or_insert_with(HashMap::new);
|
||||
let key = EventKey {
|
||||
event_type,
|
||||
byte_offset: input.get_byte_offset(),
|
||||
};
|
||||
*db.entry(key).or_insert(0) += 1;
|
||||
}
|
||||
|
||||
pub fn report(original_document: &str) {
|
||||
let mut db = GLOBAL_DATA.lock().unwrap();
|
||||
let db = db.get_or_insert_with(HashMap::new);
|
||||
let mut results: Vec<_> = db.iter().map(|(k, v)| (k, v)).collect();
|
||||
results.sort_by_key(|(_k, v)| *v);
|
||||
// This would put the most common at the top, but that is a pain when there is already a lot of output from the parser.
|
||||
// results.sort_by(|(_ak, av), (_bk, bv)| bv.cmp(av));
|
||||
for (key, count) in results {
|
||||
println!(
|
||||
"{:?} {} character offset: {}",
|
||||
key.event_type,
|
||||
count,
|
||||
original_document[..key.byte_offset].chars().count() + 1
|
||||
)
|
||||
}
|
||||
}
|
||||
7
src/event_count/event_type.rs
Normal file
7
src/event_count/event_type.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
#[derive(Debug, Eq, Hash, PartialEq)]
|
||||
pub(crate) enum EventType {
|
||||
ElementStart,
|
||||
ElementFinish,
|
||||
ObjectStart,
|
||||
ObjectFinish,
|
||||
}
|
||||
6
src/event_count/mod.rs
Normal file
6
src/event_count/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
mod database;
|
||||
mod event_type;
|
||||
|
||||
pub(crate) use database::record_event;
|
||||
pub use database::report;
|
||||
pub(crate) use event_type::EventType;
|
||||
Reference in New Issue
Block a user