Skip to content

Commit

Permalink
Move error to its own submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
JSorngard committed Dec 16, 2023
1 parent 6b5df36 commit 5a45d1c
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 108 deletions.
111 changes: 111 additions & 0 deletions common/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//! Contains the definition of the error type used by the eoncoding functions in the crate.
use core::fmt;

#[derive(Debug, Clone, Copy, PartialEq)]
/// The error returned by [`zalgo_encode`], [`ZalgoString::new`], and [`zalgo_wrap_python`]
/// if they encounter a byte they can not encode.
///
/// Only implements the [`Error`](std::error::Error) trait if the `std` feature is enabled.
pub enum Error {
/// Represents a valid ASCII character that is outside of the encodable set.
UnencodableAscii(u8, usize, usize, &'static str),
/// Represents some other unicode character.
NotAscii(u8, usize, usize),
}

impl Error {
/// Returns the 1-indexed line number of the line on which the unencodable byte occured.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("❤️").err().unwrap().line(), 1);
/// assert_eq!(zalgo_encode("a\nb\nc\r\n").err().unwrap().line(), 3);
/// ```
#[must_use = "the method returns a new valus and does not modify `self`"]
pub const fn line(&self) -> usize {
match self {
Self::UnencodableAscii(_, line, _, _) | Self::NotAscii(_, line, _) => *line,
}
}

/// Returns the 1-indexed column where the unencodable byte occured.
/// Columns are counted from left to right and the count resets for each new line.
///
/// # Example
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("I ❤️ 🎂").err().unwrap().column(), 3);
/// assert_eq!(zalgo_encode("I\n❤️\n🎂").err().unwrap().column(), 1);
/// ```
#[must_use = "the method returns a new valus and does not modify `self`"]
pub const fn column(&self) -> usize {
match self {
Self::UnencodableAscii(_, _, column, _) | Self::NotAscii(_, _, column) => *column,
}
}

/// Returns the value of the first byte of the unencodable character.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("\r").err().unwrap().byte(), 13);
/// ```
/// Note that this might not be the complete representation of
/// the character in unicode, just the first byte of it.
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("❤️").err().unwrap().byte(), 226);
/// // Even though
/// assert_eq!("❤️".as_bytes(), &[226, 157, 164, 239, 184, 143])
/// ```
#[must_use = "the method returns a new value and does not modify `self`"]
pub const fn byte(&self) -> u8 {
match self {
Self::UnencodableAscii(byte, _, _, _) | Self::NotAscii(byte, _, _) => *byte,
}
}

