-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.rs
60 lines (51 loc) · 2.33 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//! Semi-dynamic backend system
//!
//! A backend is a struct implementing [`Backend`], having a name, offering deserialization
//! (input) abilities through [`Backend::deserialize`] and serialization (output) abilities
//! through [`Backend::serialize`], using [`SnippetFile`] as the linking part between them.
//!
//! In order to create a new backend, create a struct implementing [`Backend`] and add it to the
//! [`all`] method in this module. That's all what's needed, it'll appear on the CLI automatically.
//!
//! If a backend happens to support only deserialization or only serialization (or neither, in
//! which case it's inaccessible though), it's also possible to override [`Backend::name_in`] and
//! [`Backend::name_out`] respectively and make them return [`None`] instead.
mod ols;
mod ultisnips;
use anyhow::Result;
pub use ols::Ols;
pub use ultisnips::UltiSnips;
use crate::SnippetFile;
/// All registered backends.
pub fn all() -> Vec<Box<dyn Backend>> {
vec![Box::new(Ols), Box::new(UltiSnips)]
}
/// Offers communication to and from a file format. See the module-level docs for details.
pub trait Backend: std::fmt::Debug {
/// Tries parsing the given input into _the IR_.
///
/// # Panics
///
/// Panics if the backend doesn't actually support deserializing. Note to the implementor:
/// Don't forget to also implement [`Backend::name_in`] to return [`None`] in that case.
fn deserialize(&self, input: &str) -> Result<SnippetFile>;
/// Tries writing _the IR_ into a string.
///
/// # Panics
///
/// Panics if the backend doesn't actually support serializing. Note to the implementor:
/// Don't forget to also implement [`Backend::name_out`] to return [`None`] in that case.
fn serialize(&self, snippets: &SnippetFile) -> Result<String>;
/// The name of this backend, ideally an all-lowercase, short identifier.
fn name(&self) -> &'static str;
/// Returns the input argument name for this backend. Returns [`None`] if this backend
/// doesn't support deserialization.
fn name_in(&self) -> Option<String> {
Some(format!("{}-in", self.name()))
}
/// Returns the input argument name for this backend. Returns [`None`] if this backend
/// doesn't support serialization.
fn name_out(&self) -> Option<String> {
Some(format!("{}-out", self.name()))
}
}