-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlib.rs
77 lines (69 loc) · 2.22 KB
/
lib.rs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::sync::OnceLock;
use pyo3::prelude::*;
use jiter::{JsonParseError, LosslessFloat, PartialMode, PythonParse, StringCacheMode};
#[allow(clippy::fn_params_excessive_bools)]
#[allow(clippy::too_many_arguments)]
#[pyfunction(
signature = (
json_data,
/,
*,
allow_inf_nan=true,
cache_mode=StringCacheMode::All,
partial_mode=PartialMode::Off,
catch_duplicate_keys=false,
lossless_floats=false,
error_in_path=false,
)
)]
pub fn from_json<'py>(
py: Python<'py>,
json_data: &[u8],
allow_inf_nan: bool,
cache_mode: StringCacheMode,
partial_mode: PartialMode,
catch_duplicate_keys: bool,
lossless_floats: bool,
error_in_path: bool,
) -> PyResult<Bound<'py, PyAny>> {
let parse_builder = PythonParse {
allow_inf_nan,
cache_mode,
partial_mode,
catch_duplicate_keys,
lossless_floats,
error_in_path,
};
parse_builder.python_parse_exc(py, json_data)
}
pub fn get_jiter_version() -> &'static str {
static JITER_VERSION: OnceLock<String> = OnceLock::new();
JITER_VERSION.get_or_init(|| {
let version = env!("CARGO_PKG_VERSION");
// cargo uses "1.0-alpha1" etc. while python uses "1.0.0a1", this is not full compatibility,
// but it's good enough for now
// see https://docs.rs/semver/1.0.9/semver/struct.Version.html#method.parse for rust spec
// see https://peps.python.org/pep-0440/ for python spec
// it seems the dot after "alpha/beta" e.g. "-alpha.1" is not necessary, hence why this works
version.replace("-alpha", "a").replace("-beta", "b")
})
}
#[pyfunction]
pub fn cache_clear(py: Python<'_>) {
jiter::cache_clear(py);
}
#[pyfunction]
pub fn cache_usage(py: Python<'_>) -> usize {
jiter::cache_usage(py)
}
#[pymodule]
#[pyo3(name = "jiter")]
fn jiter_python(_py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add("__version__", get_jiter_version())?;
m.add_function(wrap_pyfunction!(from_json, m)?)?;
m.add_function(wrap_pyfunction!(cache_clear, m)?)?;
m.add_function(wrap_pyfunction!(cache_usage, m)?)?;
m.add_class::<LosslessFloat>()?;
m.add_class::<JsonParseError>()?;
Ok(())
}