ogn_parser/
lib.rs

1use ogn_parser::ServerResponse;
2use pyo3::prelude::*;
3use pythonize::pythonize;
4use rayon::prelude::*;
5
6/// Parse an APRS packet from a string to a list of JSON strings: List[str]
7#[pyfunction]
8fn parse_to_json(s: &str) -> PyResult<Vec<String>> {
9    let lines = s.lines().collect::<Vec<_>>();
10    let json_strings = lines
11        .par_iter()
12        .map(|&aprs_string| {
13            serde_json::to_string(&aprs_string.parse::<ServerResponse>().unwrap()).unwrap()
14        })
15        .collect();
16    Ok(json_strings)
17}
18
19/// Parse an APRS packet from a string to a Python object: List[Dict[str, Any]]
20#[pyfunction]
21fn parse(py: Python, s: &str) -> PyResult<Py<PyAny>> {
22    let lines = s.lines().collect::<Vec<_>>();
23    let result = if lines.len() == 1 {
24        pythonize(py, &lines[0].parse::<ServerResponse>().unwrap())?.into()
25    } else {
26        let packets = lines
27            .par_iter()
28            .map(|&aprs_string| aprs_string.parse::<ServerResponse>().unwrap())
29            .collect::<Vec<_>>();
30        pythonize(py, &packets)?.into()
31    };
32    Ok(result)
33}
34
35/// A Python module implemented in Rust.
36#[pymodule(name = "ogn_parser")]
37fn python_module(m: &Bound<'_, PyModule>) -> PyResult<()> {
38    m.add_function(wrap_pyfunction!(parse_to_json, m)?)?;
39    m.add_function(wrap_pyfunction!(parse, m)?)?;
40    Ok(())
41}