From 4e827c1a26beffd71f80836197e981b82e447c21 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Tue, 16 Jul 2024 07:50:53 +0800 Subject: style: Format python & rust code --- src/lib.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 2 deletions(-) (limited to 'src/lib.rs') diff --git a/src/lib.rs b/src/lib.rs index 5337470..efe51ac 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +use clap::builder::Str; use pyo3::prelude::*; use pyo3::wrap_pyfunction; @@ -17,11 +18,65 @@ fn process_rule_pack(rule_pack: &str) -> PyResult { Ok(format!("Processed rule pack: {}", rule_pack)) } -/// A Python module implemented in Rust. +#[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 { + 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 { + 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)?)?; - perf::Asparagus; + m.add_class::()?; Ok(()) } -- cgit v1.2.3-70-g09d2