Skip to content

Commit

Permalink
Clean up parsing helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jan 12, 2024
1 parent 0edadec commit 29ac5de
Show file tree
Hide file tree
Showing 23 changed files with 2,423 additions and 262 deletions.
8 changes: 8 additions & 0 deletions .github/.cspell/project-dictionary.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
binormal
bitangent
brep
bytecount
collada
ctypes
Eisel
elems
emin
endfacet
endloop
endsolid
gltf
idents
IDREF
Lemire
linestrips
nalgebra
NMTOKEN
polylist
powerset
repr
rustdocflags
rustflags
rustup
SIDREF
significand
splitn
texbinormal
texcoord
Expand Down
36 changes: 32 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ collada = ["roxmltree"]
# obj = []

[dependencies]
# Used in all formats.
fast-float = "0.2"

# Used in STL parsing.
memchr = { version = "2.4", optional = true }

Expand All @@ -52,9 +49,40 @@ workspace = true
resolver = "2"
members = ["example"]

# This table is shared by projects under https://github.com/taiki-e.
# It is not intended for manual editing.
[workspace.lints.rust]
improper_ctypes = "warn"
improper_ctypes_definitions = "warn"
missing_debug_implementations = "warn"
# missing_docs = "warn" # TODO
non_ascii_idents = "warn"
rust_2018_idioms = "warn"
single_use_lifetimes = "warn"
unreachable_pub = "warn"
# unsafe_op_in_unsafe_fn = "warn" # Set at crate-level instead since https://github.com/rust-lang/rust/pull/100081 is not available on MSRV
[workspace.lints.clippy]
all = "warn" # Downgrade deny-by-default lints
pedantic = "warn"
as_ptr_cast_mut = "warn"
default_union_representation = "warn"
inline_asm_x86_att_syntax = "warn"
trailing_empty_array = "warn"
transmute_undefined_repr = "warn"
undocumented_unsafe_blocks = "warn"
# Suppress buggy or noisy clippy lints
bool_assert_comparison = { level = "allow", priority = 1 }
borrow_as_ptr = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/8286
doc_markdown = { level = "allow", priority = 1 }
float_cmp = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/7725
manual_assert = { level = "allow", priority = 1 }
manual_range_contains = { level = "allow", priority = 1 } # https://github.com/rust-lang/rust-clippy/issues/6455#issuecomment-1225966395
missing_errors_doc = { level = "allow", priority = 1 }
module_name_repetitions = { level = "allow", priority = 1 }
similar_names = { level = "allow", priority = 1 }
single_match = { level = "allow", priority = 1 }
single_match_else = { level = "allow", priority = 1 }
struct_excessive_bools = { level = "allow", priority = 1 }
struct_field_names = { level = "allow", priority = 1 }
too_many_arguments = { level = "allow", priority = 1 }
too_many_lines = { level = "allow", priority = 1 }
type_complexity = { level = "allow", priority = 1 }
1 change: 1 addition & 0 deletions src/collada/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ fn parse_primitive(node: xml::Node<'_, '_>, ty: PrimitiveType) -> io::Result<Pri
p.push(value?);
}

