summaryrefslogtreecommitdiff
path: root/src/lib.rs
blob: 78ced5a8bfc46db70ae2fb24b85a6557496d244a (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
extern crate ncurses;
extern crate nom;
extern crate pyo3;
#[macro_use]
extern crate lazy_static;
extern crate enum_set;

pub mod theming;

use self::theming::{curses_attr, parse_attrs};

use pyo3::{
    conversion::{IntoPy, ToPyObject},
    create_exception,
    marker::Python,
    prelude::{pyfunction, pymodule, wrap_pyfunction, PyErr, PyModule, PyObject, PyResult},
};

create_exception!(libpoezio, LogParseError, pyo3::exceptions::PyException);

#[pymodule]
fn libpoezio(py: Python, m: &PyModule) -> PyResult<()> {
    m.add("LogParseError", py.get_type::<LogParseError>())?;
    m.add_function(wrap_pyfunction!(to_curses_attr, m)?)?;

    Ok(())
}

macro_rules! py_object {
    ($py:ident, $i:expr) => {
        $i.into_py($py).to_object($py)
    };
}

fn nom_to_py_err(py: Python, err: nom::Err<nom::error::Error<&str>>) -> PyErr {
    LogParseError::new_err(py_object!(py, err.to_string()))
}

#[pyfunction]
fn to_curses_attr(py: Python, fg: i16, bg: i16, attrs: &str) -> PyResult<PyObject> {
    let attrs = match parse_attrs(attrs) {
        Ok(attrs) => attrs,
        Err(err) => return Err(nom_to_py_err(py, err)),
    };
    let result = curses_attr(fg, bg, attrs);
    Ok(py_object!(py, result))
}