Skip to content

Commit

Permalink
[merge] parser impls
Browse files Browse the repository at this point in the history
  • Loading branch information
wilhelmagren authored May 10, 2024
1 parent f8d2558 commit f57b783
Showing 1 changed file with 299 additions and 26 deletions.
325 changes: 299 additions & 26 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
/*
* MIT License
*
* Copyright (c) 2024 Firelink Data
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* File created: 2024-05-08
* Last updated: 2024-05-09
*/
//
// MIT License
//
// Copyright (c) 2024 Firelink Data
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// File created: 2024-05-08
// Last updated: 2024-05-10
//

use half::f16;
use padder::{Alignment, Symbol};

use std::str::from_utf8;
Expand Down Expand Up @@ -64,3 +65,275 @@ impl BooleanParser {
}
}
}

///
#[derive(Debug)]
pub(crate) struct Float16Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Float16Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<f16> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<f16>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Float32Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Float32Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<f32> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<f32>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Float64Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Float64Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<f64> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<f64>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Int16Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Int16Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<i16> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<i16>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Int32Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Int32Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<i32> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<i32>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Int64Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Int64Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse(&self, bytes: &[u8]) -> Result<i64> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed.parse::<i64>()?)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct Utf8Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl Utf8Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse<'a>(&self, bytes: &'a [u8]) -> Result<&'a str> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

///
#[derive(Debug)]
pub(crate) struct LargeUtf8Parser {
alignment: Alignment,
trim_symbol: Symbol,
}

///
impl LargeUtf8Parser {
///
pub fn new(alignment: Alignment, trim_symbol: Symbol) -> Self {
Self {
alignment,
trim_symbol,
}
}

///
pub fn parse<'a>(&self, bytes: &'a [u8]) -> Result<&'a str> {
let text: &str = from_utf8(bytes)?;
let trimmed: &str = self.trim(text);
Ok(trimmed)
}

///
pub fn trim<'a>(&self, text: &'a str) -> &'a str {
match self.alignment {
Alignment::Left => text.trim_end_matches::<char>(self.trim_symbol.into()),
Alignment::Right => text.trim_start_matches::<char>(self.trim_symbol.into()),
Alignment::Center => text.trim_matches::<char>(self.trim_symbol.into()),
}
}
}

0 comments on commit f57b783

Please sign in to comment.