Debian Codegraph @

third_party/rust/qlog/src/reader.rs

raw
1 // Copyright (C) 2023, Cloudflare, Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
19 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 use crate::QlogSeq;
28
29 /// Represents the format of the read event.
30 #[allow(clippy::large_enum_variant)]
31 #[derive(Clone, Debug)]
32 pub enum Event {
33 /// A native qlog event type.
34 Qlog(crate::events::Event),
35
36 // An extended JSON event type.
37 Json(crate::events::JsonEvent),
38 }
39
40 /// A helper object specialized for reading JSON-SEQ qlog from a [`BufRead`]
41 /// trait.
42 ///
43 /// [`BufRead`]: https://doc.rust-lang.org/std/io/trait.BufRead.html
44 pub struct QlogSeqReader<'a> {
45 pub qlog: QlogSeq,
46 reader: Box<dyn std::io::BufRead + Send + Sync + 'a>,
47 }
48
49 impl<'a> QlogSeqReader<'a> {
50 pub fn new(
51 mut reader: Box<dyn std::io::BufRead + Send + Sync + 'a>,
52 ) -> Result<Self, Box<dyn std::error::Error>> {
53 // "null record" skip it
54 Self::read_record(reader.as_mut());
55
56 let header = Self::read_record(reader.as_mut()).ok_or_else(|| {
57 std::io::Error::other("error reading file header bytes")
58 })?;
59
60 let res: Result<QlogSeq, serde_json::Error> =
61 serde_json::from_slice(&header);
62 match res {
63 Ok(qlog) => Ok(Self { qlog, reader }),
64
65 Err(e) => Err(e.into()),
66 }
67 }
68
69 fn read_record(
70 reader: &mut (dyn std::io::BufRead + Send + Sync),
71 ) -> Option<Vec<u8>> {
72 let mut buf = Vec::<u8>::new();
73 let size = reader.read_until(b'', &mut buf).unwrap();
74 if size <= 1 {
75 return None;
76 }
77
78 buf.truncate(buf.len() - 1);
79
80 Some(buf)
81 }
82 }
83
84 impl Iterator for QlogSeqReader<'_> {
85 type Item = Event;
86
87 #[inline]
88 fn next(&mut self) -> Option<Self::Item> {
89 // Attempt to deserialize events but skip them if that fails for any
90 // reason, ensuring we always read all bytes in the reader.
91 while let Some(bytes) = Self::read_record(&mut self.reader) {
92 let r: serde_json::Result<crate::events::Event> =
93 serde_json::from_slice(&bytes);
94
95 if let Ok(event) = r {
96 return Some(Event::Qlog(event));
97 }
98
99 let r: serde_json::Result<crate::events::JsonEvent> =
100 serde_json::from_slice(&bytes);
101
102 if let Ok(event) = r {
103 return Some(Event::Json(event));
104 }
105 }
106
107 None
108 }
109 }