/// Return a representation of the unencodable byte.
/// This exists if the character is an unencodable ASCII character.
/// If it is some other unicode character we only know its first byte, so we can not
/// accurately represent it.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::zalgo_encode;
/// assert_eq!(zalgo_encode("\r").err().unwrap().representation(), Some("Carriage Return"));
/// assert_eq!(zalgo_encode("❤️").err().unwrap().representation(), None);
/// ```
#[must_use = "the method returns a new value and does not modify `self`"]
pub const fn representation(&self) -> Option<&'static str> {
match self {
Self::UnencodableAscii(_, _, _, repr) => Some(*repr),
Self::NotAscii(_, _, _) => None,
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UnencodableAscii(byte, line, column, repr) => write!(
f,
"line {line} at column {column}: can not encode ASCII \"{repr}\" character with byte value {byte}"
),
Self::NotAscii(byte, line, column) => write!(
f,
"line {line} at column {column}: byte value {byte} does not correspond to an ASCII character"
),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
110 changes: 2 additions & 108 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ use core::{fmt, str};
#[cfg(feature = "std")]
use std::string::FromUtf8Error;

mod error;
pub mod zalgo_string;

pub use error::Error;
pub use zalgo_string::ZalgoString;

/// Takes in an ASCII string without control characters (except newlines)
Expand Down Expand Up @@ -277,114 +279,6 @@ pub fn zalgo_wrap_python(python: &str) -> Result<String, Error> {
Ok(format!("b='{encoded_string}'.encode();exec(''.join(chr(((h<<6&64|c&63)+22)%133+10)for h,c in zip(b[1::2],b[2::2])))"))
}

#[derive(Debug, Clone, Copy, PartialEq)]
/// The error returned by [`zalgo_encode`], [`ZalgoString::new`], and [`zalgo_wrap_python`]
/// if they encounter a byte they can not encode.
///
/// Only implements the [`Error`](std::error::Error) trait if the `std` feature is enabled.
pub enum Error {
/// Represents a valid ASCII character that is outside of the encodable set.
UnencodableAscii(u8, usize, usize, &'static str),
/// Represents some other unicode character.
NotAscii(u8, usize, usize),
}

impl Error {
/// Returns the 1-indexed line number of the line on which the unencodable byte occured.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("❤️").err().unwrap().line(), 1);
/// assert_eq!(zalgo_encode("a\nb\nc\r\n").err().unwrap().line(), 3);
/// ```
#[must_use = "the method returns a new valus and does not modify `self`"]
pub const fn line(&self) -> usize {
match self {
Self::UnencodableAscii(_, line, _, _) | Self::NotAscii(_, line, _) => *line,
}
}

/// Returns the 1-indexed column where the unencodable byte occured.
/// Columns are counted from left to right and the count resets for each new line.
///
/// # Example
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("I ❤️ 🎂").err().unwrap().column(), 3);
/// assert_eq!(zalgo_encode("I\n❤️\n🎂").err().unwrap().column(), 1);
/// ```
#[must_use = "the method returns a new valus and does not modify `self`"]
pub const fn column(&self) -> usize {
match self {
Self::UnencodableAscii(_, _, column, _) | Self::NotAscii(_, _, column) => *column,
}
}

/// Returns the value of the first byte of the unencodable character.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("\r").err().unwrap().byte(), 13);
/// ```
/// Note that this might not be the complete representation of
/// the character in unicode, just the first byte of it.
/// ```
/// # use zalgo_codec_common::{Error, zalgo_encode};
/// assert_eq!(zalgo_encode("❤️").err().unwrap().byte(), 226);
/// // Even though
/// assert_eq!("❤️".as_bytes(), &[226, 157, 164, 239, 184, 143])
/// ```
#[must_use = "the method returns a new value and does not modify `self`"]
pub const fn byte(&self) -> u8 {
match self {
Self::UnencodableAscii(byte, _, _, _) | Self::NotAscii(byte, _, _) => *byte,
}
}

/// Return a representation of the unencodable byte.
/// This exists if the character is an unencodable ASCII character.
/// If it is some other unicode character we only know its first byte, so we can not
/// accurately represent it.
///
/// # Examples
///
/// ```
/// # use zalgo_codec_common::zalgo_encode;
/// assert_eq!(zalgo_encode("\r").err().unwrap().representation(), Some("Carriage Return"));
/// assert_eq!(zalgo_encode("❤️").err().unwrap().representation(), None);
/// ```
#[must_use = "the method returns a new value and does not modify `self`"]
pub const fn representation(&self) -> Option<&'static str> {
match self {
Self::UnencodableAscii(_, _, _, repr) => Some(*repr),
Self::NotAscii(_, _, _) => None,
}
}
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::UnencodableAscii(byte, line, column, repr) => write!(
f,
"line {line} at column {column}: can not encode ASCII \"{repr}\" character with byte value {byte}"
),
Self::NotAscii(byte, line, column) => write!(
f,
"line {line} at column {column}: byte value {byte} does not correspond to an ASCII character"
),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}

/// Returns the representation of the given ASCII byte if it's not printable.
#[inline]
#[must_use = "the function returns a new value and does not modify the input"]
Expand Down

0 comments on commit 5a45d1c

Please sign in to comment.