sudachi/dic/build/
report.rs1use std::time::{Duration, Instant};
18
19pub struct DictPartReport {
20 pub(self) part: String,
21 pub(self) time: Duration,
22 pub(self) size: usize,
23 pub(self) write: bool,
24}
25
26impl DictPartReport {
27 pub fn part(&self) -> &str {
28 &self.part
29 }
30
31 pub fn time(&self) -> Duration {
32 self.time
33 }
34
35 pub fn size(&self) -> usize {
36 self.size
37 }
38
39 pub fn is_write(&self) -> bool {
40 self.write
41 }
42}
43
44pub(crate) struct Reporter {
45 reports: Vec<DictPartReport>,
46}
47
48impl Reporter {
49 pub fn new() -> Reporter {
50 Self {
51 reports: Vec::with_capacity(10),
52 }
53 }
54
55 pub fn collect(&mut self, end: usize, report: ReportBuilder) {
56 let mut rep = report.report;
57 rep.time = Instant::now().duration_since(report.start);
58 rep.size = end;
59 self.reports.push(rep);
60 }
61
62 pub fn collect_r<T>(
63 &mut self,
64 end: Result<usize, T>,
65 report: ReportBuilder,
66 ) -> Result<usize, T> {
67 match end {
68 Ok(s) => {
69 self.collect(s, report);
70 Ok(s)
71 }
72 e => e,
73 }
74 }
75
76 pub fn reports(&self) -> &[DictPartReport] {
77 &self.reports
78 }
79}
80
81pub(crate) struct ReportBuilder {
82 start: Instant,
83 report: DictPartReport,
84}
85
86impl ReportBuilder {
87 pub fn new<S: Into<String>>(desc: S) -> Self {
88 Self {
89 start: Instant::now(),
90 report: DictPartReport {
91 part: desc.into(),
92 size: 0,
93 time: Duration::default(),
94 write: true,
95 },
96 }
97 }
98
99 pub fn read(mut self) -> Self {
100 self.report.write = false;
101 self
102 }
103}