ev3dev_rs/error.rs
1use crate::{
2 attribute::FileMode,
3 parameters::{MotorPort, SensorPort},
4 sensor_driver::SensorType,
5};
6use std::num::ParseFloatError;
7use std::{error::Error, fmt::Display, num::ParseIntError, path::PathBuf};
8
9#[derive(Debug)]
10#[allow(private_interfaces)]
11/// The error type for the ev3dev_rs crate
12pub enum Ev3Error {
13 /// Unable to find a sensor.
14 SensorNotFound {
15 /// The port the sensor was supposed to be on.
16 port: SensorPort,
17 /// The expected sensor type.
18 expected_sensor_type: SensorType,
19 },
20 /// Unable to find a motor.
21 MotorNotFound {
22 /// The port the motor was supposed to be on.
23 port: MotorPort,
24 },
25 /// An internal error
26 FileNotFound {
27 /// The name of the expected file
28 path: PathBuf,
29 },
30 /// An internal error
31 InvalidPath,
32 /// Found the incorrect sensor
33 IncorrectSensorType {
34 /// The type of the expected sensor.
35 expected: SensorType,
36 /// The type of the found sensor
37 found: SensorType,
38 },
39 /// Failed to parse a string into an enum variant.
40 ParseStr {
41 /// The string that was unable to be parsed.
42 input: String,
43 /// The name of the enum
44 to: String,
45 },
46 /// An internal error
47 PermissionDenied {
48 /// The permission that was expected
49 required_permission: FileMode,
50 },
51 /// Failed to read the contents of a sysfs file to a string.
52 InvalidStringBytes,
53 /// Unable to read a motor or sensor attribute.
54 ReadAttributeFailure {
55 /// The name of the file that failed to read
56 filename: PathBuf,
57 /// The raw OS error.
58 os_error: std::io::Error,
59 },
60 /// Failed to write to an attribute of a sensor or motor.
61 WriteAttributeFailure {
62 /// The name of the file that we failed to write to.
63 filename: PathBuf,
64 /// The value that we tried to write.
65 value: String,
66 /// The raw OS error.
67 os_error: std::io::Error,
68 },
69 /// Failed to parse an integer
70 ParseInt {
71 /// The raw error
72 err: ParseIntError,
73 },
74 /// Failed to parse a floating point number.
75 ParseFloat {
76 /// The raw error
77 err: ParseFloatError,
78 },
79
80 /// Failed to read a string into an enum variant.
81 InvalidValue {
82 /// The function that tried to read the value.
83 func: String,
84 /// The value.
85 value: String,
86 },
87 /// No sensor was provided to something that needed it.
88 ///
89 /// This was most likely caused by a `DriveBase` not being provided a `GyroSensor`.
90 NoSensorProvided,
91}
92
93impl Display for Ev3Error {
94 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95 write!(f, "{:?}", self)
96 }
97}
98
99impl Error for Ev3Error {}
100
101impl From<ParseIntError> for Ev3Error {
102 fn from(err: ParseIntError) -> Self {
103 Ev3Error::ParseInt { err }
104 }
105}
106
107impl From<ParseFloatError> for Ev3Error {
108 fn from(err: ParseFloatError) -> Self {
109 Ev3Error::ParseFloat { err }
110 }
111}
112
113/// The result type for the ev3dev_rs crate.
114pub type Ev3Result<T> = Result<T, Ev3Error>;