Anki/rspy/src/lib.rs
Damien Elmes 1acb3b86b9 backend init can now fail, and update media db when file is added
- Adding files inside Anki now updates the media DB, so a full file
scan at sync time is no longer required if no other changes have been
made.
- Use a protobuf message for backend initialization, and return a
string error if initialization fails.
2020-02-17 08:40:16 +10:00

42 lines
1 KiB
Rust

use anki::backend::{init_backend, Backend as RustBackend};
use pyo3::prelude::*;
use pyo3::types::PyBytes;
use pyo3::{exceptions, wrap_pyfunction};
#[pyclass]
struct Backend {
backend: RustBackend,
}
#[pyfunction]
fn buildhash() -> &'static str {
include_str!("../../meta/buildhash").trim()
}
#[pymethods]
impl Backend {
#[new]
fn init(obj: &PyRawObject, init_msg: &PyBytes) -> PyResult<()> {
match init_backend(init_msg.as_bytes()) {
Ok(backend) => {
obj.init({ Backend { backend } });
Ok(())
}
Err(e) => Err(exceptions::Exception::py_err(e)),
}
}
fn command(&mut self, py: Python, input: &PyBytes) -> PyObject {
let out_bytes = self.backend.run_command_bytes(input.as_bytes());
let out_obj = PyBytes::new(py, &out_bytes);
out_obj.into()
}
}
#[pymodule]
fn ankirspy(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<Backend>()?;
m.add_wrapped(wrap_pyfunction!(buildhash)).unwrap();
Ok(())
}