impl Decoder

This commit is contained in:
mykola2312 2023-08-26 18:32:08 +03:00
parent 22fee005cd
commit f1ee4a3d29
5 changed files with 38 additions and 1 deletions

View file

@ -1,2 +1,3 @@
mod raw;
mod decoder;
pub mod save;

25
src/fot/decoder.rs Normal file
View file

@ -0,0 +1,25 @@
use std::str;
use anyhow::anyhow;
use anyhow::Result;
use crate::fot::raw::Raw;
pub trait Decoder: Sized {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self>;
fn encode(&self) -> Raw;
}
impl Decoder for String {
fn decode(raw: &Raw, offset: usize, size: usize) -> Result<Self> {
let str = &raw.mem[offset..];
match str.iter().position(|&c| c == 0) {
Some(pos) => Ok(str::from_utf8(&str[..pos])?.to_string()),
None => Ok(str::from_utf8(&raw.mem[offset..offset+size])?.to_string())
}
}
fn encode(&self) -> Raw {
let mut str = self.clone().into_bytes();
str.push(0);
Raw { offset: 0, size: str.len(), mem: str}
}
}

View file

@ -7,6 +7,7 @@ use std::path::Path;
use memmem::{Searcher, TwoWaySearcher};
use anyhow::Result;
#[derive(Debug)]
pub struct Raw {
pub offset: usize,
pub size: usize,

View file

@ -7,7 +7,9 @@ use anyhow::anyhow;
use anyhow::Result;
use inflate::inflate_bytes_zlib;
use deflate::deflate_bytes_zlib;
use crate::fot::decoder::Decoder;
use crate::fot::raw::Raw;
use crate::fot::decoder;
#[derive(Debug)]
pub struct World {
@ -98,4 +100,11 @@ impl Save {
Ok(())
}
pub fn test(&self) -> Result<()> {
let a = "hello".to_string().encode();
dbg!(a);
Ok(())
}
}

View file

@ -18,5 +18,6 @@ fn main() {
println!("World {:x} size {}", w.offset, w.size);
}
save.save(Path::new("out.sav")).expect("failed to save");
//save.save(Path::new("out.sav")).expect("failed to save");
save.test().expect("test");
}