Skip to main content

PieceTable

Struct PieceTable 

Source
pub struct PieceTable { /* private fields */ }

Implementations§

Source§

impl PieceTable

Source

pub fn from_file(path: &Path) -> Result<PieceTable, Error>

Create new PieceTable using a file as read_buffer.

§Examples
use piecetable::PieceTable;
use std::path::Path;

let mut piecetable = PieceTable::from_file(Path::new("test.txt")).unwrap();
§Errors

Possible error when opening file.

Possible error when reading metadata of file.

Source

pub fn from_str(buf: &str) -> PieceTable

Create new PieceTable using a base string as read_buffer.

§Examples
use piecetable::PieceTable;

let mut piecetable = PieceTable::from_str("Buenos dias");
Source

pub fn display_result(&self) -> Result<String, Error>

Join pieces reading from read_buffer and append_buffer.

§Examples
use piecetable::PieceTable;

let piecetable = PieceTable::from_str("Buenos dias");

println!("{}", piecetable.display_result().unwrap());
§Errors

Posible error when reading file to string.

Source

pub fn insert(&mut self, buf: &str, index: usize)

Insert string slice in piece table.

split_index is the global index for the starting character or the new string slice.

§Examples
use piecetable::PieceTable;

let buffer = "Buenos dias, que buen clima hoy";
//                       |
//                      11
let mut piece_table = PieceTable::from_str(buffer);

piece_table.insert(" Matias", 11);

assert_eq!("Buenos dias Matias, que buen clima hoy", piece_table.display_result().unwrap());
Source

pub fn delete_char(&mut self, char_index: usize)

Delete character at position char_index

§Examples
use piecetable::PieceTable;

let buffer = "Mucho gus8to";
//                     |
//                     9
let mut piece_table = PieceTable::from_str(buffer);

piece_table.delete_char(9);

assert_eq!("Mucho gusto", piece_table.display_result().unwrap());
Source

pub fn save_file(&mut self) -> Result<usize, &str>

Save piecetable to file

§Expamples
use piecetable::PieceTable;
use std::{
    path::Path,
    fs,
    io::Write
};

{
    let mut file = fs::File::create("test.txt").unwrap();
    file.write_all(b"Buenos dias").unwrap();
}

let mut piecetable = PieceTable::from_file(Path::new("test.txt")).unwrap();
piecetable.insert(" Matias", 11);

let _file_length: usize = piecetable.save_file().unwrap();
§Errors

Returns error if piecetable does not contain a filepath to save the pieces to.

Source

pub fn save_to_file(&mut self, path: &Path) -> Result<usize, Error>

Save piecetable to new filepath and modify filename of piecetable if it exists.

§Examples
use piecetable::PieceTable;
use std::path::Path;

let mut piecetable = PieceTable::from_str("Buenos dias");
let _new_file_length = piecetable.save_to_file(Path::new("test.txt")).unwrap();
§Errors

Error creating file Error opening file Error reading file Error reading metadata

Trait Implementations§

Source§

impl Debug for PieceTable

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.