Snippet

Enum Snippet 

Source
#[repr(u8)]
pub enum Snippet { At(Boundary), From(Boundary), To(Boundary), Between { start: Boundary, end: Boundary, }, All, }
Expand description

Specifies a text range through boundary markers or positions.

The exceptions are the Between variant (which takes two Boundary arguments for the start and end; and the All variant (which takes no Boundary argument, because it implies the entire file. The From variant implies an end position of End Of File, and the To variant implies a start position of the Beginning Of File.

Variants§

§

At(Boundary)

Targets a single boundary location.

§

From(Boundary)

Selects from a boundary to end of file.

§

To(Boundary)

Selects from beginning of file to a boundary.

§

Between

Selects the range between two boundaries.

Fields

§start: Boundary

Starting boundary of the range.

§end: Boundary

Ending boundary of the range.

§

All

Selects the entire file.

Implementations§

Source§

impl Snippet

Source

pub fn replace( &self, rope: &Rope, replacement: &str, ) -> Result<Rope, SnippetError>

Replaces the text selected by this snippet with the given replacement string.

This method resolves the snippet’s boundaries to determine the target range, validates the replacement text, and returns a new Rope with the replacement applied.

§Behavior by Snippet Type
  • Zero-width range (start == end): Performs insertion at the position
  • Empty replacement: Performs deletion of the selected range
  • Non-empty replacement on non-zero range: Performs edit (replace existing text)
§Arguments
  • rope - The rope containing the text to modify
  • replacement - The string to insert at the resolved position
§Returns

Returns a new Rope with the replacement applied.

§Errors

Returns SnippetError::BoundaryError if the snippet’s boundaries cannot be resolved. Returns SnippetError::InvalidRange if the resolved range is invalid (start >= end). Returns SnippetError::InvalidUtf8 if the replacement string contains null bytes. Returns SnippetError::OutOfBounds if the resolved range exceeds rope length.

§Examples

Insert text at a position:

use textum::{Snippet, Target, Boundary, BoundaryMode};
use ropey::Rope;

let rope = Rope::from_str("hello world");
let target = Target::Char(4); // The o in "hello"
let boundary = Boundary::new(target, BoundaryMode::Exclude); // Exclude the o
let snippet = Snippet::At(boundary);

let result = snippet.replace(&rope, ", beautiful").unwrap();
assert_eq!(result.to_string(), "hello, beautiful world");

Delete a line:

use textum::{Snippet, Target, Boundary, BoundaryMode};
use ropey::Rope;

let rope = Rope::from_str("line1\nline2\nline3\n");
let target = Target::Line(1); // Second line
let boundary = Boundary::new(target, BoundaryMode::Include);
let snippet = Snippet::At(boundary);

let result = snippet.replace(&rope, "").unwrap();
assert_eq!(result.to_string(), "line1\nline3\n");

Replace text between boundaries:

use textum::{Snippet, Target, Boundary, BoundaryMode};
use ropey::Rope;

let rope = Rope::from_str("<!-- comment -->text<!-- /comment -->");
let start_target = Target::Literal("<!-- comment -->".to_string());
let end_target = Target::Literal("<!-- /comment -->".to_string());
let start_boundary = Boundary::new(start_target, BoundaryMode::Exclude);
let end_boundary = Boundary::new(end_target, BoundaryMode::Exclude);
let snippet = Snippet::Between { start: start_boundary, end: end_boundary };

let result = snippet.replace(&rope, "new content").unwrap();
assert_eq!(result.to_string(), "<!-- comment -->new content<!-- /comment -->");
Source§

impl Snippet

Source

pub fn resolve(&self, rope: &Rope) -> Result<SnippetResolution, SnippetError>

Resolves this snippet into absolute character indices.

§Errors

Returns SnippetError if boundaries cannot be resolved or the resulting range is invalid.

Trait Implementations§

Source§

impl Clone for Snippet

Source§

fn clone(&self) -> Snippet

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Snippet

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Hash for Snippet

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Snippet

Source§

fn eq(&self, other: &Snippet) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Snippet

Source§

impl StructuralPartialEq for Snippet

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> 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.