pub struct SnippetBuilder { /* private fields */ }
Expand description
§The Snippet Builder
🏗️ A builder pattern implementation for creating Snippet instances with elegant fluent interface. Think of it as your personal snippet butler.
§Features
- 🎯 Fluent interface for natural snippet construction
- 🔄 Automatic unique name generation
- 📝 Rich body manipulation methods
- ⚡ Validation before building
§Usage
let snippet = SnippetBuilder::new()
.set_prefix("fn main")
.add_line("fn main() {")
.add_line(" println!(\"Hello, World!\");")
.add_line("}")
.build()
.unwrap();
§🛠️ Advanced Usage
let snippet = SnippetBuilder::new()
.set_prefix("test")
.set_body(vec![
"#[test]",
"fn test_${1}() {",
" ${0:// Test code}",
"}"
])
.set_description("Test function template")
.set_scope("rust")
.set_priority(10)
.build()
.unwrap();
§🔧 Body Manipulation
let snippet = SnippetBuilder::new()
.set_prefix("hello")
.add_line("print!(\"Hello, world!\");")
.map_body(|lines| {
lines.insert(0, "// Hello world".to_owned());
})
.map_line(1, |line| {
*line = line.replace("print!", "println!");
})
.build()
.unwrap();
§Methods
§🏷️ Core Methods:
new()
- Creates new builder instancebuild()
- Constructs final Snippetvalidate()
- Checks builder state
§📝 Content Setting:
set_name(name)
- Sets snippet nameset_prefix(prefix)
- Sets trigger textset_description(desc)
- Sets descriptionset_scope(scope)
- Sets language scopeset_priority(prio)
- Sets suggestion priority
§📄 Body Manipulation:
set_body(lines)
- Sets entire body contentadd_line(line)
- Adds single lineadd_lines(lines)
- Adds multiple linesset_line(n, line)
- Changes specific linemap_body(fn)
- Transforms entire bodymap_line(n, fn)
- Transforms specific line
§⚠️ Validation Rules
Builder will fail if:
- Name is empty
- Prefix is empty
- Body is empty
- Line index is out of bounds
§🎯 Best Practices
- Always set meaningful prefix
- Add description for clarity
- Use scope for language-specific snippets
- Consider priority for frequently used snippets
§🔄 Default Values
name
: Auto-generated unique IDprefix
: Empty stringbody
: Empty vector- Other fields: None
Implementations§
Source§impl SnippetBuilder
impl SnippetBuilder
Sourcepub fn set_prefix<S: Into<String>>(self, prefix: S) -> Self
pub fn set_prefix<S: Into<String>>(self, prefix: S) -> Self
Sets the prefix (trigger) of the snippet
Sourcepub fn set_body<S: Into<String>>(self, body: Vec<S>) -> Self
pub fn set_body<S: Into<String>>(self, body: Vec<S>) -> Self
Sets the entire body of the snippet
Sourcepub fn map_body<F>(self, f: F) -> Self
pub fn map_body<F>(self, f: F) -> Self
Map snippet body using a transformation function that gets mutable reference
Sourcepub fn add_lines<S: Into<String>>(
self,
lines: impl IntoIterator<Item = S>,
) -> Self
pub fn add_lines<S: Into<String>>( self, lines: impl IntoIterator<Item = S>, ) -> Self
Adds multiple lines to the snippet body
Sourcepub fn set_line<S: Into<String>>(self, n: usize, line: S) -> Result<Self>
pub fn set_line<S: Into<String>>(self, n: usize, line: S) -> Result<Self>
Edits a specific line in the snippet body
Sourcepub fn map_line<F>(self, n: usize, f: F) -> Result<Self>
pub fn map_line<F>(self, n: usize, f: F) -> Result<Self>
Map specific line using a transformation function
Sourcepub fn set_description<S: Into<String>>(self, description: S) -> Self
pub fn set_description<S: Into<String>>(self, description: S) -> Self
Sets the description of the snippet
Sourcepub fn set_is_file_template(self, is_template: bool) -> Self
pub fn set_is_file_template(self, is_template: bool) -> Self
Sets whether this snippet is a file template
Sourcepub fn set_priority(self, priority: u32) -> Self
pub fn set_priority(self, priority: u32) -> Self
Sets the priority of the snippet
Trait Implementations§
Source§impl Clone for SnippetBuilder
impl Clone for SnippetBuilder
Source§fn clone(&self) -> SnippetBuilder
fn clone(&self) -> SnippetBuilder
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for SnippetBuilder
impl Debug for SnippetBuilder
Source§impl Default for SnippetBuilder
impl Default for SnippetBuilder
Source§impl From<SnippetBuilder> for Snippet
impl From<SnippetBuilder> for Snippet
Source§fn from(value: SnippetBuilder) -> Self
fn from(value: SnippetBuilder) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for SnippetBuilder
impl RefUnwindSafe for SnippetBuilder
impl Send for SnippetBuilder
impl Sync for SnippetBuilder
impl Unpin for SnippetBuilder
impl UnwindSafe for SnippetBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more