1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::translate::*;
use std::ffi::CStr;
use std::fmt;
use std::num::NonZeroU32;

#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
#[repr(transparent)]
#[doc(alias = "GQuark")]
pub struct Quark(NonZeroU32);

impl Quark {
    #[doc(alias = "g_quark_from_string")]
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Quark {
        unsafe { from_glib(ffi::g_quark_from_string(s.to_glib_none().0)) }
    }

    #[allow(clippy::trivially_copy_pass_by_ref)]
    #[doc(alias = "g_quark_to_string")]
    pub fn as_str<'a>(&self) -> &'a str {
        unsafe {
            CStr::from_ptr(ffi::g_quark_to_string(self.into_glib()))
                .to_str()
                .unwrap()
        }
    }

    #[doc(alias = "g_quark_try_string")]
    pub fn try_from_str(s: &str) -> Option<Quark> {
        unsafe { Self::try_from_glib(ffi::g_quark_try_string(s.to_glib_none().0)).ok() }
    }
}

impl fmt::Debug for Quark {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        f.write_str(Quark::as_str(self))
    }
}

impl<'a> From<&'a str> for Quark {
    fn from(s: &'a str) -> Self {
        Self::from_str(s)
    }
}

impl std::str::FromStr for Quark {
    type Err = std::convert::Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::from_str(s))
    }
}

#[doc(hidden)]
impl FromGlib<ffi::GQuark> for Quark {
    unsafe fn from_glib(value: ffi::GQuark) -> Self {
        assert_ne!(value, 0);
        Self(NonZeroU32::new_unchecked(value))
    }
}

#[doc(hidden)]
impl TryFromGlib<ffi::GQuark> for Quark {
    type Error = GlibNoneError;
    unsafe fn try_from_glib(value: ffi::GQuark) -> Result<Self, Self::Error> {
        if value == 0 {
            Err(GlibNoneError)
        } else {
            Ok(Self(NonZeroU32::new_unchecked(value)))
        }
    }
}

#[doc(hidden)]
impl IntoGlib for Quark {
    type GlibType = ffi::GQuark;

    fn into_glib(self) -> ffi::GQuark {
        self.0.get()
    }
}

#[doc(hidden)]
impl IntoGlib for Option<Quark> {
    type GlibType = ffi::GQuark;

    fn into_glib(self) -> ffi::GQuark {
        self.map(|s| s.0.get()).unwrap_or(0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_from_str() {
        let q1 = Quark::from_str("some-quark");
        let q2 = Quark::try_from_str("some-quark");
        assert_eq!(Some(q1), q2);
        assert_eq!(q1.as_str(), "some-quark");
    }
}