Struct SnippetBuilder

Source
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 instance
  • build() - Constructs final Snippet
  • validate() - Checks builder state
§📝 Content Setting:
  • set_name(name) - Sets snippet name
  • set_prefix(prefix) - Sets trigger text
  • set_description(desc) - Sets description
  • set_scope(scope) - Sets language scope
  • set_priority(prio) - Sets suggestion priority
§📄 Body Manipulation:
  • set_body(lines) - Sets entire body content
  • add_line(line) - Adds single line
  • add_lines(lines) - Adds multiple lines
  • set_line(n, line) - Changes specific line
  • map_body(fn) - Transforms entire body
  • map_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 ID
  • prefix: Empty string
  • body: Empty vector
  • Other fields: None

Implementations§

Source§

impl SnippetBuilder

Source

pub fn new() -> Self

Creates a new SnippetBuilder instance

Source

pub fn gen_name() -> String

Generates a an unique name for snippet

Source

pub fn validate(&self) -> Result<()>

Validates the builder state

Source

pub fn build(self) -> Result<Snippet>

Builds the Snippet instance

Source

pub fn set_name<S: Into<String>>(self, name: S) -> Self

Sets the name of the snippet

Source

pub fn set_prefix<S: Into<String>>(self, prefix: S) -> Self

Sets the prefix (trigger) of the snippet

Source

pub fn set_body<S: Into<String>>(self, body: Vec<S>) -> Self

Sets the entire body of the snippet

Source

pub fn map_body<F>(self, f: F) -> Self
where F: FnMut(&mut Vec<String>),

Map snippet body using a transformation function that gets mutable reference

Source

pub fn add_line<S: Into<String>>(self, line: S) -> Self

Adds a single line to the snippet body

Source

pub fn add_lines<S: Into<String>>( self, lines: impl IntoIterator<Item = S>, ) -> Self

Adds multiple lines to the snippet body

Source

pub fn set_line<S: Into<String>>(self, n: usize, line: S) -> Result<Self>

Edits a specific line in the snippet body

Source

pub fn map_line<F>(self, n: usize, f: F) -> Result<Self>
where F: FnMut(&mut String),

Map specific line using a transformation function

Source

pub fn set_description<S: Into<String>>(self, description: S) -> Self

Sets the description of the snippet

Source

pub fn set_scope<S: Into<String>>(self, scope: S) -> Self

Sets the scope of the snippet

Source

pub fn set_is_file_template(self, is_template: bool) -> Self

Sets whether this snippet is a file template

Source

pub fn set_priority(self, priority: u32) -> Self

Sets the priority of the snippet

Trait Implementations§

Source§

impl Clone for SnippetBuilder

Source§

fn clone(&self) -> SnippetBuilder

Returns a copy 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 SnippetBuilder

Source§

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

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

impl Default for SnippetBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<SnippetBuilder> for Snippet

Source§

fn from(value: SnippetBuilder) -> Self

Converts to this type from the input type.

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.