use crate::error::Error;
use crate::RefactoryBuffer;
pub struct RefactoryString<'a>(RefactoryBuffer<'a>);
impl<'a> RefactoryString<'a> {
pub fn new(content: &'a str) -> RefactoryString<'a> {
RefactoryString(RefactoryBuffer::new(content.as_bytes()))
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn to_string(&self) -> Result<String, Error> {
return String::from_utf8(self.0.to_bytes()?).map_err(|_| Error::InvalidInternalState);
}
pub fn append_left(&mut self, index: usize, content: &str) -> Result<(), Error> {
self.0.append_left(index, content.as_bytes())
}
pub fn prepend_left(&mut self, index: usize, content: &str) -> Result<(), Error> {
self.0.prepend_left(index, content.as_bytes())
}
pub fn append_right(&mut self, index: usize, content: &str) -> Result<(), Error> {
self.0.append_right(index, content.as_bytes())
}
pub fn prepend_right(&mut self, index: usize, content: &str) -> Result<(), Error> {
self.0.prepend_right(index, content.as_bytes())
}
pub fn prepend(&mut self, content: &str) -> Result<(), Error> {
self.prepend_left(0, content)
}
pub fn append(&mut self, content: &str) -> Result<(), Error> {
self.append_right(self.len(), content)
}
pub fn overwrite(&mut self, start: usize, end: usize, content: &str) -> Result<(), Error> {
self.remove(start, end)?;
self.append_left(start, content)?;
Ok(())
}
pub fn remove(&mut self, start: usize, end: usize) -> Result<(), Error> {
self.0.remove(start, end)
}
}