Skip to content

Commit

Permalink
Merge pull request #4 from gadunga/nom_8_update
Browse files Browse the repository at this point in the history
Update to nom 8
  • Loading branch information
gadunga authored Feb 2, 2025
2 parents 9ca9322 + 24d598f commit 20f7c58
Show file tree
Hide file tree
Showing 10 changed files with 509 additions and 339 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
rust_versions: ["stable", "1.46"]
rust_versions: ["stable", "1.75"]
os: [ubuntu-latest, windows-latest]
steps:
- name: Checkout the source code
Expand Down
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nobject-rs"
version = "2.0.0"
version = "3.0.0"
authors = ["shmapdy <[email protected]>"]
edition = "2021"
license = "MIT"
Expand All @@ -12,7 +12,7 @@ description = "A parser for wavefront Obj/Mtl files. Written with Nom."
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
derive_more = "0.99"
derive_more = {version = "1.0", features = ["constructor", "from", "into"]}
log = "0.4"
nom = "7.1.3"
thiserror = "1.0"
nom = "8.0"
thiserror = "2.0"
35 changes: 19 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ mod tokenizer;
mod material;
mod model;

use std::borrow::Cow;
use std::result::Result;

pub use model::{
Expand Down Expand Up @@ -121,8 +122,8 @@ pub enum ObjError {
MaterialParse(#[from] MaterialError),

/// An unexpected token was encountered in the token stream.
#[error("Unexpected token encountered: `{0:#?}`")]
UnexpectedToken(Token),
#[error("Unexpected token encountered: `{0}`")]
UnexpectedToken(String),

/// The specification for obj/mtl files has some settings
/// either being "on" or "off". If there is an issue
Expand All @@ -141,7 +142,7 @@ pub enum ObjError {
/// or a constructed `Model`.
pub fn load_obj(input: &str) -> Result<Model, ObjError> {
match tokenizer::parse_obj(input) {
Ok(tokens) => Ok(model::parse(&tokens)?),
Ok(tokens) => Ok(model::parse(tokens)?),
Err(e) => Err(e.into()),
}
}
Expand All @@ -156,7 +157,7 @@ pub fn load_obj(input: &str) -> Result<Model, ObjError> {
/// or a collection of `Material`.
pub fn load_mtl(input: &str) -> Result<Vec<Material>, ObjError> {
match tokenizer::parse_mtl(input) {
Ok(tokens) => Ok(material::parse(&tokens)?),
Ok(tokens) => Ok(material::parse(tokens)?),
Err(e) => Err(e.into()),
}
}
Expand All @@ -167,7 +168,7 @@ fn get_token_float(token: &Token) -> Result<f32, ObjError> {
} else if let Token::Int(i) = token {
Ok(*i as f32)
} else {
Err(ObjError::UnexpectedToken(token.clone()))
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
}
}

Expand All @@ -178,7 +179,7 @@ fn get_opt_token_float_opt(token: &Option<Token>) -> Result<Option<f32>, ObjErro
} else if let Token::Int(i) = t {
Ok(Some(*i as f32))
} else {
Err(ObjError::UnexpectedToken(t.clone()))
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
}
} else {
Ok(None)
Expand All @@ -189,27 +190,29 @@ fn get_token_int(token: &Token) -> Result<i32, ObjError> {
if let Token::Int(i) = token {
Ok(*i)
} else {
Err(ObjError::UnexpectedToken(token.clone()))
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
}
}

fn get_token_string(token: &Token) -> Result<String, ObjError> {
fn get_token_string<'a>(token: &'a Token) -> Result<Cow<'a, str>, ObjError> {
if let Token::String(s) = token {
Ok(s.clone())
} else if let Token::Int(i) = token {
Ok(i.to_string())
Ok(Cow::Owned(i.to_string()))
} else if let Token::Float(f) = token {
Ok(f.to_string())
Ok(Cow::Owned(f.to_string()))
} else {
Err(ObjError::UnexpectedToken(token.clone()))
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
}
}

fn get_on_off_from_str(token: &Token) -> Result<bool, ObjError> {
let s = get_token_string(&token)?;
match s.as_str() {
"on" => Ok(true),
"off" => Ok(false),
_ => Err(ObjError::InvalidOnOffValue(s.clone())),
let s = get_token_string(token)?;
if s.eq_ignore_ascii_case("on") {
Ok(true)
} else if s.eq_ignore_ascii_case("off") {
Ok(false)
} else {
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
}
}
8 changes: 4 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
macro_rules! token_match {
($($token:tt)*) => {{
fn inner() -> impl Fn(&[Token]) -> IResult<&[Token], Token> {
move |input: &[Token]| -> IResult<&[Token], Token> {
fn inner() -> impl Fn(crate::tokenizer::TokenSet) -> IResult<crate::tokenizer::TokenSet, Token> {
move |input: crate::tokenizer::TokenSet| -> IResult<crate::tokenizer::TokenSet, Token> {
if input.is_empty() {
Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Eof,
)))
} else if matches!(input[0], $($token)*) {
let token = input[0].clone();
} else if matches!(input.as_ref()[0], $($token)*) {
let token = input.as_ref()[0].clone();
let (_, remainder) = input.split_at(1);
Ok((remainder, token))
} else {
Expand Down
Loading

0 comments on commit 20f7c58

Please sign in to comment.