sudachi/dic/build/
report.rs

1/*
2 *  Copyright (c) 2021 Works Applications Co., Ltd.
3 *
4 *  Licensed under the Apache License, Version 2.0 (the "License");
5 *  you may not use this file except in compliance with the License.
6 *  You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 *   Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16
17use 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}