Struct SnippetsFile

Source
pub struct SnippetsFile {
    pub snippets: HashMap<String, Snippet>,
}
Expand description

§Snippets File Manager

📁 A file controller for managing VS Code snippets collections and their file operations.

§Overview

SnippetsFile provides a convenient way to:

  • 📦 Create and manage collections of code snippets
  • 🔄 Serialize snippets to VS Code compatible format
  • 💾 Save snippets to the filesystem

§Usage

§✨ Creating a new snippets file:
let file = SnippetsFile::new(vec![
    Snippet::new("print", vec!["println!(\"$0\");"]),
    Snippet::new("debug", vec!["dbg!($0);"])
]);
§➕ Adding snippets dynamically:
let mut file = SnippetsFile::new(vec![]);
 
// Add single snippet
file.add_snippet(Snippet::new("log", vec!["log::info!(\"$0\")"]));
 
// Add multiple snippets
file.add_snippets(vec![
    Snippet::new("error", vec!["log::error!(\"$0\");"]),
    Snippet::new("warn", vec!["log::warn!(\"$0\");"])
]);
§💾 Saving to file:
let file = SnippetsFile::new(vec![
    Snippet::new("test", vec![
        "#[test]",
        "fn test_name() {",
        "    ",
        "}"
    ])
]);
 
// Saves to VS Code snippets directory
file.write_to("./snippets/rust.code-snippets")?;

§📋 File Format

The snippets are saved in VS Code compatible JSON format:

{
    "print": {
        "prefix": "print",
        "body": [
            "println!(\"Hello\");"
        ],
        "description": "Basic print statement"
    }
}

§⚠️ Error Handling

The structure uses custom Result type for error handling:

  • 🔄 JSON serialization errors
  • 📂 File system operation errors
  • 📁 Directory creation errors

§User Snippets

📝 You can write this snippets to your VS Code custom user snippets folder Locales:

  • 🗂️ Windows: %APPDATA%/Code/User/snippets
  • 🍎 MacOS: ~/Library/Application Support/Code/User/snippets
  • 🐧 Linux: ~/.config/Code/User/snippets

💡 Or simply access it via VS Code:

  1. Press Ctrl/Cmd + Shift + P
  2. Type “Snippets: Configure User Snippets”
  3. Select the language or create a new snippet file

Fields§

§snippets: HashMap<String, Snippet>

Implementations§

Source§

impl SnippetsFile

Source

pub fn new<Sn: Into<Snippet>>(snippets: impl IntoIterator<Item = Sn>) -> Self

Creates a new snippets file controller

Source

pub fn add_snippet<S: Into<Snippet>>(&mut self, snippet: S)

Adds a new snippet to the collection

Source

pub fn add_snippets<S: Into<Snippet>>( &mut self, snippets: impl IntoIterator<Item = S>, )

Adds a new snippets to the collection

Source

pub fn to_json(&self) -> Result<String>

Converts the snippets to json string

Source

pub fn write_to(&self, path: &str) -> Result<()>

Writes the snippets to file path

Trait Implementations§

Source§

impl Clone for SnippetsFile

Source§

fn clone(&self) -> SnippetsFile

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 SnippetsFile

Source§

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

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

impl Serialize for SnippetsFile

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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.