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:
parent
427563bb55
commit
fedfccacda
1 changed files with 6 additions and 5 deletions
11
src/data.rs
11
src/data.rs
|
|
@ -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)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue