diff options
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> | 2018-10-17 04:37:05 +0200 |
---|---|---|
committer | Maxime “pep” Buquet <pep@bouah.net> | 2022-08-21 17:19:58 +0200 |
commit | eef1d2041d9c6d213cc41640d57805cca6f1d3ef (patch) | |
tree | 0ae7cebfa8b717296da2dc5870105b9e1539a100 /src/lib.rs | |
parent | 6378f6ee6b43ef801725f3dda428cc10a68f26d4 (diff) | |
download | poezio-eef1d2041d9c6d213cc41640d57805cca6f1d3ef.tar.gz poezio-eef1d2041d9c6d213cc41640d57805cca6f1d3ef.tar.bz2 poezio-eef1d2041d9c6d213cc41640d57805cca6f1d3ef.tar.xz poezio-eef1d2041d9c6d213cc41640d57805cca6f1d3ef.zip |
Convert theming to Rust.
Diffstat (limited to 'src/lib.rs')
-rw-r--r-- | src/lib.rs | 49 |
1 files changed, 49 insertions, 0 deletions
@@ -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::<LogParseError>().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<PyObject> { + 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)) +} |