Keep size of names

This commit is contained in:
Jaby 2022-10-11 19:41:23 +02:00
parent 58ab918eca
commit c27e62709f
1 changed files with 18 additions and 14 deletions

View File

@ -98,24 +98,27 @@ impl std::fmt::Display for File {
pub struct DirectoryName { pub struct DirectoryName {
name: DString<8>, name: DString<8>,
len: usize,
} }
impl DirectoryName { impl DirectoryName {
pub fn from_str(dir_name: &str) -> Result<DirectoryName, Error> { pub fn from_str(dir_name: &str) -> Result<DirectoryName, Error> {
let dir_name = dir_name.to_uppercase(); let dir_name = dir_name.to_uppercase();
Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?}) Ok(DirectoryName{name: DString::from_str(dir_name.as_ref())?, len: dir_name.len()})
} }
} }
impl std::fmt::Display for DirectoryName { impl std::fmt::Display for DirectoryName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", dstring_as_str(&self.name)) write!(f, "{}", dstring_as_str(&self.name, self.len))
} }
} }
pub struct FileName { pub struct FileName {
name: DString<8>, name: DString<8>,
ext: Option<DString<3>> len: usize,
ext: Option<DString<3>>,
ext_len: Option<usize>
} }
impl FileName { impl FileName {
@ -125,24 +128,25 @@ impl FileName {
let mut sub_str = file_name.split('.'); let mut sub_str = file_name.split('.');
(Error::ok_or_new(sub_str.next(), || "File name can't be emplty".to_owned())?, sub_str.next()) (Error::ok_or_new(sub_str.next(), || "File name can't be emplty".to_owned())?, sub_str.next())
}; };
let (ext, ext_len) = {
Ok(FileName{name: DString::from_str(name)?, ext: {
match ext { match ext {
Some(ext) => Some(DString::from_str(ext)?), Some(ext) => (Some(DString::from_str(ext)?), Some(ext.len())),
None => None None => (None, None)
} }
}}) };
Ok(FileName{name: DString::from_str(name)?, len: name.len(), ext, ext_len})
} }
} }
impl std::fmt::Display for FileName { impl std::fmt::Display for FileName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(ext) = &self.ext { if let Some(ext) = &self.ext {
write!(f, "{}.{};1", dstring_as_str(&self.name), dstring_as_str(&ext)) write!(f, "{}.{};1", dstring_as_str(&self.name, self.len), dstring_as_str(&ext, self.ext_len.unwrap_or(0)))
} }
else { else {
write!(f, "{}", dstring_as_str(&self.name)) write!(f, "{}", dstring_as_str(&self.name, self.len))
} }
} }
} }
@ -159,8 +163,8 @@ impl Default for Properties {
} }
} }
fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>) -> &str { fn dstring_as_str<const SIZE: usize>(string: &DString<SIZE>, len: usize) -> &str {
match std::str::from_utf8(string.as_raw()) { match std::str::from_utf8(&string.as_raw()[0..len]) {
Ok(str) => str, Ok(str) => str,
Err(_) => "???", Err(_) => "???",
} }