freeswitch_log_parser/fields/
kind.rs1use std::fmt;
5use std::ops::Range;
6
7#[non_exhaustive]
13#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14pub enum FieldKind {
15 ChannelName,
17 CallerIdName,
19 CallerIdNumber,
21 DestinationNumber,
23 CallId,
25 Uuid,
27 SipUri,
31 IpAddr,
34}
35
36impl FieldKind {
37 pub fn label(&self) -> &'static str {
39 match self {
40 FieldKind::ChannelName => "channel-name",
41 FieldKind::CallerIdName => "caller-id-name",
42 FieldKind::CallerIdNumber => "caller-id-number",
43 FieldKind::DestinationNumber => "destination-number",
44 FieldKind::CallId => "call-id",
45 FieldKind::Uuid => "uuid",
46 FieldKind::SipUri => "sip-uri",
47 FieldKind::IpAddr => "ip-addr",
48 }
49 }
50}
51
52impl fmt::Display for FieldKind {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 f.pad(self.label())
55 }
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
64pub enum FieldLocation {
65 Message,
67 Attached(usize),
69}
70
71#[derive(Debug, Clone, PartialEq, Eq)]
75pub struct Field {
76 pub kind: FieldKind,
77 pub at: FieldLocation,
78 pub range: Range<usize>,
79}
80
81#[non_exhaustive]
86#[derive(Debug, Clone, PartialEq, Eq)]
87pub enum RenderError {
88 OutOfBounds {
90 at: FieldLocation,
91 range: Range<usize>,
92 len: usize,
93 },
94 NotOnCharBoundary {
96 at: FieldLocation,
97 range: Range<usize>,
98 },
99 OverlappingSpans {
102 at: FieldLocation,
103 first: Range<usize>,
104 second: Range<usize>,
105 },
106}
107
108impl fmt::Display for RenderError {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 match self {
111 RenderError::OutOfBounds { at, range, len } => write!(
112 f,
113 "span {}..{} is past the end of {at} ({len} bytes)",
114 range.start, range.end
115 ),
116 RenderError::NotOnCharBoundary { at, range } => write!(
117 f,
118 "span {}..{} splits a character in {at}",
119 range.start, range.end
120 ),
121 RenderError::OverlappingSpans { at, first, second } => write!(
122 f,
123 "replaced spans {}..{} and {}..{} partially overlap in {at}",
124 first.start, first.end, second.start, second.end
125 ),
126 }
127 }
128}
129
130impl std::error::Error for RenderError {}
131
132impl fmt::Display for FieldLocation {
133 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134 match self {
135 FieldLocation::Message => f.pad("the message"),
136 FieldLocation::Attached(i) => write!(f, "attached line {i}"),
137 }
138 }
139}
140
141#[derive(Debug, Clone, PartialEq, Eq)]
143pub struct RenderedEntry {
144 pub message: String,
145 pub attached: Vec<String>,
146}
147pub(super) fn kind_rank(kind: FieldKind) -> u8 {
150 match kind {
151 FieldKind::ChannelName => 0,
152 FieldKind::CallerIdName => 1,
153 FieldKind::CallerIdNumber => 2,
154 FieldKind::DestinationNumber => 3,
155 FieldKind::CallId => 4,
156 FieldKind::SipUri => 5,
157 FieldKind::IpAddr => 6,
158 FieldKind::Uuid => 7,
159 }
160}