implement Display, PartialEq and Borrow for FString. May need to complete it so indexing by &str would be possible

This commit is contained in:
mykola2312 2023-08-31 13:46:20 +03:00
parent a5d44bd730
commit 4df2803fdb
3 changed files with 27 additions and 6 deletions

View file

@ -5,8 +5,7 @@ use super::stream::{ReadStream, WriteStream};
use super::tag::Tag;
use anyhow::Result;
use indexmap::IndexMap;
use std::fmt::{self, write};
use std::path::Display;
use std::fmt;
#[derive(Debug)]
pub struct ESHUnknown {
@ -221,8 +220,8 @@ impl fmt::Display for ESHValue {
ESHValue::Bool(val) => write!(f, "{}", val),
ESHValue::Float(val) => write!(f, "{}", val),
ESHValue::Int(val) => write!(f, "{}", val),
ESHValue::String(str) => write!(f, "{}", str.str),
ESHValue::Sprite(spr) => write!(f, "{}", spr.str),
ESHValue::String(str) => write!(f, "{}", str),
ESHValue::Sprite(spr) => write!(f, "{}", spr),
ESHValue::Binary(bin) => write!(f, "Binary, size {}", bin.len()),
ESHValue::EntityFlags(val) => {
write!(f, "entity {} flags {:x}", val.entity_id, val.flags)

View file

@ -4,6 +4,8 @@ use anyhow::Result;
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use encoding_rs::WINDOWS_1251;
use std::io::Cursor;
use std::borrow::Borrow;
use std::fmt;
// FString - Fallout
@ -13,7 +15,7 @@ pub enum FStringEncoding {
WCS2,
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
#[derive(Debug, Hash, Eq, Clone)]
pub struct FString {
pub encoding: FStringEncoding,
pub enc_len: usize,
@ -79,3 +81,21 @@ impl Decoder for FString {
}
}
}
impl PartialEq for FString {
fn eq(&self, other: &Self) -> bool {
self.str == other.str
}
}
impl Borrow<str> for FString {
fn borrow(&self) -> &str {
self.str.as_str()
}
}
impl fmt::Display for FString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "\"{}\"", self.str)
}
}

View file

@ -1,6 +1,7 @@
use super::decoder::Decoder;
use super::esh::ESH;
use super::fstring::FString;
use super::fstring::FStringEncoding;
use super::raw::Raw;
use super::sgd::SGD;
use super::ssg::SSG;
@ -34,8 +35,9 @@ impl World {
let mut rd = ReadStream::new(&self.data, 0x14AC);
let esh: ESH = rd.read(0)?;
for (name, value) in esh.props.iter() {
println!("{}\t{}", name.str, value);
println!("{}\t{}", name, value);
}
dbg!(&esh.props["Display Name"]);
Ok(())
}