#[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.
All
Selects the entire file.
Implementations§
Source§impl Snippet
impl Snippet
Sourcepub fn replace(
&self,
rope: &Rope,
replacement: &str,
) -> Result<Rope, SnippetError>
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 modifyreplacement- 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
impl Snippet
Sourcepub fn resolve(&self, rope: &Rope) -> Result<SnippetResolution, SnippetError>
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.