1
0
Fork 0
mirror of https://codeberg.org/icewind/vbsp.git synced 2026-06-03 18:54:05 +02:00

name -> fixed string

This commit is contained in:
Robin Appelman 2022-02-20 16:37:20 +01:00
commit fedfccacda

View file

@ -160,16 +160,17 @@ bitflags! {
} }
} }
/// Fixed length, null-terminated string
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Name(ArrayString<64>); pub struct FixedString<const LEN: usize>(ArrayString<LEN>);
impl Display for Name { impl<const LEN: usize> Display for FixedString<LEN> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f) Display::fmt(&self.0, f)
} }
} }
impl BinRead for Name { impl<const LEN: usize> BinRead for FixedString<LEN> {
type Args = (); type Args = ();
fn read_options<R: binrw::io::Read + binrw::io::Seek>( fn read_options<R: binrw::io::Read + binrw::io::Seek>(
@ -179,7 +180,7 @@ impl BinRead for Name {
) -> BinResult<Self> { ) -> BinResult<Self> {
use std::str; use std::str;
let name_buf = <[u8; 64]>::read_options(reader, options, args)?; let name_buf = <[u8; LEN]>::read_options(reader, options, args)?;
let zero_pos = let zero_pos =
name_buf name_buf
@ -190,7 +191,7 @@ impl BinRead for Name {
err: Box::new(StringError::NotNullTerminated), err: Box::new(StringError::NotNullTerminated),
})?; })?;
let name = &name_buf[..zero_pos]; let name = &name_buf[..zero_pos];
Ok(Name( Ok(FixedString(
ArrayString::from( ArrayString::from(
str::from_utf8(name) str::from_utf8(name)
.map_err(StringError::NonUTF8) .map_err(StringError::NonUTF8)