kachaka_api/
conversion.rs

1use crate::kachaka_api;
2use crate::types::{CommandResult, CommandState, KachakaError, Pose, PowerSupplyStatus};
3use image::DynamicImage;
4impl From<kachaka_api::Result> for std::result::Result<(), KachakaError> {
5    fn from(result: kachaka_api::Result) -> Self {
6        if result.success {
7            Ok(())
8        } else {
9            Err(KachakaError {
10                error_code: result.error_code,
11            })
12        }
13    }
14}
15
16impl From<kachaka_api::Pose> for Pose {
17    fn from(pose: kachaka_api::Pose) -> Self {
18        Pose {
19            x: pose.x,
20            y: pose.y,
21            theta: pose.theta,
22        }
23    }
24}
25
26impl From<kachaka_api::PowerSupplyStatus> for PowerSupplyStatus {
27    fn from(status: kachaka_api::PowerSupplyStatus) -> Self {
28        match status {
29            kachaka_api::PowerSupplyStatus::Charging => PowerSupplyStatus::Charging,
30            kachaka_api::PowerSupplyStatus::Discharging => PowerSupplyStatus::Discharging,
31            _ => panic!("Invalid power supply status"),
32        }
33    }
34}
35
36impl From<i32> for PowerSupplyStatus {
37    fn from(status: i32) -> Self {
38        kachaka_api::PowerSupplyStatus::try_from(status)
39            .unwrap()
40            .into()
41    }
42}
43
44impl From<kachaka_api::RosImage> for DynamicImage {
45    fn from(image: kachaka_api::RosImage) -> Self {
46        match image.encoding.as_str() {
47            "rgb8" => {
48                let img_buffer = image::RgbImage::from_raw(image.width, image.height, image.data)
49                    .expect("Failed to create image buffer");
50                DynamicImage::ImageRgb8(img_buffer)
51            }
52            "rgba8" => {
53                let img_buffer = image::RgbaImage::from_raw(image.width, image.height, image.data)
54                    .expect("Failed to create image buffer");
55                DynamicImage::ImageRgba8(img_buffer)
56            }
57            "bgr8" => {
58                let mut rgb_data = image.data.clone();
59                for pixel in rgb_data.chunks_mut(3) {
60                    pixel.swap(0, 2);
61                }
62                let img_buffer = image::RgbImage::from_raw(image.width, image.height, rgb_data)
63                    .expect("Failed to create image buffer");
64                DynamicImage::ImageRgb8(img_buffer)
65            }
66            _ => panic!("Unsupported image encoding: {}", image.encoding),
67        }
68    }
69}
70
71impl From<kachaka_api::RosCompressedImage> for DynamicImage {
72    fn from(image: kachaka_api::RosCompressedImage) -> Self {
73        image::load_from_memory(&image.data).expect("Failed to decode compressed image")
74    }
75}
76
77impl From<kachaka_api::GetCommandStateResponse> for CommandState {
78    fn from(get_command_state_response: kachaka_api::GetCommandStateResponse) -> Self {
79        match kachaka_api::CommandState::try_from(get_command_state_response.state).unwrap() {
80            kachaka_api::CommandState::Unspecified => CommandState::Unspecified,
81            kachaka_api::CommandState::Pending => CommandState::Pending,
82            kachaka_api::CommandState::Running => CommandState::Running(
83                get_command_state_response.command.unwrap(),
84                get_command_state_response.command_id,
85            ),
86        }
87    }
88}
89
90impl From<kachaka_api::GetLastCommandResultResponse> for Option<CommandResult> {
91    fn from(response: kachaka_api::GetLastCommandResultResponse) -> Self {
92        response.result.and_then(|result| {
93            response.command.map(|command| CommandResult {
94                command,
95                result: result.into(),
96            })
97        })
98    }
99}