Crates.io | snippets-rs |
lib.rs | snippets-rs |
version | 0.1.0 |
source | src |
created_at | 2022-01-17 10:25:02.421599 |
updated_at | 2022-01-17 10:25:02.421599 |
description | A parser for the snippet file format, a human-readable file format for storing strings |
homepage | https://github.com/Jomy10/snippets-rs |
repository | https://github.com/Jomy10/snippets-rs |
max_upload_size | |
id | 515472 |
size | 25,607 |
The snippets file format is a human-readable format for storing strings, which makes it perfect for developpers.
For more information on the format, see the snippets spec.
Example of a snippet file
-- my snippet --
This snippet contains a string
-- end --
Text here is ignored, so it can be used to write comments
-- my second snippet --
This snippet contains multiple lines --
-- end --
This is a snippet parser written in Rust. It can be used to both read and write snippets. When reading snippets from a file, they are only read into memory as needed.
In this section we will define snippets.snip
as:
-- snippet1 --
Are we human?
Or are we dancer?
-- end --
-- snippet2 --
This is my church.
This is where I heal my hurts.
-- end --
This is a comment
-- snippet3 --
Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
-- end --
let parser = SnippetParser::read("snippet.snip").unwrap();
Now that we have a parser, we have some possibilities to read snippets:
assert_eq!(
Snippet::new("snippet1".to_string(), "Are we human?\nOr are we dancer?".to_string),
parser.next()
);
OR
let snippets = vec![
Snippet::new("snippet1".to_string(), "Are we human?\nOr are we dancer?".to_string),
Snippet::new("snippet2".to_string(), /*..*/),
Snippet::new("snippet3".to_string(), /*...*/),
];
let snippets_from_iterator = parser.into_iter().map(|snip| snip).collect::<Vec<Snippet>>();
assert_eq!(snippets, snippets_from_iterator);
assert_eq!(snippets, parser.get_snippets().unwrap());
assert_eq!(
Snippet::new("snippet1".to_string(), "Are we human?\nOr are we dancer?".to_string),
parser.get_snippet("snippet1")
);
You can add a snippet to a parser using:
parser.add_snippet(Snippet::new("snippet4".to_string(), "No one knows".to_string));
You can also create an empty parser and add snippets to it, or initialize a parser directly with snippets.
let mut parser1 = SnippetParser::new();
parser1.add_snippet(Snippet::new("snippet4".to_string(), "No one knows".to_string));
let parser2 = SnippetParser::from_snippets(vec![Snippet::new("snippet4".to_string(), "No one knows".to_string)]);
assert_eq!(parser1.get_snippets(), parser2.get_snippets());
// Get file contents
let file_contents = parser.to_string();
// Write file
let f = OpenOptions::new()
.append(true)
.open("output_file.snip")
.expect("Unable to open file");
let mut f = BufWriter::new(f);
f.write_all(file_contents.as_bytes()).expect("Unable to write data");
This crate is published to crates.io, so just add the following to your cargo.toml:
snippets-rs = "0.1.0"
There are a lot of optimizations that can be made to this crate, so feel free to open an issue and a pull request. Just make sure the tests are working.
This crate is licensed under the MIT license