aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/lib.rs
blob: 5b7d6a139f8a0e6de1f8fbc1971e1a096b946bbe (plain) (blame)
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
use clap::builder::Str;
use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn process_rule_pack(rule_pack: &str) -> PyResult<String> {
    // 处理规则包的逻辑
    Ok(format!("Processed rule pack: {}", rule_pack))
}

#[pyclass]
struct Integer {
    inner: i32,
}

// A "tuple" struct
#[pyclass]
struct Number(i32);

// PyO3 supports custom discriminants in enums
#[pyclass]
enum HttpResponse {
    Ok = 200,
    NotFound = 404,
    Teapot = 418,
    // ...
}

use pyo3::exceptions::PyValueError;
use pyo3::prelude::*;
use pyo3::types::PyString;

#[pyclass]
#[derive(Debug, Clone)]
enum RuleLoadType {
    DIR,
    NAME,
    FILE,
    CLASS,
}

#[pymethods]
impl RuleLoadType {
    #[staticmethod]
    fn from_str(s: &str) -> PyResult<Self> {
        match s {
            "dir" => Ok(RuleLoadType::DIR),
            "name" => Ok(RuleLoadType::NAME),
            "file" => Ok(RuleLoadType::FILE),
            "class" => Ok(RuleLoadType::CLASS),
            _ => Err(PyValueError::new_err("Invalid value for RuleLoadType")),
        }
    }

    fn __str__(&self) -> PyResult<String> {
        Ok(match self {
            RuleLoadType::DIR => "dir",
            RuleLoadType::NAME => "name",
            RuleLoadType::FILE => "file",
            RuleLoadType::CLASS => "class",
        }
        .to_string())
    }
}

#[pymodule]
#[pyo3(name = "LibCore")]
fn libcore(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_function(wrap_pyfunction!(process_rule_pack, m)?)?;
    m.add_class::<RuleLoadType>()?;
    Ok(())
}