Skip to content

Commit 7eb88f8

Browse files
committed
fix: optional struct and some docs
1 parent 1096716 commit 7eb88f8

File tree

5 files changed

+44
-12
lines changed

5 files changed

+44
-12
lines changed

crates/league-toolkit/src/core/meta/bin_tree/object.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ pub struct BinTreeObject {
1414
}
1515

1616
impl BinTreeObject {
17+
/// Reads a BinTreeObject from a reader.
18+
///
19+
/// # Arguments
20+
///
21+
/// * `reader` - A reader that implements io::Read and io::Seek.
22+
/// * `class_hash` - The hash of the class of the object.
23+
/// * `legacy` - Whether to read in legacy format.
1724
pub fn from_reader<R: io::Read + io::Seek + ?Sized>(
1825
reader: &mut R,
1926
class_hash: u32,
@@ -43,6 +50,12 @@ impl BinTreeObject {
4350
Ok(value)
4451
}
4552

53+
/// Writes a BinTreeObject to a writer.
54+
///
55+
/// # Arguments
56+
///
57+
/// * `writer` - A writer that implements io::Write and io::Seek.
58+
/// * `legacy` - Whether to write in legacy format.
4659
pub fn to_writer<W: io::Write + io::Seek + ?Sized>(
4760
&self,
4861
writer: &mut W,
@@ -51,6 +64,7 @@ impl BinTreeObject {
5164
if legacy {
5265
unimplemented!("legacy BinTreeObject write");
5366
}
67+
5468
let size_pos = writer.stream_position()?;
5569
writer.write_u32::<LE>(0)?;
5670

crates/league-toolkit/src/core/meta/bin_tree/read.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ use io_ext::ReaderExt;
99
impl BinTree {
1010
pub const PROP: u32 = u32::from_le_bytes(*b"PROP");
1111
pub const PTCH: u32 = u32::from_le_bytes(*b"PTCH");
12+
13+
/// Reads a BinTree from a reader.
14+
///
15+
/// # Arguments
16+
///
17+
/// * `reader` - A reader that implements io::Read and io::Seek.
1218
pub fn from_reader<R: io::Read + std::io::Seek + ?Sized>(
1319
reader: &mut R,
1420
) -> Result<Self, ParseError> {

crates/league-toolkit/src/core/meta/bin_tree/write.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ use byteorder::{WriteBytesExt, LE};
55
use io_ext::WriterExt;
66

77
impl BinTree {
8+
/// Writes a BinTree to a writer.
9+
///
10+
/// # Arguments
11+
///
12+
/// * `writer` - A writer that implements io::Write and io::Seek.
13+
/// * `legacy` - Whether to write in legacy format.
814
pub fn to_writer<W: io::Write + io::Seek + ?Sized>(
915
&self,
1016
writer: &mut W,

crates/league-toolkit/src/core/meta/property/value/optional.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ use crate::core::meta::{
66
};
77
use io_ext::{ReaderExt as _, WriterExt as _};
88

9-
// I am not a fan of the double tagging, but fixing it would be a whole ordeal (and might not be
10-
// possible with static dispatch?).
119
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1210
#[derive(Clone, PartialEq, Debug)]
13-
pub struct OptionalValue(pub BinPropertyKind, pub Option<Box<PropertyValueEnum>>);
11+
pub struct OptionalValue {
12+
kind: BinPropertyKind,
13+
value: Option<Box<PropertyValueEnum>>,
14+
}
1415

1516
impl PropertyValue for OptionalValue {
1617
fn size_no_header(&self) -> usize {
17-
2 + match &self.1 {
18+
2 + match &self.value {
1819
Some(inner) => inner.size_no_header(),
1920
None => 0,
2021
}
@@ -33,13 +34,13 @@ impl ReadProperty for OptionalValue {
3334

3435
let is_some = reader.read_bool()?;
3536

36-
Ok(Self(
37+
Ok(Self {
3738
kind,
38-
match is_some {
39+
value: match is_some {
3940
true => Some(kind.read(reader, legacy)?.into()),
4041
false => None,
4142
},
42-
))
43+
})
4344
}
4445
}
4546
impl WriteProperty for OptionalValue {
@@ -51,9 +52,9 @@ impl WriteProperty for OptionalValue {
5152
if legacy {
5253
unimplemented!("legacy optional write")
5354
}
54-
writer.write_property_kind(self.0)?;
55-
writer.write_bool(self.1.is_some())?;
56-
if let Some(value) = &self.1 {
55+
writer.write_property_kind(self.kind)?;
56+
writer.write_bool(self.value.is_some())?;
57+
if let Some(value) = &self.value {
5758
value.to_writer(writer)?;
5859
}
5960

crates/league-toolkit/src/core/meta/traits.rs

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

33
use super::property::{value::*, BinPropertyKind};
4+
use byteorder::{ReadBytesExt, WriteBytesExt};
45
use enum_dispatch::enum_dispatch;
56

67
const HEADER_SIZE: usize = 5;
8+
9+
/// Trait for property values
710
#[enum_dispatch]
811
pub trait PropertyValue {
912
fn size(&self, include_header: bool) -> usize {
@@ -16,14 +19,15 @@ pub trait PropertyValue {
1619
fn size_no_header(&self) -> usize;
1720
}
1821

22+
/// Trait for reading property values
1923
pub trait ReadProperty: Sized {
2024
fn from_reader<R: io::Read + io::Seek + ?Sized>(
2125
reader: &mut R,
2226
legacy: bool,
2327
) -> Result<Self, crate::core::meta::ParseError>;
2428
}
2529

26-
use byteorder::ReadBytesExt as _;
30+
/// Extension trait for reading property values
2731
pub trait ReaderExt: io::Read {
2832
/// Reads a u8 as a property kind
2933
fn read_property_kind(
@@ -36,6 +40,7 @@ pub trait ReaderExt: io::Read {
3640

3741
impl<R: io::Read + ?Sized> ReaderExt for R {}
3842

43+
/// Trait for writing property values
3944
pub trait WriteProperty: Sized {
4045
fn to_writer<R: io::Write + io::Seek + ?Sized>(
4146
&self,
@@ -44,7 +49,7 @@ pub trait WriteProperty: Sized {
4449
) -> Result<(), io::Error>;
4550
}
4651

47-
use byteorder::WriteBytesExt as _;
52+
/// Extension trait for writing property kinds
4853
pub trait WriterExt: io::Write {
4954
fn write_property_kind(&mut self, kind: BinPropertyKind) -> Result<(), io::Error> {
5055
self.write_u8(kind.into())

0 commit comments

Comments
 (0)