Skip to content

Commit

Permalink
cleanup errors
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Jan 10, 2022
1 parent 1fa95b0 commit 39537c5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
mem::ManuallyDrop,
};

use crate::errors::SetBytesError;

/// A storage type for raw bytes, used by the parser
#[derive(Eq, PartialOrd, Ord)]
pub struct Bytes<'a> {
Expand Down Expand Up @@ -257,12 +259,6 @@ impl IntoOwnedBytes for String {
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SetBytesError {
/// The length of the given data would overflow a `u32`
LengthOverflow,
}

impl Drop for BytesInner {
fn drop(&mut self) {
// we only need to deallocate if we own the data
Expand Down
19 changes: 19 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,22 @@ impl fmt::Display for ParseError {
}

impl Error for ParseError {}

/// An error that occurred during a call to `Bytes::set`
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SetBytesError {
/// The length of the given data would overflow a `u32`
LengthOverflow,
}

impl fmt::Display for SetBytesError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self {
SetBytesError::LengthOverflow => {
write!(f, "The string length is too large to fit in a `u32`")
}
}
}
}

impl Error for SetBytesError {}
10 changes: 10 additions & 0 deletions src/parser/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,16 @@ impl<'a> Node<'a> {
/// Tries to coerce this node into a raw text node, returning the text
///
/// "Raw text nodes" are nodes that are not HTML tags, but just text
pub fn as_raw(&self) -> Option<&Bytes<'a>> {
match self {
Self::Raw(r) => Some(r),
_ => None,
}
}

/// Tries to coerce this node into a mutable raw text node, returning the text
///
/// "Raw text nodes" are nodes that are not HTML tags, but just text
pub fn as_raw_mut(&mut self) -> Option<&mut Bytes<'a>> {
match self {
Self::Raw(r) => Some(r),
Expand Down

0 comments on commit 39537c5

Please sign in to comment.