Browse Source

Change erorr handling

tags/v0.2.5-alpha.2
Yuma Hiramatsu 4 years ago
parent
commit
eeb5a8b3c5
3 changed files with 22 additions and 9 deletions
  1. +2
    -2
      rclrust/src/error.rs
  2. +18
    -5
      rclrust/src/parameter/type_.rs
  3. +2
    -2
      rclrust/src/parameter/value.rs

+ 2
- 2
rclrust/src/error.rs View File

@@ -135,10 +135,10 @@ pub enum RclRustError {

#[error("Runtime Error: {0}")]
RuntimeError(&'static str),
#[error("Service is canceled.")]
ServiceIsCanceled,
#[error("Internal message queue if full: {type_} {name}")]
MessageQueueIsFull { type_: &'static str, name: String },
#[error("Out of range: {0}")]
OutOfRange(String),

// Parameter
#[error(r#"Parameter "{name}" cannot be set because it was not declared."#)]


+ 18
- 5
rclrust/src/parameter/type_.rs View File

@@ -1,4 +1,9 @@
use std::convert::TryFrom;

use anyhow::Result;

use super::RclParameterType;
use crate::error::RclRustError;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParameterType {
@@ -14,9 +19,11 @@ pub enum ParameterType {
StringArray,
}

impl From<u8> for ParameterType {
fn from(v: u8) -> Self {
match v {
impl TryFrom<u8> for ParameterType {
type Error = anyhow::Error;

fn try_from(v: u8) -> Result<Self> {
Ok(match v {
RclParameterType::PARAMETER_NOT_SET => Self::NotSet,
RclParameterType::PARAMETER_BOOL => Self::Bool,
RclParameterType::PARAMETER_INTEGER => Self::Integer,
@@ -27,7 +34,13 @@ impl From<u8> for ParameterType {
RclParameterType::PARAMETER_INTEGER_ARRAY => Self::IntegerArray,
RclParameterType::PARAMETER_DOUBLE_ARRAY => Self::DoubleArray,
RclParameterType::PARAMETER_STRING_ARRAY => Self::StringArray,
_ => unreachable!(),
}
_ => {
return Err(RclRustError::OutOfRange(format!(
"{} cannot be converted into RclParameterType",
v
))
.into())
}
})
}
}

+ 2
- 2
rclrust/src/parameter/value.rs View File

@@ -1,4 +1,4 @@
use std::fmt;
use std::{convert::TryInto, fmt};

use super::{ParameterDescriptor, ParameterType, RclParameterType, RclParameterValue};
use crate::internal::ffi::SizedFromCChar;
@@ -124,7 +124,7 @@ impl ParameterValue {
}

pub fn get_type(&self) -> ParameterType {
self.0.type_.into()
self.0.type_.try_into().unwrap()
}

pub fn get_value(&self) -> Option<Variant> {


Loading…
Cancel
Save