use owned_str::{Error, OwnedStr}; use ufmt::uWrite; pub struct OwnedStrWriter(OwnedStr); impl OwnedStrWriter { pub fn new() -> Self { Self(OwnedStr::new()) } pub fn push(&mut self, c: char) -> Result<(), Error> { self.0.try_push(c).map(|_| ()) } } impl From> for OwnedStrWriter { fn from(owned_str: OwnedStr) -> Self { Self(owned_str) } } impl From> for OwnedStr { fn from(writer: OwnedStrWriter) -> Self { writer.0 } } impl uWrite for OwnedStrWriter { type Error = owned_str::Error; fn write_str(&mut self, s: &str) -> Result<(), Self::Error> { self.0.try_push_str(s).map(|_| ()) } fn write_char(&mut self, c: char) -> Result<(), Self::Error> { self.0.try_push(c).map(|_| ()) } } pub fn center_str( str: &str, width: usize, ) -> Result, owned_str::Error> { let mut res = OwnedStr::new(); let len = str.len(); let padding = (width.saturating_sub(len) + 1) / 2; for _ in 0..padding { res.try_push(' ')?; } res.try_push_str(str)?; Ok(res) }