1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright (c) 2021 Marco Boneberger
// Licensed under the EUPL-1.2-or-later

//! Contains the FrankaErrors type.
use std::fmt;

use crate::robot::error::FrankaError;
use crate::robot::types::RoboErrorHelperStruct;
use num_traits::FromPrimitive;

/// Represent the Errors which were received from the robot.
#[derive(Debug, Clone, Default)]
pub struct FrankaErrors {
    /// contains all errors
    pub franka_errors: Vec<FrankaError>,
}

pub enum FrankaErrorKind {
    Error,
    ReflexReason,
}

impl FrankaErrors {
    /// Creates a new set of errors.
    /// # Arguments
    /// * `errors` - a [RoboErrorHelperStruct](`crate::robot::types::RoboErrorHelperStruct`) containing
    /// all of the errors from [RobotStateIntern](`crate::robot::types::RobotStateIntern`)
    /// * `kind` - specifies if the errors are normal Errors or Reflex reasons
    pub(crate) fn new(errors: RoboErrorHelperStruct, kind: FrankaErrorKind) -> FrankaErrors {
        let error_array = match kind {
            FrankaErrorKind::Error => errors.combine_errors(),
            FrankaErrorKind::ReflexReason => errors.combine_reflex_reasons(),
        };

        let franka_errors = error_array
            .iter()
            .enumerate()
            .filter(|(_i, x)| **x)
            .map(|(i, _x)| FrankaError::from_i32(i as i32).unwrap())
            .collect::<Vec<FrankaError>>();
        FrankaErrors { franka_errors }
    }
    /// check if a specific error is received
    pub fn contains(&self, error: FrankaError) -> bool {
        self.franka_errors.contains(&error)
    }
}

impl fmt::Display for FrankaErrors {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "[")?;
        let mut iter = self.franka_errors.iter().peekable();
        while let Some(error) = iter.next() {
            match iter.peek() {
                Some(_) => {
                    write!(f, "\"{}\", ", error)?;
                }
                None => {
                    return write!(f, "\"{}\"]", error);
                }
            }
        }
        Ok(())
    }
}