1
+ use fmt:: Display ;
2
+ use std:: fmt;
1
3
use pyo3:: prelude:: * ;
2
4
use pyo3:: wrap_pyfunction;
3
5
use std:: path:: PathBuf ;
4
6
use :: shr_parser:: { SHRParser , SHRParsingType } ;
5
7
6
8
/// A wrapper around the SHRParser for Python
7
- #[ pyclass]
9
+ #[ pyclass( name= "SHRParser" , subclass ) ]
8
10
struct PySHRParser {
9
11
parser : SHRParser ,
12
+ parsing_type : PySHRParsingType ,
13
+ }
14
+
15
+ #[ pyclass( name="SHRSweep" ) ]
16
+ struct PySHRSweep {
17
+ sweep_number : i32 ,
18
+ timestamp : u64 ,
19
+ frequency : f64 ,
20
+ amplitude : f64 ,
21
+ }
22
+
23
+ #[ pyclass( name="SHRParsingType" , eq, eq_int) ]
24
+ #[ derive( Clone , Copy , Debug , PartialEq ) ]
25
+ enum PySHRParsingType {
26
+ PEAK = 0 ,
27
+ MEAN = 1 ,
28
+ LOW = 2 ,
29
+ }
30
+
31
+ impl TryFrom < i32 > for PySHRParsingType {
32
+ type Error = & ' static str ;
33
+
34
+ fn try_from ( value : i32 ) -> Result < Self , Self :: Error > {
35
+ match value {
36
+ 0 => Ok ( PySHRParsingType :: PEAK ) ,
37
+ 1 => Ok ( PySHRParsingType :: MEAN ) ,
38
+ 2 => Ok ( PySHRParsingType :: LOW ) ,
39
+ _ => Err ( "Invalid value for SHRParsingType" ) ,
40
+ }
41
+ }
42
+ }
43
+
44
+ impl TryFrom < PySHRParsingType > for SHRParsingType {
45
+ type Error = & ' static str ;
46
+
47
+ fn try_from ( value : PySHRParsingType ) -> Result < Self , Self :: Error > {
48
+ match value {
49
+ PySHRParsingType :: PEAK => Ok ( SHRParsingType :: Peak ) ,
50
+ PySHRParsingType :: MEAN => Ok ( SHRParsingType :: Mean ) ,
51
+ PySHRParsingType :: LOW => Ok ( SHRParsingType :: Low ) ,
52
+ }
53
+ }
54
+ }
55
+
56
+ impl Display for PySHRParsingType {
57
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
58
+ match self {
59
+ PySHRParsingType :: PEAK => write ! ( f, "SHRParsingType.PEAK" ) ,
60
+ PySHRParsingType :: MEAN => write ! ( f, "SHRParsingType.MEAN" ) ,
61
+ PySHRParsingType :: LOW => write ! ( f, "SHRParsingType.LOW" ) ,
62
+ }
63
+ }
64
+ }
65
+
66
+ #[ pymethods]
67
+ impl PySHRSweep {
68
+ fn __repr__ ( & self ) -> String {
69
+ format ! (
70
+ "SHRSweep(sweep_number={}, timestamp={}, frequency={}, amplitude={})" ,
71
+ self . sweep_number, self . timestamp, self . frequency, self . amplitude
72
+ )
73
+ }
74
+
75
+ #[ getter]
76
+ fn sweep_number ( & self ) -> i32 {
77
+ self . sweep_number
78
+ }
79
+
80
+ #[ getter]
81
+ fn timestamp ( & self ) -> u64 {
82
+ self . timestamp
83
+ }
84
+
85
+ #[ getter]
86
+ fn frequency ( & self ) -> f64 {
87
+ self . frequency
88
+ }
89
+
90
+ #[ getter]
91
+ fn amplitude ( & self ) -> f64 {
92
+ self . amplitude
93
+ }
10
94
}
11
95
12
96
#[ pymethods]
13
97
impl PySHRParser {
14
98
#[ new]
15
- fn new ( file_path : String , parsing_type : i32 ) -> PyResult < Self > {
16
- let parsing_type = SHRParsingType :: try_from ( parsing_type) . map_err ( |e| {
17
- pyo3:: exceptions:: PyValueError :: new_err ( format ! ( "Invalid parsing type: {}" , e) )
18
- } ) ?;
19
- let parser = SHRParser :: new ( PathBuf :: from ( file_path) , parsing_type) . map_err ( |e| {
99
+ fn new ( file_path : & str , parsing_type : PySHRParsingType ) -> PyResult < Self > {
100
+ let parser = SHRParser :: new ( PathBuf :: from ( file_path) , SHRParsingType :: try_from ( parsing_type) . unwrap ( ) ) . map_err ( |e| {
20
101
pyo3:: exceptions:: PyIOError :: new_err ( format ! ( "Failed to parse SHR file: {:?}" , e) )
21
102
} ) ?;
22
- Ok ( PySHRParser { parser } )
103
+ Ok ( PySHRParser { parser, parsing_type } )
104
+ }
105
+
106
+ fn __repr__ ( & self ) -> String {
107
+ format ! ( "SHRParser(file_path='{}', parsing_type={})" , self . parser. get_file_path( ) . to_string_lossy( ) , self . parsing_type)
23
108
}
24
109
25
110
fn to_csv ( & self , path : String ) -> PyResult < ( ) > {
@@ -28,11 +113,16 @@ impl PySHRParser {
28
113
} )
29
114
}
30
115
31
- fn get_sweeps ( & self ) -> PyResult < Vec < ( i32 , u64 , f64 , f64 ) > > {
116
+ fn get_sweeps ( & self ) -> PyResult < Vec < PySHRSweep > > {
32
117
let sweeps = self . parser . get_sweeps ( ) ;
33
118
Ok ( sweeps
34
119
. into_iter ( )
35
- . map ( |sweep| ( sweep. sweep_number , sweep. timestamp , sweep. frequency , sweep. amplitude ) )
120
+ . map ( |sweep| PySHRSweep {
121
+ sweep_number : sweep. sweep_number ,
122
+ timestamp : sweep. timestamp ,
123
+ frequency : sweep. frequency ,
124
+ amplitude : sweep. amplitude ,
125
+ } )
36
126
. collect ( ) )
37
127
}
38
128
@@ -46,16 +136,19 @@ impl PySHRParser {
46
136
}
47
137
}
48
138
139
+
49
140
/// Create a new SHRParser instance.
50
141
#[ pyfunction]
51
- fn create_parser ( file_path : String , parsing_type : i32 ) -> PyResult < PySHRParser > {
52
- PySHRParser :: new ( file_path, parsing_type)
142
+ fn create_parser ( file_path : & str , parsing_type : i32 ) -> PyResult < PySHRParser > {
143
+ PySHRParser :: new ( file_path, parsing_type. try_into ( ) . unwrap ( ) )
53
144
}
54
145
55
146
/// A Python module implemented in Rust.
56
147
#[ pymodule]
57
148
fn shr_parser ( module : & Bound < ' _ , PyModule > ) -> PyResult < ( ) > {
58
149
module. add_class :: < PySHRParser > ( ) ?;
150
+ module. add_class :: < PySHRSweep > ( ) ?;
151
+ module. add_class :: < PySHRParsingType > ( ) ?;
59
152
module. add_function ( wrap_pyfunction ! ( create_parser, module) ?) ?;
60
153
Ok ( ( ) )
61
154
}
0 commit comments