visa_device_handler/visa_interface/
object.rs1#![allow(non_upper_case_globals)]
2#![allow(non_camel_case_types)]
3#![allow(non_snake_case)]
4#![allow(dead_code)]
5
6
7use super::visa_ffi::*;
8use super::err::VisaWrapperError;
9use dlopen2::wrapper::Container;
10use std::borrow::Cow;
11#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
13pub enum Binary {
14 Keysight,
16 NiVisa,
18 #[default]
19 PlatformDefault,
21 Custom(String),
23}
24
25impl Binary {
26 fn binary_name(&self) -> Result<Cow<str>, VisaWrapperError> {
27 Ok(match self {
28 Binary::Keysight => {
29 if cfg!(target_family = "windows") {
30 "ktvisa32.dll".into()
31 } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
32 "libiovisa.so".into()
33 } else {
34 return Err(VisaWrapperError::UnsupportedPlatform);
35 }
36 } Binary::NiVisa => {
38 if cfg!(target_family = "windows") && cfg!(target_pointer_width = "64") {
39 "nivisa64.dll".into()
40 } else if cfg!(target_family = "windows") && cfg!(target_pointer_width = "32") {
41 "visa32.dll".into()
42 } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
43 "libvisa.so".into()
44 } else {
45 return Err(VisaWrapperError::UnsupportedPlatform);
46 }
47 } Binary::PlatformDefault => {
49 if cfg!(target_family = "windows") {
50 "visa32".into()
51 } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "64") {
52 "libvisa.so".into()
53 } else if cfg!(target_family = "unix") && cfg!(target_pointer_width = "32") {
54 "libvisa32.so".into()
55 } else {
56 return Err(VisaWrapperError::UnsupportedPlatform);
57 }
58 }
59 Binary::Custom(path) => path.into(),
60 })
61 }
62}
63
64impl ToString for Binary {
65 fn to_string(&self) -> String {
66 self.binary_name()
67 .unwrap_or_else(|e| e.to_string().into())
68 .into()
69 }
70}
71pub fn make(bin: &Binary) -> Result<Container<VisaFFI>, VisaWrapperError> {
79 unsafe { Container::load(bin.binary_name()?.as_ref()).map_err(|e| VisaWrapperError::from(e)) }
80}