1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::shared::*;
use std::cell::RefCell;

/// View into a borrowed log line
///
/// Unlike [`RawLogLine`](crate::raw::RawLogLine), this struct does not compute all the fields upfront,
/// but instead provides methods to access the fields on demand.
///
/// This can be useful when you need to access only one or two fields from a log line.
/// Performance gets worse if you need to access many fields;
/// cloning the iterator becomes more expensive compared to a pre-computed struct like [`RawLogLine`](crate::raw::RawLogLine).
#[derive(Debug)]
pub struct LogLineView<'a> {
    line: &'a str,
    iter: RefCell<MemchrTabSplitter<'a>>,
    prev: RefCell<usize>,
    last: RefCell<&'a str>,
}

impl<'a> LogLineView<'a> {
    /// Creates a new `RawLogLineView` from a borrowed log line
    ///
    /// The line is checked for the correct number of fields and
    /// that it's not a comment line (like the version or fields header).
    ///
    pub fn new(line: &'a str) -> Result<Self, &'static str> {
        valid_line(line)?;

        let iter = RefCell::new(MemchrTabSplitter::new(line));
        let prev = RefCell::new(0);
        let last = RefCell::new(line);

        Ok(LogLineView {
            line,
            iter,
            prev,
            last,
        })
    }

    #[inline]
    fn field(&self, index: usize) -> &'a str {
        use std::cmp::Ordering;

        let mut prev = self.prev.borrow_mut();
        let mut last = self.last.borrow_mut();

        let v = match index.cmp(&*prev) {
            Ordering::Greater => {
                let rel = index - *prev - 1;
                *prev = index;
                self.iter.borrow_mut().nth(rel).unwrap()
            }
            Ordering::Equal => *last,
            Ordering::Less => {
                let mut iter = self.iter.borrow_mut();
                *iter = split(self.line);
                *prev = index;
                iter.nth(index).unwrap()
            }
        };
        *last = v;
        v
    }

    /// Returns the date field of the log line
    ///
    /// This is the fallible version, though it should never fail,
    /// as the line has been checked for the correct number of fields
    /// when the struct was initialised
    pub fn date(&self) -> &'a str {
        self.field(0)
    }

    pub fn time(&self) -> &'a str {
        self.field(1)
    }

    pub fn x_edge_location(&self) -> &'a str {
        self.field(2)
    }

    pub fn sc_bytes(&self) -> &'a str {
        self.field(3)
    }

    pub fn c_ip(&self) -> &'a str {
        self.field(4)
    }

    pub fn cs_method(&self) -> &'a str {
        self.field(5)
    }

    pub fn cs_host(&self) -> &'a str {
        self.field(6)
    }

    pub fn cs_uri_stem(&self) -> &'a str {
        self.field(7)
    }

    pub fn sc_status(&self) -> &'a str {
        self.field(8)
    }

    pub fn cs_referer(&self) -> &'a str {
        self.field(9)
    }

    pub fn cs_user_agent(&self) -> &'a str {
        self.field(10)
    }

    pub fn cs_uri_query(&self) -> &'a str {
        self.field(11)
    }

    pub fn cs_cookie(&self) -> &'a str {
        self.field(12)
    }

    pub fn x_edge_result_type(&self) -> &'a str {
        self.field(13)
    }

    pub fn x_edge_request_id(&self) -> &'a str {
        self.field(14)
    }

    pub fn x_host_header(&self) -> &'a str {
        self.field(15)
    }

    pub fn cs_protocol(&self) -> &'a str {
        self.field(16)
    }

    pub fn cs_bytes(&self) -> &'a str {
        self.field(17)
    }

    pub fn time_taken(&self) -> &'a str {
        self.field(18)
    }

    pub fn x_forwarded_for(&self) -> &'a str {
        self.field(19)
    }

    pub fn ssl_protocol(&self) -> &'a str {
        self.field(20)
    }

    pub fn ssl_cipher(&self) -> &'a str {
        self.field(21)
    }

    pub fn x_edge_response_result_type(&self) -> &'a str {
        self.field(22)
    }

    pub fn cs_protocol_version(&self) -> &'a str {
        self.field(23)
    }

    pub fn fle_status(&self) -> &'a str {
        self.field(24)
    }

    pub fn fle_encrypted_fields(&self) -> &'a str {
        self.field(25)
    }

    pub fn c_port(&self) -> &'a str {
        self.field(26)
    }

    pub fn time_to_first_byte(&self) -> &'a str {
        self.field(27)
    }

    pub fn x_edge_detailed_result_type(&self) -> &'a str {
        self.field(28)
    }

    pub fn sc_content_type(&self) -> &'a str {
        self.field(29)
    }

    pub fn sc_content_len(&self) -> &'a str {
        self.field(30)
    }

    pub fn sc_range_start(&self) -> &'a str {
        self.field(31)
    }

    pub fn sc_range_end(&self) -> &'a str {
        self.field(32)
    }
}