@@ -6,37 +6,104 @@ use std::sync::{Arc, Mutex};
66use streamson_lib:: { error, handler, matcher, Collector } ;
77
88create_exception ! ( streamson, StreamsonError , exceptions:: ValueError ) ;
9+ create_exception ! ( streamson, MatcherUsed , exceptions:: RuntimeError ) ;
910
1011impl From < error:: General > for StreamsonError {
1112 fn from ( _gerror : error:: General ) -> Self {
1213 Self
1314 }
1415}
1516
17+ /// Python wrapper around matchers
18+ #[ pyclass]
19+ #[ derive( Debug ) ]
20+ pub struct RustMatcher {
21+ inner : Option < matcher:: Combinator > ,
22+ }
23+
24+ #[ pymethods]
25+ impl RustMatcher {
26+ /// Create a new instance of simple matcher
27+ ///
28+ /// # Arguments
29+ /// * `path` - path to match
30+ /// * `max_depth` - max depth
31+ #[ staticmethod]
32+ pub fn simple ( path : String ) -> PyResult < Self > {
33+ Ok ( Self {
34+ inner : Some ( matcher:: Combinator :: new ( matcher:: Simple :: new ( path) ) ) ,
35+ } )
36+ }
37+
38+ /// Create a new instance of depth matcher
39+ ///
40+ /// # Arguments
41+ /// * `min_depth` - min depth
42+ /// * `max_depth` - max depth (Optional)
43+ #[ staticmethod]
44+ pub fn depth ( min_depth : usize , max_depth : Option < usize > ) -> PyResult < Self > {
45+ Ok ( Self {
46+ inner : Some ( matcher:: Combinator :: new ( matcher:: Depth :: new (
47+ min_depth, max_depth,
48+ ) ) ) ,
49+ } )
50+ }
51+
52+ pub fn inv ( & mut self ) -> PyResult < Self > {
53+ if let Some ( inner) = self . inner . take ( ) {
54+ Ok ( Self {
55+ inner : Some ( !inner) ,
56+ } )
57+ } else {
58+ Err ( MatcherUsed . into ( ) )
59+ }
60+ }
61+
62+ pub fn any ( & mut self , right : & mut RustMatcher ) -> PyResult < Self > {
63+ if let ( Some ( left) , Some ( right) ) = ( self . inner . take ( ) , right. inner . take ( ) ) {
64+ Ok ( Self {
65+ inner : Some ( left | right) ,
66+ } )
67+ } else {
68+ Err ( MatcherUsed . into ( ) )
69+ }
70+ }
71+
72+ pub fn all ( & mut self , right : & mut RustMatcher ) -> PyResult < Self > {
73+ if let ( Some ( left) , Some ( right) ) = ( self . inner . take ( ) , right. inner . take ( ) ) {
74+ Ok ( Self {
75+ inner : Some ( left & right) ,
76+ } )
77+ } else {
78+ Err ( MatcherUsed . into ( ) )
79+ }
80+ }
81+
82+ #[ getter]
83+ pub fn _used ( & self ) -> bool {
84+ self . inner . is_none ( )
85+ }
86+ }
87+
1688/// Low level Python wrapper for Simple matcher and Buffer handler
1789#[ pyclass]
18- pub struct SimpleStreamson {
90+ pub struct Streamson {
1991 collector : Collector ,
2092 handler : Arc < Mutex < handler:: Buffer > > ,
2193}
2294
2395#[ pymethods]
24- impl SimpleStreamson {
25- /// Create a new instance of SimpleStreamson
96+ impl Streamson {
97+ /// Create a new instance of Streamson
2698 ///
2799 /// # Arguments
28100 /// * `matches` - a list of valid simple matches (e.g. `{"users"}`, `[]{"name"}`, `[0]{}`)
29101 #[ new]
30- pub fn new ( matches : Vec < String > ) -> Self {
102+ pub fn new ( matcher : & mut RustMatcher ) -> PyResult < Self > {
31103 let handler = Arc :: new ( Mutex :: new ( handler:: Buffer :: new ( ) ) ) ;
32- let mut collector = Collector :: new ( ) ;
33- for path_match in matches {
34- collector = collector. add_matcher (
35- Box :: new ( matcher:: Simple :: new ( path_match) ) ,
36- & [ handler. clone ( ) ] ,
37- ) ;
38- }
39- Self { collector, handler }
104+ let matcher = matcher. inner . take ( ) . ok_or ( MatcherUsed ) ?;
105+ let collector = Collector :: new ( ) . add_matcher ( Box :: new ( matcher) , & [ handler. clone ( ) ] ) ;
106+ Ok ( Self { collector, handler } )
40107 }
41108
42109 /// Feeds Streamson processor with data
@@ -66,7 +133,8 @@ impl SimpleStreamson {
66133/// This module is a python module implemented in Rust.
67134#[ pymodule]
68135fn streamson ( _py : Python , m : & PyModule ) -> PyResult < ( ) > {
69- m. add_class :: < SimpleStreamson > ( ) ?;
136+ m. add_class :: < Streamson > ( ) ?;
137+ m. add_class :: < RustMatcher > ( ) ?;
70138
71139 Ok ( ( ) )
72140}
0 commit comments