Skip to content

Commit 5e1e238

Browse files
committed
feat!: remove BinProperty & name_hash duplication
replaces BinProperty with trait methods on ReaderExt/WriterExt
1 parent c053fd7 commit 5e1e238

File tree

11 files changed

+268
-419
lines changed

11 files changed

+268
-419
lines changed

crates/ltk_meta/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ for (path_hash, object) in &tree.objects {
2727
2828
```
2929
use ltk_meta::{Bin, BinObject};
30-
use ltk_meta::property::values;
30+
use ltk_meta::property::{values, NoMeta};
3131
3232
// Using the builder pattern
3333
let tree = Bin::builder()
3434
.dependency("common.bin")
3535
.object(
36-
BinObject::builder(0x12345678, 0xABCDEF00)
36+
BinObject::<NoMeta>::builder(0x12345678, 0xABCDEF00)
3737
.property(0x1111, values::I32::new(42))
3838
.property(0x2222, values::String::from("hello"))
3939
.build()
@@ -70,7 +70,7 @@ tree.to_writer(&mut output)?;
7070
```
7171
*/
7272
pub mod property;
73-
pub use property::{BinProperty, Kind as PropertyKind, PropertyValueEnum};
73+
pub use property::{Kind as PropertyKind, PropertyValueEnum};
7474

7575
mod tree;
7676
pub use tree::*;

crates/ltk_meta/src/property.rs

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,69 +6,8 @@ pub use kind::*;
66
mod r#enum;
77
pub use r#enum::*;
88

9-
use super::traits::{ReaderExt as _, WriterExt as _};
109
use super::Error;
11-
use byteorder::{ReadBytesExt as _, WriteBytesExt as _, LE};
12-
use std::io;
13-
14-
use crate::traits::PropertyExt;
1510

1611
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1712
#[derive(Clone, Copy, Default, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
1813
pub struct NoMeta;
19-
20-
#[cfg_attr(
21-
feature = "serde",
22-
derive(serde::Serialize, serde::Deserialize),
23-
serde(bound = "for <'dee> M: serde::Serialize + serde::Deserialize<'dee>")
24-
)]
25-
#[derive(Clone, PartialEq, Debug)]
26-
pub struct BinProperty<M = NoMeta> {
27-
pub name_hash: u32,
28-
#[cfg_attr(feature = "serde", serde(flatten))]
29-
pub value: PropertyValueEnum<M>,
30-
}
31-
32-
impl<M> BinProperty<M> {
33-
#[inline(always)]
34-
#[must_use]
35-
pub fn no_meta(self) -> BinProperty<NoMeta> {
36-
BinProperty {
37-
name_hash: self.name_hash,
38-
value: self.value.no_meta(),
39-
}
40-
}
41-
42-
/// Read a BinProperty from a reader. This will read the name_hash, prop kind and then value, in that order.
43-
pub fn from_reader<R: io::Read + std::io::Seek + ?Sized>(
44-
reader: &mut R,
45-
legacy: bool,
46-
) -> Result<Self, Error>
47-
where
48-
M: Default,
49-
{
50-
let name_hash = reader.read_u32::<LE>()?;
51-
let kind = reader.read_property_kind(legacy)?;
52-
53-
Ok(Self {
54-
name_hash,
55-
value: PropertyValueEnum::from_reader(reader, kind, legacy)?,
56-
})
57-
}
58-
pub fn to_writer<W: io::Write + std::io::Seek + ?Sized>(
59-
&self,
60-
writer: &mut W,
61-
) -> Result<(), io::Error>
62-
where
63-
M: Clone,
64-
{
65-
writer.write_u32::<LE>(self.name_hash)?;
66-
writer.write_property_kind(self.value.kind())?;
67-
68-
self.value.to_writer(writer)?;
69-
Ok(())
70-
}
71-
pub fn size(&self) -> usize {
72-
5 + self.value.size_no_header()
73-
}
74-
}

crates/ltk_meta/src/property/values/struct.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::io;
22