#[allow(clippy::cast_possible_truncation)]
let added = (p.len() - prev_len) as u32;
if added % stride != 0 {
bail!(
Expand Down
1 change: 1 addition & 0 deletions src/collada/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(super) fn build_meshes(doc: &Document) -> Vec<common::Mesh> {
};

for prim in mesh_ref.primitives() {
#[allow(clippy::cast_possible_truncation)]
let prev_positions_len = mesh.vertices.len() as u32;
let p: Vec<_> = prim.positions().collect();
let n: Vec<_> = prim.normals().collect();
Expand Down
20 changes: 10 additions & 10 deletions src/collada/iter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
iter::{self, FusedIterator},
ops::RangeInclusive,
ops::Range,
slice,
};

Expand Down Expand Up @@ -378,7 +378,7 @@ enum IndicesInner<'a> {
stride: u32,
vcount: slice::Iter<'a, u32>,
index: usize,
range: Option<RangeInclusive<u32>>,
range: Option<Range<u32>>,
},
Triangles {
offset: u32,
Expand All @@ -390,7 +390,7 @@ enum IndicesInner<'a> {
stride: u32,
vcount: slice::Iter<'a, u32>,
index: usize,
range: Option<RangeInclusive<u32>>,
range: Option<Range<u32>>,
},
Lines {
offset: u32,
Expand All @@ -402,7 +402,7 @@ enum IndicesInner<'a> {
stride: u32,
vcount: slice::Iter<'a, u32>,
index: usize,
range: Option<RangeInclusive<u32>>,
range: Option<Range<u32>>,
},
None,
}
Expand Down Expand Up @@ -453,7 +453,7 @@ impl Iterator for VertexIndices<'_> {
// NOTE: Do *not* increment index until range ends.
return Some(value);
}
let vc = *r.end() + 2;
let vc = r.end + 1;
*index += stride * vc as usize;
*range = None;
}
Expand Down Expand Up @@ -482,7 +482,7 @@ impl Iterator for VertexIndices<'_> {
}
0 => unreachable!(),
_ => {
let mut ri = 1..=vc - 2;
let mut ri = 1..vc - 1;
let k = ri.next().unwrap();
let x = *index + offset;
let y = *index + offset + stride * k as usize;
Expand Down Expand Up @@ -531,7 +531,7 @@ impl Iterator for VertexIndices<'_> {
// NOTE: Do *not* increment index until range ends.
return Some(value);
}
let vc = *r.end() + 1;
let vc = r.end;
*index += stride * vc as usize;
*range = None;
}
Expand All @@ -546,14 +546,14 @@ impl Iterator for VertexIndices<'_> {
}
0..=2 => unreachable!(),
_ => {
let mut ri = 1..=vc - 1;
let k = ri.next().unwrap();
let mut r = 1..vc;
let k = r.next().unwrap();
let x = *index + offset;
let y = *index + offset + stride * k as usize;
let value = Face::Line([indices[x], indices[y]]);
// Set range for next call.
// NOTE: Do *not* increment index until range ends.
*range = Some(ri);
*range = Some(r);
Some(value)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub struct Mesh {

impl Mesh {
#[inline]
#[must_use]
pub fn merge(mut meshes: Vec<Self>) -> Self {
if meshes.len() <= 1 {
return meshes.pop().unwrap_or_default();
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) fn invalid_data(e: impl Into<Box<dyn std::error::Error + Send + Sync>

#[cfg(feature = "stl")]
#[cold]
pub(crate) fn with_location(e: io::Error, location: Location<'_>) -> io::Error {
pub(crate) fn with_location(e: &io::Error, location: &Location<'_>) -> io::Error {
io::Error::new(e.kind(), format!("{e} ({location})"))
}

Expand Down
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
#![doc(test(
no_crate_inject,
attr(
deny(warnings, rust_2018_idioms, single_use_lifetimes),
allow(dead_code, unused_variables)
)
))]
#![forbid(unsafe_code)]
#![warn(clippy::exhaustive_enums, clippy::exhaustive_structs)]
#![allow(
clippy::match_same_arms, // https://github.com/rust-lang/rust-clippy/issues/12044
clippy::missing_panics_doc,
clippy::must_use_candidate,
clippy::naive_bytecount,
clippy::unreadable_literal,
clippy::wildcard_imports, // TODO
)]

#[cfg(any(feature = "collada", feature = "stl"))]
#[macro_use]
mod error;

#[cfg(any(feature = "collada", feature = "stl"))]
mod utils;

mod common;
Expand All @@ -16,3 +32,12 @@ pub mod collada;
// pub mod obj;
#[cfg(feature = "stl")]
pub mod stl;

// Not public API. (exposed for benchmarks)
#[doc(hidden)]
#[cfg(any(feature = "collada", feature = "stl"))]
pub mod __private {
pub use crate::utils::float;
#[cfg(feature = "collada")]
pub use crate::utils::int;
}
Loading

0 comments on commit 29ac5de

Please sign in to comment.