Skip to content

Commit

Permalink
Complete rewrite of the whole parser logic
Browse files Browse the repository at this point in the history
now using nom instead of manually iterating over bytes
however the parsing may now fail in case of malformed text
  • Loading branch information
uttarayan21 committed Jul 20, 2022
1 parent 92736b3 commit 0e28929
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 619 deletions.
17 changes: 7 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
[package]
name = "ansi-to-tui"
version = "0.5.0"
authors = ["Uttarayan Mondal <uttarayan21@gmail.com>"]
version = "1.0.1"
authors = ["Uttarayan Mondal <email@uttarayan.me>"]
edition = "2018"
description = "A library to convert ansi color coded text into tui::text::Text type from tui-rs library"
keywords = ["ansi", "ascii", "tui", "parser"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/uttarayan21/ansi-to-tui"
exclude = [
".github/*",
"ascii",
"tests",
]

[dependencies]
simdutf8 = { version = "0.1.1", optional = true }
tui = { version = "0.17.0", default-features = false }
nom = "7.1.1"
simdutf8 = { version = "0.1.4", optional = true }
tui = { version = "0.18.0", default-features = false }
thiserror = "1.0.31"

[dev-dependencies]
anyhow = "1.0.40"
anyhow = "1.0.58"

[features]
simd = ["simdutf8"]
2 changes: 1 addition & 1 deletion ascii/text.ascii
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
AAAABBBB
TRUECOLOR
TRUECOLOR
235 changes: 0 additions & 235 deletions src/ansi.rs

This file was deleted.

6 changes: 2 additions & 4 deletions src/code.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// use crate::color::Color;
use tui::style::Color;

/// This enum stores most types of ansi escape sequences
///
/// You can turn an escape sequence to this enum variant using
/// from(u8) trait.
/// AnsiCode::from(code: u8)
/// This doesn't support all of them but does support most of them.

#[derive(Debug, PartialEq)]
#[repr(u8)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum AnsiCode {
/// Reset the terminal
Reset,
Expand Down
46 changes: 14 additions & 32 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
#[cfg(feature = "simd")]
use simdutf8::basic::Utf8Error;
#[cfg(not(feature = "simd"))]
use std::string::FromUtf8Error;

use std::error::Error as StdErr;

/// This enum stores the error types
#[derive(Debug)]
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum Error {
/// Stack is empty (should never happen)
StackEmpty,
#[error("Nom Error")]
NomError(String),

/// Error parsing the input as utf-8
Utf8Error,
#[cfg(feature = "simdutf8")]
/// Cannot determine the foreground or background
UnknownLayer,
/// Error while parsing the color
ColorParsingError,
/// Error while paring the ansi sequnce as a usize ( should really be u8 max )
UsizeParsingError,
}
#[error("{0:?}")]
Utf8Error(#[from] simdutf8::basic::Utf8Error),

#[cfg(not(feature = "simd"))]
impl From<FromUtf8Error> for Error {
fn from(_: FromUtf8Error) -> Self {
Error::Utf8Error
}
}
#[cfg(feature = "simd")]
impl From<Utf8Error> for Error {
fn from(_: Utf8Error) -> Self {
Error::Utf8Error
}
#[cfg(not(feature = "simdutf8"))]
/// Cannot determine the foreground or background
#[error("{0:?}")]
Utf8Error(#[from] std::string::FromUtf8Error),
}

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
impl From<nom::Err<nom::error::Error<&[u8]>>> for Error {
fn from(e: nom::Err<nom::error::Error<&[u8]>>) -> Self {
Self::NomError(format!("{:?}", e))
}
}

impl StdErr for Error {}
Loading

0 comments on commit 0e28929

Please sign in to comment.