mirror of
https://github.com/ankitects/anki.git
synced 2026-01-16 23:39:01 -05:00
So, this is fun. Apparently "DeckId" is considered preferable to the "DeckID" were were using until now, and the latest clippy will start warning about it. We could of course disable the warning, but probably better to bite the bullet and switch to the naming that's generally considered best.
71 lines
3 KiB
Rust
71 lines
3 KiB
Rust
// Copyright: Ankitects Pty Ltd and contributors
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
use crate::{
|
|
backend_proto as pb,
|
|
err::{AnkiError, NetworkErrorKind, SyncErrorKind},
|
|
prelude::*,
|
|
};
|
|
|
|
/// Convert an Anki error to a protobuf error.
|
|
pub(super) fn anki_error_to_proto_error(err: AnkiError, tr: &I18n) -> pb::BackendError {
|
|
use pb::backend_error::Value as V;
|
|
let localized = err.localized_description(tr);
|
|
let value = match err {
|
|
AnkiError::InvalidInput { .. } => V::InvalidInput(pb::Empty {}),
|
|
AnkiError::TemplateError { .. } => V::TemplateParse(pb::Empty {}),
|
|
AnkiError::IoError { .. } => V::IoError(pb::Empty {}),
|
|
AnkiError::DbError { .. } => V::DbError(pb::Empty {}),
|
|
AnkiError::NetworkError { kind, .. } => {
|
|
V::NetworkError(pb::NetworkError { kind: kind.into() })
|
|
}
|
|
AnkiError::SyncError { kind, .. } => V::SyncError(pb::SyncError { kind: kind.into() }),
|
|
AnkiError::Interrupted => V::Interrupted(pb::Empty {}),
|
|
AnkiError::CollectionNotOpen => V::InvalidInput(pb::Empty {}),
|
|
AnkiError::CollectionAlreadyOpen => V::InvalidInput(pb::Empty {}),
|
|
AnkiError::JsonError { info } => V::JsonError(info),
|
|
AnkiError::ProtoError { info } => V::ProtoError(info),
|
|
AnkiError::NotFound => V::NotFoundError(pb::Empty {}),
|
|
AnkiError::Existing => V::Exists(pb::Empty {}),
|
|
AnkiError::DeckIsFiltered => V::DeckIsFiltered(pb::Empty {}),
|
|
AnkiError::SearchError(_) => V::SearchError(pb::Empty {}),
|
|
AnkiError::TemplateSaveError { .. } => V::TemplateParse(pb::Empty {}),
|
|
AnkiError::ParseNumError => V::InvalidInput(pb::Empty {}),
|
|
AnkiError::FilteredDeckEmpty => V::FilteredDeckEmpty(pb::Empty {}),
|
|
};
|
|
|
|
pb::BackendError {
|
|
value: Some(value),
|
|
localized,
|
|
}
|
|
}
|
|
|
|
impl std::convert::From<NetworkErrorKind> for i32 {
|
|
fn from(e: NetworkErrorKind) -> Self {
|
|
use pb::network_error::NetworkErrorKind as V;
|
|
(match e {
|
|
NetworkErrorKind::Offline => V::Offline,
|
|
NetworkErrorKind::Timeout => V::Timeout,
|
|
NetworkErrorKind::ProxyAuth => V::ProxyAuth,
|
|
NetworkErrorKind::Other => V::Other,
|
|
}) as i32
|
|
}
|
|
}
|
|
|
|
impl std::convert::From<SyncErrorKind> for i32 {
|
|
fn from(e: SyncErrorKind) -> Self {
|
|
use pb::sync_error::SyncErrorKind as V;
|
|
(match e {
|
|
SyncErrorKind::Conflict => V::Conflict,
|
|
SyncErrorKind::ServerError => V::ServerError,
|
|
SyncErrorKind::ClientTooOld => V::ClientTooOld,
|
|
SyncErrorKind::AuthFailed => V::AuthFailed,
|
|
SyncErrorKind::ServerMessage => V::ServerMessage,
|
|
SyncErrorKind::ResyncRequired => V::ResyncRequired,
|
|
SyncErrorKind::DatabaseCheckRequired => V::DatabaseCheckRequired,
|
|
SyncErrorKind::Other => V::Other,
|
|
SyncErrorKind::ClockIncorrect => V::ClockIncorrect,
|
|
SyncErrorKind::SyncNotStarted => V::SyncNotStarted,
|
|
}) as i32
|
|
}
|
|
}
|