cleaned up code, condensed code and fixed a couple of things
This commit is contained in:
parent
40ca10651f
commit
38bcad37bd
10 changed files with 178 additions and 95 deletions
|
|
@ -1,6 +1,6 @@
|
|||
use std::io::{Read, Seek};
|
||||
use std::io::{Read, Seek, Write};
|
||||
use crate::endianness::{IS_BIG_ENDIAN, ReadExtensions};
|
||||
use super::{string, Result};
|
||||
use super::{string, Result, RmcSerialize};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Any{
|
||||
|
|
@ -8,21 +8,26 @@ pub struct Any{
|
|||
pub data: Vec<u8>
|
||||
}
|
||||
|
||||
pub fn read(reader: &mut (impl Read + Seek)) -> Result<Any>{
|
||||
let name = string::read(reader)?;
|
||||
impl RmcSerialize for Any{
|
||||
fn serialize(&self, writer: &mut dyn Write) -> Result<()> {
|
||||
todo!()
|
||||
}
|
||||
fn deserialize(mut reader: &mut dyn Read) -> Result<Self> {
|
||||
let name = String::deserialize(reader)?;
|
||||
|
||||
// also length ?
|
||||
let len2: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
let length: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
// also length ?
|
||||
let len2: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
let length: u32 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
|
||||
let mut data = vec![0; length as usize];
|
||||
let mut data = vec![0; length as usize];
|
||||
|
||||
reader.read_exact(&mut data)?;
|
||||
reader.read_exact(&mut data)?;
|
||||
|
||||
Ok(
|
||||
Any{
|
||||
name,
|
||||
data
|
||||
}
|
||||
)
|
||||
Ok(
|
||||
Any{
|
||||
name,
|
||||
data
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
use std::io;
|
||||
use std::io::{Read, Seek, Write};
|
||||
use std::str::Utf8Error;
|
||||
use std::string::FromUtf8Error;
|
||||
use md5::digest::impl_oid_carrier;
|
||||
use thiserror::Error;
|
||||
|
||||
//ideas for the future: make a proc macro library which allows generation of struct reads
|
||||
|
|
@ -16,4 +18,9 @@ pub enum Error{
|
|||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
pub mod string;
|
||||
pub mod any;
|
||||
pub mod any;
|
||||
|
||||
pub trait RmcSerialize: Sized{
|
||||
fn serialize(&self, writer: &mut dyn Write) -> Result<()>;
|
||||
fn deserialize(reader: &mut dyn Read) -> Result<Self>;
|
||||
}
|
||||
|
|
@ -1,18 +1,30 @@
|
|||
use std::ffi::CString;
|
||||
use std::io::{Read, Seek};
|
||||
use std::io::{Read, Seek, Write};
|
||||
use bytemuck::bytes_of;
|
||||
use log::error;
|
||||
use crate::endianness::{IS_BIG_ENDIAN, ReadExtensions};
|
||||
use super::Result;
|
||||
use super::{Result, RmcSerialize};
|
||||
|
||||
pub fn read(reader: &mut (impl Read + Seek)) -> Result<String>{
|
||||
let len: u16 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
let mut data = vec![0; len as usize - 1];
|
||||
reader.read_exact(&mut data)?;
|
||||
impl RmcSerialize for String{
|
||||
fn deserialize(mut reader: &mut dyn Read) -> Result<Self> {
|
||||
let len: u16 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
let mut data = vec![0; len as usize - 1];
|
||||
reader.read_exact(&mut data)?;
|
||||
|
||||
let null: u8 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
if null != 0{
|
||||
error!("unable to find null terminator... continuing anyways");
|
||||
let null: u8 = reader.read_struct(IS_BIG_ENDIAN)?;
|
||||
if null != 0{
|
||||
error!("unable to find null terminator... continuing anyways");
|
||||
}
|
||||
|
||||
Ok(String::from_utf8(data)?)
|
||||
}
|
||||
fn serialize(&self, writer: &mut dyn Write) -> Result<()> {
|
||||
let u16_len: u16 = self.len() as u16;
|
||||
writer.write(bytes_of(&u16_len))?;
|
||||
|
||||
Ok(String::from_utf8(data)?)
|
||||
writer.write(self.as_bytes())?;
|
||||
writer.write(&[0])?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue