|
1 | | -/* |
2 | | -* MIT License |
3 | | -* |
4 | | -* Copyright (c) 2024 Firelink Data |
5 | | -* |
6 | | -* Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | | -* of this software and associated documentation files (the "Software"), to deal |
8 | | -* in the Software without restriction, including without limitation the rights |
9 | | -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10 | | -* copies of the Software, and to permit persons to whom the Software is |
11 | | -* furnished to do so, subject to the following conditions: |
12 | | -* |
13 | | -* The above copyright notice and this permission notice shall be included in all |
14 | | -* copies or substantial portions of the Software. |
15 | | -* |
16 | | -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19 | | -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21 | | -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22 | | -* SOFTWARE. |
23 | | -* |
24 | | -* File created: 2024-05-08 |
25 | | -* Last updated: 2024-05-09 |
26 | | -*/ |
| 1 | +// |
| 2 | +// MIT License |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Firelink Data |
| 5 | +// |
| 6 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | +// of this software and associated documentation files (the "Software"), to deal |
| 8 | +// in the Software without restriction, including without limitation the rights |
| 9 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | +// copies of the Software, and to permit persons to whom the Software is |
| 11 | +// furnished to do so, subject to the following conditions: |
| 12 | +// |
| 13 | +// The above copyright notice and this permission notice shall be included in all |
| 14 | +// copies or substantial portions of the Software. |
| 15 | +// |
| 16 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | +// SOFTWARE. |
| 23 | +// |
| 24 | +// File created: 2024-05-08 |
| 25 | +// Last updated: 2024-05-10 |
| 26 | +// |
27 | 27 |
|
| 28 | +use half::f16; |
28 | 29 | use padder::{Alignment, Symbol}; |
29 | 30 |
|
30 | 31 | use std::str::from_utf8; |
@@ -64,3 +65,275 @@ impl BooleanParser { |
64 | 65 | } |
65 | 66 | } |
66 | 67 | } |
| 68 | + |
| 69 | +/// |
| 70 | +#[derive(Debug)] |
| 71 | +pub(crate) struct Float16Parser { |
| 72 | + alignment: Alignment, |
| 73 | + trim_symbol: Symbol, |
| 74 | +} |
| 75 | + |
| 76 | +/// |
| 77 | +impl Float16Parser { |
| 78 | + /// |
| 79 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 80 | + Self { |
| 81 | + alignment, |
| 82 | + trim_symbol, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// |
| 87 | + pub fn parse(&self, bytes: &[u8]) -> Result<f16> { |
| 88 | + let text: &str = from_utf8(bytes)?; |
| 89 | + let trimmed: &str = self.trim(text); |
| 90 | + Ok(trimmed.parse::<f16>()?) |
| 91 | + } |
| 92 | + |
| 93 | + /// |
| 94 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 95 | + match self.alignment { |
| 96 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 97 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 98 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +/// |
| 104 | +#[derive(Debug)] |
| 105 | +pub(crate) struct Float32Parser { |
| 106 | + alignment: Alignment, |
| 107 | + trim_symbol: Symbol, |
| 108 | +} |
| 109 | + |
| 110 | +/// |
| 111 | +impl Float32Parser { |
| 112 | + /// |
| 113 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 114 | + Self { |
| 115 | + alignment, |
| 116 | + trim_symbol, |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + /// |
| 121 | + pub fn parse(&self, bytes: &[u8]) -> Result<f32> { |
| 122 | + let text: &str = from_utf8(bytes)?; |
| 123 | + let trimmed: &str = self.trim(text); |
| 124 | + Ok(trimmed.parse::<f32>()?) |
| 125 | + } |
| 126 | + |
| 127 | + /// |
| 128 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 129 | + match self.alignment { |
| 130 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 131 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 132 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/// |
| 138 | +#[derive(Debug)] |
| 139 | +pub(crate) struct Float64Parser { |
| 140 | + alignment: Alignment, |
| 141 | + trim_symbol: Symbol, |
| 142 | +} |
| 143 | + |
| 144 | +/// |
| 145 | +impl Float64Parser { |
| 146 | + /// |
| 147 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 148 | + Self { |
| 149 | + alignment, |
| 150 | + trim_symbol, |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /// |
| 155 | + pub fn parse(&self, bytes: &[u8]) -> Result<f64> { |
| 156 | + let text: &str = from_utf8(bytes)?; |
| 157 | + let trimmed: &str = self.trim(text); |
| 158 | + Ok(trimmed.parse::<f64>()?) |
| 159 | + } |
| 160 | + |
| 161 | + /// |
| 162 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 163 | + match self.alignment { |
| 164 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 165 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 166 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 167 | + } |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/// |
| 172 | +#[derive(Debug)] |
| 173 | +pub(crate) struct Int16Parser { |
| 174 | + alignment: Alignment, |
| 175 | + trim_symbol: Symbol, |
| 176 | +} |
| 177 | + |
| 178 | +/// |
| 179 | +impl Int16Parser { |
| 180 | + /// |
| 181 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 182 | + Self { |
| 183 | + alignment, |
| 184 | + trim_symbol, |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + /// |
| 189 | + pub fn parse(&self, bytes: &[u8]) -> Result<i16> { |
| 190 | + let text: &str = from_utf8(bytes)?; |
| 191 | + let trimmed: &str = self.trim(text); |
| 192 | + Ok(trimmed.parse::<i16>()?) |
| 193 | + } |
| 194 | + |
| 195 | + /// |
| 196 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 197 | + match self.alignment { |
| 198 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 199 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 200 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 201 | + } |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +/// |
| 206 | +#[derive(Debug)] |
| 207 | +pub(crate) struct Int32Parser { |
| 208 | + alignment: Alignment, |
| 209 | + trim_symbol: Symbol, |
| 210 | +} |
| 211 | + |
| 212 | +/// |
| 213 | +impl Int32Parser { |
| 214 | + /// |
| 215 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 216 | + Self { |
| 217 | + alignment, |
| 218 | + trim_symbol, |
| 219 | + } |
| 220 | + } |
| 221 | + |
| 222 | + /// |
| 223 | + pub fn parse(&self, bytes: &[u8]) -> Result<i32> { |
| 224 | + let text: &str = from_utf8(bytes)?; |
| 225 | + let trimmed: &str = self.trim(text); |
| 226 | + Ok(trimmed.parse::<i32>()?) |
| 227 | + } |
| 228 | + |
| 229 | + /// |
| 230 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 231 | + match self.alignment { |
| 232 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 233 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 234 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 235 | + } |
| 236 | + } |
| 237 | +} |
| 238 | + |
| 239 | +/// |
| 240 | +#[derive(Debug)] |
| 241 | +pub(crate) struct Int64Parser { |
| 242 | + alignment: Alignment, |
| 243 | + trim_symbol: Symbol, |
| 244 | +} |
| 245 | + |
| 246 | +/// |
| 247 | +impl Int64Parser { |
| 248 | + /// |
| 249 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 250 | + Self { |
| 251 | + alignment, |
| 252 | + trim_symbol, |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + /// |
| 257 | + pub fn parse(&self, bytes: &[u8]) -> Result<i64> { |
| 258 | + let text: &str = from_utf8(bytes)?; |
| 259 | + let trimmed: &str = self.trim(text); |
| 260 | + Ok(trimmed.parse::<i64>()?) |
| 261 | + } |
| 262 | + |
| 263 | + /// |
| 264 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 265 | + match self.alignment { |
| 266 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 267 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 268 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 269 | + } |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +/// |
| 274 | +#[derive(Debug)] |
| 275 | +pub(crate) struct Utf8Parser { |
| 276 | + alignment: Alignment, |
| 277 | + trim_symbol: Symbol, |
| 278 | +} |
| 279 | + |
| 280 | +/// |
| 281 | +impl Utf8Parser { |
| 282 | + /// |
| 283 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 284 | + Self { |
| 285 | + alignment, |
| 286 | + trim_symbol, |
| 287 | + } |
| 288 | + } |
| 289 | + |
| 290 | + /// |
| 291 | + pub fn parse<'a>(&self, bytes: &'a [u8]) -> Result<&'a str> { |
| 292 | + let text: &str = from_utf8(bytes)?; |
| 293 | + let trimmed: &str = self.trim(text); |
| 294 | + Ok(trimmed) |
| 295 | + } |
| 296 | + |
| 297 | + /// |
| 298 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 299 | + match self.alignment { |
| 300 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 301 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 302 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 303 | + } |
| 304 | + } |
| 305 | +} |
| 306 | + |
| 307 | +/// |
| 308 | +#[derive(Debug)] |
| 309 | +pub(crate) struct LargeUtf8Parser { |
| 310 | + alignment: Alignment, |
| 311 | + trim_symbol: Symbol, |
| 312 | +} |
| 313 | + |
| 314 | +/// |
| 315 | +impl LargeUtf8Parser { |
| 316 | + /// |
| 317 | + pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self { |
| 318 | + Self { |
| 319 | + alignment, |
| 320 | + trim_symbol, |
| 321 | + } |
| 322 | + } |
| 323 | + |
| 324 | + /// |
| 325 | + pub fn parse<'a>(&self, bytes: &'a [u8]) -> Result<&'a str> { |
| 326 | + let text: &str = from_utf8(bytes)?; |
| 327 | + let trimmed: &str = self.trim(text); |
| 328 | + Ok(trimmed) |
| 329 | + } |
| 330 | + |
| 331 | + /// |
| 332 | + pub fn trim<'a>(&self, text: &'a str) -> &'a str { |
| 333 | + match self.alignment { |
| 334 | + Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()), |
| 335 | + Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()), |
| 336 | + Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()), |
| 337 | + } |
| 338 | + } |
| 339 | +} |
0 commit comments