mirror of
https://github.com/ankitects/anki.git
synced 2025-11-20 19:47:14 -05:00
50 lines
1.6 KiB
Rust
50 lines
1.6 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
pub use failure::{Error, Fail};
|
|
|
|
pub type Result<T> = std::result::Result<T, AnkiError>;
|
|
|
|
#[derive(Debug, Fail)]
|
|
pub enum AnkiError {
|
|
#[fail(display = "invalid input: {}", info)]
|
|
InvalidInput { info: String },
|
|
|
|
#[fail(display = "invalid card template: {}", info)]
|
|
TemplateError { info: String },
|
|
}
|
|
|
|
// error helpers
|
|
impl AnkiError {
|
|
pub(crate) fn invalid_input<S: Into<String>>(s: S) -> AnkiError {
|
|
AnkiError::InvalidInput { info: s.into() }
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
pub enum TemplateError {
|
|
NoClosingBrackets(String),
|
|
ConditionalNotClosed(String),
|
|
ConditionalNotOpen(String),
|
|
FieldNotFound { filters: String, field: String },
|
|
}
|
|
|
|
impl From<TemplateError> for AnkiError {
|
|
fn from(terr: TemplateError) -> Self {
|
|
AnkiError::TemplateError {
|
|
info: match terr {
|
|
TemplateError::NoClosingBrackets(context) => {
|
|
format!("missing '}}}}' in '{}'", context)
|
|
}
|
|
TemplateError::ConditionalNotClosed(tag) => format!("missing '{{{{/{}}}}}'", tag),
|
|
TemplateError::ConditionalNotOpen(tag) => {
|
|
format!("missing '{{{{#{}}}}}' or '{{{{^{}}}}}'", tag, tag)
|
|
}
|
|
TemplateError::FieldNotFound { field, filters } => format!(
|
|
"found '{{{{{}{}}}}}', but there is no field called '{}'",
|
|
filters, field, field
|
|
),
|
|
},
|
|
}
|
|
}
|
|
}
|