Anki/rslib/src/backend/ops.rs
Damien Elmes fa625d7ad8
Minor Rust cleanups (#2272)
* Run cargo +nightly fmt

* Latest prost-build includes clippy workaround

* Tweak Rust protobuf imports

- Avoid use of stringify!(), as JetBrains editors get confused by it
- Stop merging all protobuf symbols into a single namespace

* Remove some unnecessary qualifications

Found via IntelliJ lint

* Migrate some asserts to assert_eq/ne

* Remove mention of node_modules exclusion

This no longer seems to be necessary after migrating away from Bazel,
and excluding it means TS/Svelte files can't be edited properly.
2022-12-16 21:40:27 +10:00

74 lines
2.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::{
ops::OpChanges,
pb,
prelude::*,
undo::{UndoOutput, UndoStatus},
};
impl From<OpChanges> for pb::collection::OpChanges {
fn from(c: OpChanges) -> Self {
pb::collection::OpChanges {
card: c.changes.card,
note: c.changes.note,
deck: c.changes.deck,
tag: c.changes.tag,
notetype: c.changes.notetype,
config: c.changes.config,
deck_config: c.changes.deck_config,
mtime: c.changes.mtime,
browser_table: c.requires_browser_table_redraw(),
browser_sidebar: c.requires_browser_sidebar_redraw(),
note_text: c.requires_note_text_redraw(),
study_queues: c.requires_study_queue_rebuild(),
}
}
}
impl UndoStatus {
pub(crate) fn into_protobuf(self, tr: &I18n) -> pb::collection::UndoStatus {
pb::collection::UndoStatus {
undo: self.undo.map(|op| op.describe(tr)).unwrap_or_default(),
redo: self.redo.map(|op| op.describe(tr)).unwrap_or_default(),
last_step: self.last_step as u32,
}
}
}
impl From<OpOutput<()>> for pb::collection::OpChanges {
fn from(o: OpOutput<()>) -> Self {
o.changes.into()
}
}
impl From<OpOutput<usize>> for pb::collection::OpChangesWithCount {
fn from(out: OpOutput<usize>) -> Self {
pb::collection::OpChangesWithCount {
count: out.output as u32,
changes: Some(out.changes.into()),
}
}
}
impl From<OpOutput<i64>> for pb::collection::OpChangesWithId {
fn from(out: OpOutput<i64>) -> Self {
pb::collection::OpChangesWithId {
id: out.output,
changes: Some(out.changes.into()),
}
}
}
impl OpOutput<UndoOutput> {
pub(crate) fn into_protobuf(self, tr: &I18n) -> pb::collection::OpChangesAfterUndo {
pb::collection::OpChangesAfterUndo {
changes: Some(self.changes.into()),
operation: self.output.undone_op.describe(tr),
reverted_to_timestamp: self.output.reverted_to.0,
new_status: Some(self.output.new_undo_status.into_protobuf(tr)),
counter: self.output.counter as u32,
}
}
}