diff options
| author | Valentin Popov <valentin@popov.link> | 2026-02-10 01:58:16 +0300 |
|---|---|---|
| committer | Valentin Popov <valentin@popov.link> | 2026-02-10 01:58:16 +0300 |
| commit | e08b5f3853784e2fb8dc016d4a149c1a2282f127 (patch) | |
| tree | 0308096ae68dde3977bf18d360064638043257fc /crates/nres/src/data.rs | |
| parent | 5a97f2e42910f552cde0cda3561f4259cd200147 (diff) | |
| download | fparkan-e08b5f3853784e2fb8dc016d4a149c1a2282f127.tar.xz fparkan-e08b5f3853784e2fb8dc016d4a149c1a2282f127.zip | |
feat: add initial implementation of rsli crate
- Created Cargo.toml for the rsli crate with flate2 dependency.
- Implemented ResourceData enum for handling borrowed and owned byte slices.
- Added OutputBuffer trait and its Vec<u8> implementation for writing data.
- Defined a comprehensive Error enum for error handling in the library.
- Developed the Library struct to manage resource entries and provide methods for loading and unpacking resources.
- Implemented various packing methods and decompression algorithms, including LZSS and Deflate.
- Added tests for validating the functionality of the rsli library against sample data.
Diffstat (limited to 'crates/nres/src/data.rs')
| -rw-r--r-- | crates/nres/src/data.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/crates/nres/src/data.rs b/crates/nres/src/data.rs new file mode 100644 index 0000000..bb9e778 --- /dev/null +++ b/crates/nres/src/data.rs @@ -0,0 +1,43 @@ +use std::io; + +/// Resource payload that can be either borrowed from mapped bytes or owned. +#[derive(Clone, Debug)] +pub enum ResourceData<'a> { + Borrowed(&'a [u8]), + Owned(Vec<u8>), +} + +impl<'a> ResourceData<'a> { + pub fn as_slice(&self) -> &[u8] { + match self { + Self::Borrowed(slice) => slice, + Self::Owned(buf) => buf.as_slice(), + } + } + + pub fn into_owned(self) -> Vec<u8> { + match self { + Self::Borrowed(slice) => slice.to_vec(), + Self::Owned(buf) => buf, + } + } +} + +impl AsRef<[u8]> for ResourceData<'_> { + fn as_ref(&self) -> &[u8] { + self.as_slice() + } +} + +/// Output sink used by `read_into`/`load_into` APIs. +pub trait OutputBuffer { + fn write_exact(&mut self, data: &[u8]) -> io::Result<()>; +} + +impl OutputBuffer for Vec<u8> { + fn write_exact(&mut self, data: &[u8]) -> io::Result<()> { + self.clear(); + self.extend_from_slice(data); + Ok(()) + } +} |
