summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: fae545706fa72f0e758e4cd6a8a77bc8c74d9de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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))
}