From eef1d2041d9c6d213cc41640d57805cca6f1d3ef Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 17 Oct 2018 04:37:05 +0200 Subject: Convert theming to Rust. --- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index d261b89e..fae54570 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,55 @@ #[macro_use] extern crate cpython; +#[macro_use] +extern crate nom; +extern crate ncurses; +#[macro_use] +extern crate lazy_static; +extern crate enum_set; + +pub mod theming; + +use self::theming::{curses_attr, parse_attrs}; +use cpython::{PyErr, PyObject, PyResult, Python, PythonObject, ToPyObject}; py_module_initializer!(libpoezio, initlibpoezio, PyInit_libpoezio, |py, m| { + m.add( + py, + "to_curses_attr", + py_fn!(py, to_curses_attr(fg: i16, bg: i16, attrs: &str)), + )?; Ok(()) }); + +py_exception!(libpoezio, LogParseError); + +macro_rules! py_int { + ($py:ident, $i:expr) => { + $i.to_py_object($py).into_object() + }; +} + +fn nom_to_py_err(py: Python, err: nom::Err<&str>) -> PyErr { + PyErr { + ptype: py.get_type::().into_object(), + pvalue: Some( + LogParseError( + err.into_error_kind() + .description() + .to_py_object(py) + .into_object(), + ) + .into_object(), + ), + ptraceback: None, + } +} + +fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult { + let attrs = match parse_attrs(attrs) { + Ok(attrs) => attrs.1, + Err(err) => return Err(nom_to_py_err(py, err)), + }; + let result = curses_attr(fg, bg, attrs); + Ok(py_int!(py, result)) +} -- cgit v1.2.3