Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(nosapi_data): remove FileParser trait #24

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 12 additions & 14 deletions crates/nosapi_data/src/nos/text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{Read, Seek, SeekFrom};
use std::io::{Cursor, Read, Seek, SeekFrom};

use byteorder::{LittleEndian, ReadBytesExt};
use chrono::{DateTime, TimeZone, Utc};
Expand All @@ -7,7 +7,7 @@ use derivative::Derivative;
use crate::nos::decrypt::{NOSFileDecryptor, NOSTextFileDataDecryptor, NOSTextFileSimpleDecryptor};
use crate::nos::error::NOSFileError;
use crate::nos::NOSFileType;
use crate::traits::{FileParser, ReadExt};
use crate::traits::ReadExt;

static OLE_TIME_CHECK: [u8; 4] = [0xEE, 0x3E, 0x32, 0x01];

Expand Down Expand Up @@ -35,13 +35,12 @@ pub struct NOSTextFileEntry {
pub raw_content: Vec<u8>,
}

impl FileParser for NOSTextFile {
type Error = NOSFileError;
impl NOSTextFile {
pub fn from_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> Result<Self, NOSFileError> {
Self::from_reader(&mut Cursor::new(bytes.as_ref()))
}

fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, Self::Error>
where
Self: Sized,
{
fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, NOSFileError> {
let file_type = NOSFileType::from_reader(reader)?;

if file_type != NOSFileType::Text {
Expand Down Expand Up @@ -81,13 +80,12 @@ impl FileParser for NOSTextFile {
}
}

impl FileParser for NOSTextFileEntry {
type Error = NOSFileError;
impl NOSTextFileEntry {
pub fn from_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> Result<Self, NOSFileError> {
Self::from_reader(&mut Cursor::new(bytes.as_ref()))
}

fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, Self::Error>
where
Self: Sized,
{
pub fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, NOSFileError> {
let file_number = reader.read_i32::<LittleEndian>()?;
let name_size = reader.read_i32::<LittleEndian>()? as usize;
let name = reader.read_to_utf8_sized(name_size)?;
Expand Down
15 changes: 3 additions & 12 deletions crates/nosapi_data/src/nos/type.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io::{Read, Seek};

use crate::nos::error::NOSFileHeaderError;
use crate::traits::FileParser;

static DATA_FILE_HEADER: [&str; 3] = ["NT Data", "32GBS V1.0", "ITEMS V1.0"];
static CCINF_FILE_HEADER: &str = "CCINF V1.20";
Expand All @@ -13,13 +12,8 @@ pub enum NOSFileType {
CCInf,
}

impl FileParser for NOSFileType {
type Error = NOSFileHeaderError;

fn from_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> Result<Self, Self::Error>
where
Self: Sized,
{
impl NOSFileType {
pub fn from_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> Result<Self, NOSFileHeaderError> {
let bytes = bytes.as_ref();

if bytes.len() != 0x0B {
Expand All @@ -42,10 +36,7 @@ impl FileParser for NOSFileType {
})
}

fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, Self::Error>
where
Self: Sized,
{
pub fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, NOSFileHeaderError> {
let mut buf = [0u8; 0x0B];
reader
.read_exact(&mut buf)
Expand Down
3 changes: 2 additions & 1 deletion crates/nosapi_data/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub use crate::traits::FileParser;
pub use crate::client::error::*;
pub use crate::nos::error::*;
18 changes: 1 addition & 17 deletions crates/nosapi_data/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::error::Error;
use std::io;
use std::io::{Cursor, Read, Seek};
use std::io::Read;

pub(crate) trait ReadExt: Read {
fn read_to_utf8_sized(&mut self, size: usize) -> io::Result<String> {
Expand All @@ -11,19 +10,4 @@ pub(crate) trait ReadExt: Read {
}
}

pub trait FileParser {
type Error: Error;

fn from_bytes<T: AsRef<[u8]> + ?Sized>(bytes: &T) -> Result<Self, Self::Error>
where
Self: Sized,
{
Self::from_reader(&mut Cursor::new(bytes.as_ref()))
}

fn from_reader<T: Read + Seek>(reader: &mut T) -> Result<Self, Self::Error>
where
Self: Sized;
}

impl<T> ReadExt for T where T: Read {}
1 change: 0 additions & 1 deletion crates/nosapi_data/tests/nos/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use sha1_smol::Sha1;

use nosapi_data::nos::error::NOSFileError;
use nosapi_data::nos::{NOSTextFile, NOSTextFileEntry};
use nosapi_data::prelude::*;

static FILE_PATH: &str = "./tests/fixtures/NSgtdData.NOS";
static LST_FILE_SHA1: &str = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
Expand Down
1 change: 0 additions & 1 deletion crates/nosapi_data/tests/nos/type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use nosapi_data::nos::error::NOSFileHeaderError;
use nosapi_data::nos::{NOSDataType, NOSFileType};
use nosapi_data::prelude::*;

#[test]
fn handles_invalid_header_size() {
Expand Down
Loading