use std::fmt;
use crate::toc_error::TocError;
#[derive(Default, Debug, Clone, PartialEq)]
pub(crate) struct TocString {
pub(crate) opt: Option<Vec<u8>>
}
impl TocString {
pub(crate) fn new(buf: Vec<u8>) -> Self {
Self {
opt: Some(buf)
}
}
pub(crate) fn none() -> Self {
Self {
opt: None
}
}
pub(crate) fn empty() -> Self {
Self {
opt: Some(Vec::with_capacity(0usize))
}
}
pub(crate) fn from_string(st: String) -> Self {
Self {
opt: Some(st.into_bytes())
}
}
pub(crate) fn from_string_opt(opt: &Option<String>) -> Self {
Self {
opt: opt.clone().map(|st| st.into_bytes())
}
}
pub(crate) fn from_str(st: &str) -> Self {
Self {
opt: Some(st.to_string().into_bytes())
}
}
pub(crate) fn to_string(&self) -> Result<String, TocError> {
let res = match &self.opt {
Some(bin) => String::from_utf8(bin.clone())?,
None => "".to_string()
};
Ok(res)
}
pub(crate) fn to_string_lossy(&self) -> String {
match &self.opt {
Some(bin) => {
String::from_utf8_lossy(bin.as_slice()).to_string()
},
None => "".to_string()
}
}
pub(crate) fn to_string_opt(&self) -> Result<Option<String>, TocError> {
let res = match &self.opt {
Some(bin) => Some(String::from_utf8(bin.clone())?),
None => None
};
Ok(res)
}
}
impl fmt::Display for TocString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string_lossy())?;
Ok(())
}
}