33
use crate::{
44
property::{Kind, NoMeta},
5-
traits::{PropertyExt, PropertyValueExt, ReadProperty, WriteProperty},
6-
BinProperty, Error,
5+
traits::{
6+
PropertyExt, PropertyValueExt, ReadProperty, ReaderExt, WriteProperty, WriterExt as _,
7+
},
8+
Error, PropertyValueEnum,
79
};
810
use byteorder::{ReadBytesExt as _, WriteBytesExt as _, LE};
911
use indexmap::IndexMap;
@@ -17,7 +19,7 @@ use ltk_io_ext::{measure, window_at};
1719
#[derive(Clone, PartialEq, Debug, Default)]
1820
pub struct Struct<M = NoMeta> {
1921
pub class_hash: u32,
20-
pub properties: IndexMap<u32, BinProperty<M>>,
22+
pub properties: IndexMap<u32, PropertyValueEnum<M>>,
2123
pub meta: M,
2224
}
2325

@@ -45,7 +47,13 @@ impl<M> PropertyExt for Struct<M> {
4547
fn size_no_header(&self) -> usize {
4648
match self.class_hash {
4749
0 => 4,
48-
_ => 10 + self.properties.values().map(|p| p.size()).sum::<usize>(),
50+
_ => {
51+
10 + self
52+
.properties
53+
.values()
54+
.map(|p| 5 + p.size_no_header())
55+
.sum::<usize>()
56+
}
4957
}
5058
}
5159

@@ -77,8 +85,8 @@ impl<M: Default> ReadProperty for Struct<M> {
7785
let prop_count = reader.read_u16::<LE>()?;
7886
let mut properties = IndexMap::with_capacity(prop_count as _);
7987
for _ in 0..prop_count {
80-
let prop = BinProperty::from_reader(reader, legacy)?;
81-
properties.insert(prop.name_hash, prop);
88+
let (name_hash, value) = reader.read_property::<M>(legacy)?;
89+
properties.insert(name_hash, value);
8290
}
8391
Ok::<_, Error>(Self {
8492
class_hash,
@@ -116,8 +124,10 @@ impl<M: Clone> WriteProperty for Struct<M> {
116124
let (size, _) = measure(writer, |writer| {
117125
writer.write_u16::<LE>(self.properties.len() as _)?;
118126

119-
for prop in self.properties.values() {
120-
prop.to_writer(writer)?;
127+
for (name_hash, value) in self.properties.iter() {
128+
writer.write_u32::<LE>(*name_hash)?;
129+
writer.write_property_kind(value.kind())?;
130+
value.to_writer(writer)?;
121131
}
122132

123133
Ok::<_, io::Error>(())

crates/ltk_meta/src/traits.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::io;
22

3+
use crate::PropertyValueEnum;
4+
35
use super::property::Kind;
4-
use byteorder::{ReadBytesExt, WriteBytesExt};
6+
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
57

68
const HEADER_SIZE: usize = 5;
79

@@ -50,6 +52,22 @@ pub trait ReaderExt: io::Read {
5052
fn read_property_kind(&mut self, legacy: bool) -> Result<Kind, crate::Error> {
5153
Kind::unpack(self.read_u8()?, legacy)
5254
}
55+
56+
fn read_property<M: Default>(
57+
&mut self,
58+
legacy: bool,
59+
) -> Result<(u32, PropertyValueEnum<M>), crate::Error>
60+
where
61+
Self: io::Seek,
62+
{
63+
let name_hash = self.read_u32::<LE>()?;
64+
let kind = self.read_property_kind(legacy)?;
65+
66+
Ok((
67+
name_hash,
68+
PropertyValueEnum::from_reader(self, kind, legacy)?,
69+
))
70+
}
5371
}
5472

5573
impl<R: io::Read + ?Sized> ReaderExt for R {}
@@ -68,6 +86,21 @@ pub trait WriterExt: io::Write {
6886
fn write_property_kind(&mut self, kind: Kind) -> Result<(), io::Error> {
6987
self.write_u8(kind.into())
7088
}
89+
90+
fn write_property<M>(
91+
&mut self,
92+
name_hash: u32,
93+
value: &PropertyValueEnum<M>,
94+
) -> Result<(), io::Error>
95+
where
96+
M: Clone,
97+
Self: io::Seek,
98+
{
99+
self.write_u32::<LE>(name_hash)?;
100+
self.write_property_kind(value.kind())?;
101+
value.to_writer(self)?;
102+
Ok(())
103+
}
71104
}
72105

73106
impl<R: io::Write + ?Sized> WriterExt for R {}

0 commit comments

Comments
 (0)