Skip to content

Commit

Permalink
Merge pull request #84 from rusty-ecma/fix/remove-dyn-error
Browse files Browse the repository at this point in the history
fix: Error type is now Send + Sync
  • Loading branch information
FreeMasen authored Jun 4, 2023
2 parents bfda0c8 + 1e85283 commit 5e332c0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ressa"
version = "0.9.0-alpha.1"
version = "0.9.0-alpha.2"
authors = ["Robert Masen <[email protected]>"]
repository = "https://github.com/rusty-ecma/RESSA"
description = "An ECMAscript parser"
Expand Down
31 changes: 28 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ress::{tokens::Keyword, Position};
use std::fmt::{Display, Formatter, Result};

#[derive(Debug)]
pub enum Error {
UnexpectedEoF,
Expand Down Expand Up @@ -43,7 +44,8 @@ pub enum Error {
UndefinedExports(Vec<String>),
ContinueOfNotIterationLabel(Position, String),
Scanner(ress::error::Error),
Other(Box<dyn ::std::error::Error>),
Regex(res_regex::Error),
Io(std::io::Error),
Misc(String),
}

Expand Down Expand Up @@ -91,7 +93,8 @@ impl Display for Error {
Error::UndefinedExports(ref names) => write!(f, "Undefined exports in module: {}", names.join(", ")),
Error::ContinueOfNotIterationLabel(ref pos, ref token) => write!(f, "Label `{}` is does not label a loop, continue is invalid at {}", token, pos),
Error::Scanner(ref e) => write!(f, "Error when tokenizing {}", e),
Error::Other(ref e) => write!(f, "{}", e),
Error::Regex(ref e) => write!(f, "{}", e),
Error::Io(ref e) => write!(f, "{}", e),
Error::Misc(ref e) => write!(f, "{}", e),
}
}
Expand Down Expand Up @@ -153,7 +156,7 @@ impl Error {

impl From<::std::io::Error> for Error {
fn from(other: ::std::io::Error) -> Self {
Error::Other(Box::new(other))
Error::Io(other)
}
}
impl ::std::error::Error for Error {}
Expand All @@ -163,3 +166,25 @@ impl From<ress::error::Error> for Error {
Error::Scanner(other)
}
}

impl From<res_regex::Error> for Error {
fn from(value: res_regex::Error) -> Self {
Self::Regex(value)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn error_is_send_and_sync() {
fn print_error<E>(arg: E)
where
E: std::error::Error + Send + Sync,
{
println!("{arg}");
}
print_error(Error::Misc("some misc error".to_string()))
}
}
7 changes: 2 additions & 5 deletions src/regex.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use super::{Error, Res};
use super::Res;
use res_regex::RegexParser;
/// Validate that an already parsed regular expression
/// literal does not contain any illegal constructs
/// like a duplicate flag or invalid class range
pub fn validate_regex<'a>(regex: &'a str) -> Res<()> {
RegexParser::new(&regex)
.map_err(|e| Error::Other(Box::new(e)))?
.validate()
.map_err(|e| Error::Other(Box::new(e)))?;
RegexParser::new(&regex)?.validate()?;
Ok(())
}

0 comments on commit 5e332c0

Please sign in to comment.