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