1
1
//! Defines error types.
2
- use crate :: DataType ;
3
- use pyo3:: { exceptions as exc, PyErr , PyErrArguments , PyObject , Python , ToPyObject } ;
4
- use std:: fmt;
5
-
6
- /// Represents a dimension and dtype of numpy array.
7
- ///
8
- /// Only for error formatting.
9
- #[ derive( Debug ) ]
10
- pub ( crate ) struct ArrayDim {
11
- dim : Option < usize > ,
12
- dtype : Option < DataType > ,
13
- }
14
2
15
- impl fmt:: Display for ArrayDim {
16
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
17
- let ArrayDim { dim, dtype } = self ;
18
- match ( dim, dtype) {
19
- ( Some ( dim) , Some ( dtype) ) => write ! ( f, "dim={:?}, dtype={:?}" , dim, dtype) ,
20
- ( None , Some ( dtype) ) => write ! ( f, "dim=_, dtype={:?}" , dtype) ,
21
- ( Some ( dim) , None ) => write ! ( f, "dim={:?}, dtype=Unknown" , dim) ,
22
- ( None , None ) => write ! ( f, "dim=_, dtype=Unknown" ) ,
23
- }
24
- }
25
- }
26
-
27
- /// Represents that shapes of the given arrays don't match.
28
- #[ derive( Debug ) ]
29
- pub struct ShapeError {
30
- from : ArrayDim ,
31
- to : ArrayDim ,
32
- }
3
+ use std:: fmt;
33
4
34
- impl ShapeError {
35
- pub ( crate ) fn new (
36
- from_dtype : & crate :: PyArrayDescr ,
37
- from_dim : usize ,
38
- to_type : DataType ,
39
- to_dim : Option < usize > ,
40
- ) -> Self {
41
- ShapeError {
42
- from : ArrayDim {
43
- dim : Some ( from_dim) ,
44
- dtype : from_dtype. get_datatype ( ) ,
45
- } ,
46
- to : ArrayDim {
47
- dim : to_dim,
48
- dtype : Some ( to_type) ,
49
- } ,
50
- }
51
- }
52
- }
5
+ use pyo3:: { exceptions as exc, PyErr , PyErrArguments , PyObject , Python , ToPyObject } ;
53
6
54
- impl fmt:: Display for ShapeError {
55
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
56
- let ShapeError { from, to } = self ;
57
- write ! ( f, "Shape Mismatch:\n from=({}), to=({})" , from, to)
58
- }
59
- }
7
+ use crate :: dtype:: PyArrayDescr ;
60
8
61
9
macro_rules! impl_pyerr {
62
10
( $err_type: ty) => {
@@ -76,7 +24,57 @@ macro_rules! impl_pyerr {
76
24
} ;
77
25
}
78
26
79
- impl_pyerr ! ( ShapeError ) ;
27
+ /// Represents that dimensionalities of the given arrays don't match.
28
+ #[ derive( Debug ) ]
29
+ pub struct DimensionalityError {
30
+ from : usize ,
31
+ to : usize ,
32
+ }
33
+
34
+ impl DimensionalityError {
35
+ pub ( crate ) fn new ( from : usize , to : usize ) -> Self {
36
+ Self { from, to }
37
+ }
38
+ }
39
+
40
+ impl fmt:: Display for DimensionalityError {
41
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
42
+ let Self { from, to } = self ;
43
+ write ! ( f, "dimensionality mismatch:\n from={}, to={}" , from, to)
44
+ }
45
+ }
46
+
47
+ impl_pyerr ! ( DimensionalityError ) ;
48
+
49
+ /// Represents that types of the given arrays don't match.
50
+ #[ derive( Debug ) ]
51
+ pub struct TypeError {
52
+ from : String ,
53
+ to : String ,
54
+ }
55
+
56
+ impl TypeError {
57
+ pub ( crate ) fn new ( from : & PyArrayDescr , to : & PyArrayDescr ) -> Self {
58
+ let dtype_to_str = |dtype : & PyArrayDescr | {
59
+ dtype
60
+ . str ( )
61
+ . map_or_else ( |_| "(unknown)" . into ( ) , |s| s. to_string_lossy ( ) . into_owned ( ) )
62
+ } ;
63
+ Self {
64
+ from : dtype_to_str ( from) ,
65
+ to : dtype_to_str ( to) ,
66
+ }
67
+ }
68
+ }
69
+
70
+ impl fmt:: Display for TypeError {
71
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
72
+ let Self { from, to } = self ;
73
+ write ! ( f, "type mismatch:\n from={}, to={}" , from, to)
74
+ }
75
+ }
76
+
77
+ impl_pyerr ! ( TypeError ) ;
80
78
81
79
/// Represents that given vec cannot be treated as array.
82
80
#[ derive( Debug ) ]
0 commit comments