urdf_viz/
errors.rs

1/*
2  Copyright 2017 Takashi Ogura
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15*/
16
17use std::io;
18use thiserror::Error;
19
20#[derive(Debug, Error)]
21#[non_exhaustive]
22pub enum Error {
23    #[error("Error: {:?}", .0)]
24    Other(String),
25    #[error("IOError: {:?}", .0)]
26    IoError(#[from] io::Error),
27    #[error("FromUtf8Error: {:?}", .0)]
28    FromUtf8Error(#[from] std::string::FromUtf8Error),
29    #[error("UrdfError: {:?}", .0)]
30    Urdf(#[from] urdf_rs::UrdfError),
31}
32
33pub type Result<T> = ::std::result::Result<T, Error>;
34
35impl<'a> From<&'a str> for Error {
36    fn from(error: &'a str) -> Self {
37        Error::Other(error.to_owned())
38    }
39}
40
41impl From<String> for Error {
42    fn from(error: String) -> Self {
43        Error::Other(error)
44    }
45}
46
47#[cfg(target_family = "wasm")]
48impl From<wasm_bindgen::JsValue> for Error {
49    fn from(error: wasm_bindgen::JsValue) -> Self {
50        Error::Other(format!("{error:?}"))
51    }
52}
53
54#[cfg(target_family = "wasm")]
55impl From<Error> for wasm_bindgen::JsValue {
56    fn from(error: Error) -> Self {
57        Self::from_str(&error.to_string())
58    }
59}