Struct Editor

Source
pub struct Editor { /* private fields */ }
Expand description

Handle to a UTF-8 text file kept in memory until save is called.

All mutating methods return &mut self, enabling a fluent builder style.

Editor::open("Cargo.toml")?
    .insert_before("[dependencies]", "regex = \"1\"\n", false)
    .save()?;

Implementations§

Source§

impl Editor

Source

pub fn create<P: AsRef<Path>>(path: P) -> Result<Self>

Create or truncate a file and return an editor over it.

Equivalent to fs::write(path, "") followed by open.

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open an existing UTF-8 file into an in-memory buffer.

Source

pub fn rename<P: AsRef<Path>>(&mut self, new_name: P) -> Result<&mut Self>

Rename the underlying file on disk and update the internal path.

Source

pub fn save(&mut self) -> Result<&mut Self>

Write the in-memory buffer back to disk iff it was modified.

Returns Ok(self) even when there was nothing to do.

Source

pub fn prepend(&mut self, text: &str) -> &mut Self

Insert text at the beginning of the buffer.

Source

pub fn append(&mut self, text: &str) -> &mut Self

Append text to the end of the buffer.

Source

pub fn insert_before( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self

Insert text before the first occurrence of marker.

  • If same_indent is true, the current indentation of the line containing marker is copied and prepended to text.
Source

pub fn insert_after( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self

Insert text after the first occurrence of marker.

  • If marker ends a line, the insertion starts on the next line.
  • Otherwise the insertion is in-line; a space is auto-inserted when needed.
  • When same_indent is true, every subsequent line in text is indented to match the marker line.
Source

pub fn replace_marker( &mut self, marker: &str, text: &str, same_indent: bool, ) -> &mut Self

Replace the first occurrence of marker with text.

When same_indent is true, the replacement receives the indentation that preceded the marker.

Source

pub fn find_lines<'a, P>(&self, pattern: P, limit: Option<usize>) -> Vec<usize>
where P: Into<Pattern<'a>>,

Return 1-based line numbers where pattern occurs.

Pass limit = Some(n) to cap the results.

Source

pub fn erase<'a, P>(&mut self, pattern: P) -> &mut Self
where P: Into<Pattern<'a>>,

Erase all occurrences of pattern.

Source

pub fn replace<'a, P>(&mut self, pattern: P, replacement: &str) -> &mut Self
where P: Into<Pattern<'a>>,

Replace all occurrences of pattern with replacement.

Source

pub fn mask<'a, P>(&mut self, pattern: P, mask: &str) -> &mut Self
where P: Into<Pattern<'a>>,

Mask all occurrences of pattern with mask.

Trait Implementations§

Source§

impl Clone for Editor

Source§

fn clone(&self) -> Editor

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Editor

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Editor

§

impl RefUnwindSafe for Editor

§

impl Send for Editor

§

impl Sync for Editor

§

impl Unpin for Editor

§

impl UnwindSafe for Editor

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.