autd3capi_def/
result.rs

1/*
2 * File: result.rs
3 * Project: src
4 * Created Date: 10/11/2023
5 * Author: Shun Suzuki
6 * -----
7 * Last Modified: 08/12/2023
8 * Modified By: Shun Suzuki (suzuki@hapis.k.u-tokyo.ac.jp)
9 * -----
10 * Copyright (c) 2023 Shun Suzuki. All rights reserved.
11 *
12 */
13
14use std::{
15    collections::HashMap,
16    ffi::{c_char, CStr, CString},
17};
18
19use crate::{ConstPtr, DynamicDatagram, L};
20use autd3::prelude::*;
21use autd3_driver::{common::Drive, error::AUTDInternalError};
22
23use crate::{
24    ControllerPtr, DatagramPtr, GainCalcDrivesMapPtr, ModulationPtr, AUTD3_ERR, AUTD3_FALSE,
25    AUTD3_TRUE,
26};
27
28#[no_mangle]
29pub unsafe extern "C" fn AUTDGetErr(src: ConstPtr, dst: *mut c_char) {
30    let src = Box::from_raw(src as *mut String);
31    let c_string: CString = CString::new(src.as_str()).unwrap();
32    let c_str: &CStr = c_string.as_c_str();
33    libc::strcpy(dst, c_str.as_ptr());
34}
35
36#[repr(C)]
37#[derive(Debug, Clone, Copy)]
38pub struct ResultI32 {
39    pub result: i32,
40    pub err_len: u32,
41    pub err: ConstPtr,
42}
43
44impl From<Result<(), AUTDInternalError>> for ResultI32 {
45    fn from(r: Result<(), AUTDInternalError>) -> Self {
46        match r {
47            Ok(_) => Self {
48                result: AUTD3_TRUE,
49                err_len: 0,
50                err: std::ptr::null_mut(),
51            },
52            Err(e) => {
53                let err = e.to_string();
54                Self {
55                    result: AUTD3_ERR,
56                    err_len: err.as_bytes().len() as u32 + 1,
57                    err: Box::into_raw(Box::new(err)) as _,
58                }
59            }
60        }
61    }
62}
63
64impl From<Result<bool, AUTDError>> for ResultI32 {
65    fn from(r: Result<bool, AUTDError>) -> Self {
66        match r {
67            Ok(v) => Self {
68                result: if v { AUTD3_TRUE } else { AUTD3_FALSE },
69                err_len: 0,
70                err: std::ptr::null_mut(),
71            },
72            Err(e) => {
73                let err = e.to_string();
74                Self {
75                    result: AUTD3_ERR,
76                    err_len: err.as_bytes().len() as u32 + 1,
77                    err: Box::into_raw(Box::new(err)) as _,
78                }
79            }
80        }
81    }
82}
83
84impl From<Result<bool, AUTDInternalError>> for ResultI32 {
85    fn from(r: Result<bool, AUTDInternalError>) -> Self {
86        match r {
87            Ok(v) => Self {
88                result: if v { AUTD3_TRUE } else { AUTD3_FALSE },
89                err_len: 0,
90                err: std::ptr::null_mut(),
91            },
92            Err(e) => {
93                let err = e.to_string();
94                Self {
95                    result: AUTD3_ERR,
96                    err_len: err.as_bytes().len() as u32 + 1,
97                    err: Box::into_raw(Box::new(err)) as _,
98                }
99            }
100        }
101    }
102}
103
104impl From<Result<usize, AUTDInternalError>> for ResultI32 {
105    fn from(r: Result<usize, AUTDInternalError>) -> Self {
106        match r {
107            Ok(v) => Self {
108                result: v as i32,
109                err_len: 0,
110                err: std::ptr::null_mut(),
111            },
112            Err(e) => {
113                let err = e.to_string();
114                Self {
115                    result: AUTD3_ERR,
116                    err_len: err.as_bytes().len() as u32 + 1,
117                    err: Box::into_raw(Box::new(err)) as _,
118                }
119            }
120        }
121    }
122}
123
124#[repr(C)]
125#[derive(Debug, Clone, Copy)]
126pub struct ResultController {
127    pub result: ControllerPtr,
128    pub err_len: u32,
129    pub err: ConstPtr,
130}
131
132impl From<Result<Controller<Box<L>>, AUTDError>> for ResultController {
133    fn from(r: Result<Controller<Box<L>>, AUTDError>) -> Self {
134        match r {
135            Ok(v) => Self {
136                result: ControllerPtr(Box::into_raw(Box::new(v)) as _),
137                err_len: 0,
138                err: std::ptr::null_mut(),
139            },
140            Err(e) => {
141                let err = e.to_string();
142                Self {
143                    result: ControllerPtr(std::ptr::null()),
144                    err_len: err.as_bytes().len() as u32 + 1,
145                    err: Box::into_raw(Box::new(err)) as _,
146                }
147            }
148        }
149    }
150}
151
152#[repr(C)]
153#[derive(Debug, Clone, Copy)]
154pub struct ResultGainCalcDrivesMap {
155    pub result: GainCalcDrivesMapPtr,
156    pub err_len: u32,
157    pub err: ConstPtr,
158}
159
160impl From<Result<HashMap<usize, Vec<Drive>>, AUTDInternalError>> for ResultGainCalcDrivesMap {
161    fn from(r: Result<HashMap<usize, Vec<Drive>>, AUTDInternalError>) -> Self {
162        match r {
163            Ok(v) => Self {
164                result: GainCalcDrivesMapPtr(Box::into_raw(Box::new(v)) as _),
165                err_len: 0,
166                err: std::ptr::null_mut(),
167            },
168            Err(e) => {
169                let err = e.to_string();
170                Self {
171                    result: GainCalcDrivesMapPtr(std::ptr::null()),
172                    err_len: err.as_bytes().len() as u32 + 1,
173                    err: Box::into_raw(Box::new(err)) as _,
174                }
175            }
176        }
177    }
178}
179
180#[repr(C)]
181#[derive(Debug, Clone, Copy)]
182pub struct ResultModulation {
183    pub result: ModulationPtr,
184    pub err_len: u32,
185    pub err: ConstPtr,
186}
187
188#[repr(C)]
189#[derive(Debug, Clone, Copy)]
190pub struct ResultDatagram {
191    pub result: DatagramPtr,
192    pub err_len: u32,
193    pub err: ConstPtr,
194}
195
196impl<T: DynamicDatagram> From<Result<T, AUTDInternalError>> for ResultDatagram {
197    fn from(r: Result<T, AUTDInternalError>) -> Self {
198        match r {
199            Ok(v) => Self {
200                result: DatagramPtr::new(v),
201                err_len: 0,
202                err: std::ptr::null_mut(),
203            },
204            Err(e) => {
205                let err = e.to_string();
206                Self {
207                    result: DatagramPtr(std::ptr::null()),
208                    err_len: err.as_bytes().len() as u32 + 1,
209                    err: Box::into_raw(Box::new(err)) as _,
210                }
211            }
212        }
213    }
214}