Skip to content

Commit 20f7c58

Browse files
authored
Merge pull request #4 from gadunga/nom_8_update
Update to nom 8
2 parents 9ca9322 + 24d598f commit 20f7c58

File tree

10 files changed

+509
-339
lines changed

10 files changed

+509
-339
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
runs-on: ${{ matrix.os }}
3636
strategy:
3737
matrix:
38-
rust_versions: ["stable", "1.46"]
38+
rust_versions: ["stable", "1.75"]
3939
os: [ubuntu-latest, windows-latest]
4040
steps:
4141
- name: Checkout the source code

Cargo.toml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nobject-rs"
3-
version = "2.0.0"
3+
version = "3.0.0"
44
authors = ["shmapdy <[email protected]>"]
55
edition = "2021"
66
license = "MIT"
@@ -12,7 +12,7 @@ description = "A parser for wavefront Obj/Mtl files. Written with Nom."
1212
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1313

1414
[dependencies]
15-
derive_more = "0.99"
15+
derive_more = {version = "1.0", features = ["constructor", "from", "into"]}
1616
log = "0.4"
17-
nom = "7.1.3"
18-
thiserror = "1.0"
17+
nom = "8.0"
18+
thiserror = "2.0"

src/lib.rs

+19-16
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ mod tokenizer;
8888
mod material;
8989
mod model;
9090

91+
use std::borrow::Cow;
9192
use std::result::Result;
9293

9394
pub use model::{
@@ -121,8 +122,8 @@ pub enum ObjError {
121122
MaterialParse(#[from] MaterialError),
122123

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

127128
/// The specification for obj/mtl files has some settings
128129
/// either being "on" or "off". If there is an issue
@@ -141,7 +142,7 @@ pub enum ObjError {
141142
/// or a constructed `Model`.
142143
pub fn load_obj(input: &str) -> Result<Model, ObjError> {
143144
match tokenizer::parse_obj(input) {
144-
Ok(tokens) => Ok(model::parse(&tokens)?),
145+
Ok(tokens) => Ok(model::parse(tokens)?),
145146
Err(e) => Err(e.into()),
146147
}
147148
}
@@ -156,7 +157,7 @@ pub fn load_obj(input: &str) -> Result<Model, ObjError> {
156157
/// or a collection of `Material`.
157158
pub fn load_mtl(input: &str) -> Result<Vec<Material>, ObjError> {
158159
match tokenizer::parse_mtl(input) {
159-
Ok(tokens) => Ok(material::parse(&tokens)?),
160+
Ok(tokens) => Ok(material::parse(tokens)?),
160161
Err(e) => Err(e.into()),
161162
}
162163
}
@@ -167,7 +168,7 @@ fn get_token_float(token: &Token) -> Result<f32, ObjError> {
167168
} else if let Token::Int(i) = token {
168169
Ok(*i as f32)
169170
} else {
170-
Err(ObjError::UnexpectedToken(token.clone()))
171+
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
171172
}
172173
}
173174

@@ -178,7 +179,7 @@ fn get_opt_token_float_opt(token: &Option<Token>) -> Result<Option<f32>, ObjErro
178179
} else if let Token::Int(i) = t {
179180
Ok(Some(*i as f32))
180181
} else {
181-
Err(ObjError::UnexpectedToken(t.clone()))
182+
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
182183
}
183184
} else {
184185
Ok(None)
@@ -189,27 +190,29 @@ fn get_token_int(token: &Token) -> Result<i32, ObjError> {
189190
if let Token::Int(i) = token {
190191
Ok(*i)
191192
} else {
192-
Err(ObjError::UnexpectedToken(token.clone()))
193+
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
193194
}
194195
}
195196

196-
fn get_token_string(token: &Token) -> Result<String, ObjError> {
197+
fn get_token_string<'a>(token: &'a Token) -> Result<Cow<'a, str>, ObjError> {
197198
if let Token::String(s) = token {
198199
Ok(s.clone())
199200
} else if let Token::Int(i) = token {
200-
Ok(i.to_string())
201+
Ok(Cow::Owned(i.to_string()))
201202
} else if let Token::Float(f) = token {
202-
Ok(f.to_string())
203+
Ok(Cow::Owned(f.to_string()))
203204
} else {
204-
Err(ObjError::UnexpectedToken(token.clone()))
205+
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
205206
}
206207
}
207208

208209
fn get_on_off_from_str(token: &Token) -> Result<bool, ObjError> {
209-
let s = get_token_string(&token)?;
210-
match s.as_str() {
211-
"on" => Ok(true),
212-
"off" => Ok(false),
213-
_ => Err(ObjError::InvalidOnOffValue(s.clone())),
210+
let s = get_token_string(token)?;
211+
if s.eq_ignore_ascii_case("on") {
212+
Ok(true)
213+
} else if s.eq_ignore_ascii_case("off") {
214+
Ok(false)
215+
} else {
216+
Err(ObjError::UnexpectedToken(format!("{:#?}", token)))
214217
}
215218
}

src/macros.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
macro_rules! token_match {
22
($($token:tt)*) => {{
3-
fn inner() -> impl Fn(&[Token]) -> IResult<&[Token], Token> {
4-
move |input: &[Token]| -> IResult<&[Token], Token> {
3+
fn inner() -> impl Fn(crate::tokenizer::TokenSet) -> IResult<crate::tokenizer::TokenSet, Token> {
4+
move |input: crate::tokenizer::TokenSet| -> IResult<crate::tokenizer::TokenSet, Token> {
55
if input.is_empty() {
66
Err(nom::Err::Error(nom::error::Error::new(
77
input,
88
nom::error::ErrorKind::Eof,
99
)))
10-
} else if matches!(input[0], $($token)*) {
11-
let token = input[0].clone();
10+
} else if matches!(input.as_ref()[0], $($token)*) {
11+
let token = input.as_ref()[0].clone();
1212
let (_, remainder) = input.split_at(1);
1313
Ok((remainder, token))
1414
} else {

0 commit comments

Comments
 (0)