cairo/
utils.rs

1// Take a look at the license at the top of the repository in the LICENSE file.
2
3use std::{ffi::CStr, fmt};
4
5use crate::{ffi, Error};
6
7// rustdoc-stripper-ignore-next
8/// Resets all static data within cairo to its original state (i.e. identical to the state at program
9/// invocation). For example, all caches within cairo will be flushed empty.
10///
11/// # Safety
12/// It is only safe to call this function when there are no active cairo objects remaining (all
13/// cairo objects have been dropped).
14///
15/// This function is thread safe.
16#[doc(alias = "cairo_debug_reset_static_data")]
17pub unsafe fn debug_reset_static_data() {
18    ffi::cairo_debug_reset_static_data()
19}
20
21pub fn status_to_result(status: ffi::cairo_status_t) -> Result<(), Error> {
22    match status {
23        ffi::STATUS_SUCCESS => Ok(()),
24        err => Err(err.into()),
25    }
26}
27
28#[doc(alias = "cairo_version_string")]
29#[doc(alias = "get_version_string")]
30pub fn version_string() -> &'static str {
31    unsafe {
32        let ptr = ffi::cairo_version_string();
33        CStr::from_ptr(ptr)
34            .to_str()
35            .expect("invalid version string")
36    }
37}
38
39#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
40pub struct Version {
41    major: u8,
42    minor: u8,
43    micro: u8,
44}
45
46impl Version {
47    #[doc(alias = "cairo_version")]
48    #[doc(alias = "get_version")]
49    pub fn new() -> Version {
50        let version = unsafe { ffi::cairo_version() };
51        Version {
52            major: (version / 10_000 % 100) as _,
53            minor: (version / 100 % 100) as _,
54            micro: (version % 100) as _,
55        }
56    }
57
58    pub fn major(self) -> u8 {
59        self.major
60    }
61    pub fn minor(self) -> u8 {
62        self.minor
63    }
64    pub fn micro(self) -> u8 {
65        self.micro
66    }
67}
68
69impl Default for Version {
70    fn default() -> Self {
71        Self::new()
72    }
73}
74
75impl fmt::Display for Version {
76    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77        write!(f, "{}.{}.{}", self.major, self.minor, self.micro)
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn check_versions() {
87        assert_eq!(version_string(), Version::new().to_string());
88    }
89}