Skip to content

Commit

Permalink
Merge pull request #21 from 4LT/implement-error-trait
Browse files Browse the repository at this point in the history
Implement error trait on crate error types
  • Loading branch information
4LT authored Mar 15, 2023
2 parents 40c4333 + 22d7e0a commit cdee91f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 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 = "quake-util"
version = "0.1.0"
version = "0.1.1"
authors = ["Seth <[email protected]>"]
edition = "2021"
description = "A utility library for using Quake file formats"
Expand Down
2 changes: 1 addition & 1 deletion src/qmap/lexer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn lex_all_symbols() {
#[test]
fn lex_bad_quoted() {
let input = b"good-token \"bad-token eof-here-we-come";
let bad_token = TokenIterator::new(&input[..]).skip(1).next();
let bad_token = TokenIterator::new(&input[..]).nth(1);

if let Err(qmap_error) = bad_token.unwrap() {
if let ParseError::Lexer(line_error) = qmap_error {
Expand Down
7 changes: 5 additions & 2 deletions src/qmap/parse_result.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[cfg(feature = "std")]
extern crate std;

use std::{
fmt,
error, fmt,
num::NonZeroU64,
string::{String, ToString},
};
Expand All @@ -22,6 +21,8 @@ impl fmt::Display for LineError {
}
}

impl error::Error for LineError {}

#[derive(Debug, Clone)]
pub enum ParseError {
Io(String),
Expand Down Expand Up @@ -66,4 +67,6 @@ impl fmt::Display for ParseError {
}
}

impl error::Error for ParseError {}

pub type ParseResult<T> = std::result::Result<T, ParseError>;
4 changes: 2 additions & 2 deletions src/qmap/parser_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ fn parse_closing_brace_error() {
let err = parse(&b"}"[..]).err().unwrap();
if let ParseError::Parser(line_err) = err {
assert_eq!(u64::from(line_err.line_number.unwrap()), 1u64);
assert!(line_err.message.contains("}"));
assert!(line_err.message.contains('}'));
} else {
panic_unexpected_variant(err);
}
Expand All @@ -238,7 +238,7 @@ fn parse_missing_value() {
let err = parse(&b"{\n \"classname\"\n }"[..]).err().unwrap();
if let ParseError::Parser(line_err) = err {
assert_eq!(u64::from(line_err.line_number.unwrap()), 3u64);
assert!(line_err.message.contains("}"));
assert!(line_err.message.contains('}'));
} else {
panic_unexpected_variant(err);
}
Expand Down
16 changes: 16 additions & 0 deletions src/qmap/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ extern crate alloc;
#[cfg(feature = "std")]
use std::{
collections::HashMap,
error,
ffi::{CStr, CString},
fmt,
fmt::{Display, Formatter},
io,
string::String,
vec::Vec,
Expand All @@ -32,6 +35,19 @@ pub enum WriteError {
Io(std::io::Error),
}

#[cfg(feature = "std")]
impl Display for WriteError {
fn fmt(&self, formatter: &mut Formatter) -> Result<(), fmt::Error> {
match self {
Self::Validation(msg) => write!(formatter, "Validation: {}", msg),
Self::Io(err) => write!(formatter, "I/O: {}", err),
}
}
}

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

#[cfg(feature = "std")]
pub type WriteAttempt = Result<(), WriteError>;

Expand Down

0 comments on commit cdee91f

Please sign in to comment.