Skip to main content

libusb_wishbone_tool/
version.rs

1use std::ffi::CStr;
2use std::fmt;
3use std::str;
4
5use libusb::*;
6
7/// A structure that describes the version of the underlying `libusb` library.
8pub struct LibraryVersion {
9    inner: &'static libusb_version,
10}
11
12impl LibraryVersion {
13    /// Library major version.
14    pub fn major(&self) -> u16 {
15        self.inner.major
16    }
17
18    /// Library minor version.
19    pub fn minor(&self) -> u16 {
20        self.inner.minor
21    }
22
23    /// Library micro version.
24    pub fn micro(&self) -> u16 {
25        self.inner.micro
26    }
27
28    /// Library nano version.
29    pub fn nano(&self) -> u16 {
30        self.inner.nano
31    }
32
33    /// Library release candidate suffix string, e.g., `"-rc4"`.
34    pub fn rc(&self) -> Option<&'static str> {
35        let cstr = unsafe { CStr::from_ptr(self.inner.rc) };
36
37        match str::from_utf8(cstr.to_bytes()) {
38            Ok(s) => {
39                if !s.is_empty() {
40                    Some(s)
41                }
42                else {
43                    None
44                }
45            },
46            Err(_) => {
47                None
48            },
49        }
50    }
51}
52
53impl fmt::Debug for LibraryVersion {
54    fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
55        let mut debug = fmt.debug_struct("LibraryVersion");
56
57        debug.field("major", &self.major());
58        debug.field("minor", &self.minor());
59        debug.field("micro", &self.micro());
60        debug.field("nano", &self.nano());
61        debug.field("rc", &self.rc());
62
63        debug.finish()
64    }
65}
66
67/// Returns a structure with the version of the running libusb library.
68pub fn version() -> LibraryVersion {
69    let version: &'static libusb_version = unsafe {
70        &*libusb_get_version()
71    };
72
73    LibraryVersion { inner: version }
74}