1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | |
3 | use std::{fmt, num::NonZeroU32}; |
4 | |
5 | use crate::{ffi, translate::*, GStr}; |
6 | |
7 | #[derive (Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)] |
8 | #[repr (transparent)] |
9 | #[doc (alias = "GQuark" )] |
10 | pub struct Quark(NonZeroU32); |
11 | |
12 | impl Quark { |
13 | #[doc (alias = "g_quark_from_string" )] |
14 | #[allow (clippy::should_implement_trait)] |
15 | pub fn from_str(s: impl IntoGStr) -> Self { |
16 | unsafe { s.run_with_gstr(|s: &GStr| from_glib(val:ffi::g_quark_from_string(s.as_ptr()))) } |
17 | } |
18 | |
19 | #[doc (alias = "g_quark_from_static_string" )] |
20 | #[allow (clippy::should_implement_trait)] |
21 | pub fn from_static_str(s: &'static GStr) -> Self { |
22 | unsafe { from_glib(val:ffi::g_quark_from_static_string(s.as_ptr())) } |
23 | } |
24 | |
25 | #[allow (clippy::trivially_copy_pass_by_ref)] |
26 | #[doc (alias = "g_quark_to_string" )] |
27 | pub fn as_str<'a>(&self) -> &'a GStr { |
28 | unsafe { GStr::from_ptr(ffi::g_quark_to_string(self.into_glib())) } |
29 | } |
30 | |
31 | #[doc (alias = "g_quark_try_string" )] |
32 | pub fn try_from_str(s: &str) -> Option<Self> { |
33 | unsafe { Self::try_from_glib(val:ffi::g_quark_try_string(s.to_glib_none().0)).ok() } |
34 | } |
35 | } |
36 | |
37 | impl fmt::Debug for Quark { |
38 | fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
39 | f.write_str(data:Quark::as_str(self)) |
40 | } |
41 | } |
42 | |
43 | impl<T: IntoGStr> From<T> for Quark { |
44 | fn from(s: T) -> Self { |
45 | Self::from_str(s) |
46 | } |
47 | } |
48 | |
49 | impl std::str::FromStr for Quark { |
50 | type Err = std::convert::Infallible; |
51 | |
52 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
53 | Ok(Self::from_str(s)) |
54 | } |
55 | } |
56 | |
57 | #[doc (hidden)] |
58 | impl FromGlib<ffi::GQuark> for Quark { |
59 | #[inline ] |
60 | unsafe fn from_glib(value: ffi::GQuark) -> Self { |
61 | debug_assert_ne!(value, 0); |
62 | Self(NonZeroU32::new_unchecked(value)) |
63 | } |
64 | } |
65 | |
66 | #[doc (hidden)] |
67 | impl TryFromGlib<ffi::GQuark> for Quark { |
68 | type Error = GlibNoneError; |
69 | unsafe fn try_from_glib(value: ffi::GQuark) -> Result<Self, Self::Error> { |
70 | if value == 0 { |
71 | Err(GlibNoneError) |
72 | } else { |
73 | Ok(Self(NonZeroU32::new_unchecked(value))) |
74 | } |
75 | } |
76 | } |
77 | |
78 | #[doc (hidden)] |
79 | impl IntoGlib for Quark { |
80 | type GlibType = ffi::GQuark; |
81 | |
82 | #[inline ] |
83 | fn into_glib(self) -> ffi::GQuark { |
84 | self.0.get() |
85 | } |
86 | } |
87 | |
88 | #[doc (hidden)] |
89 | impl IntoGlib for Option<Quark> { |
90 | type GlibType = ffi::GQuark; |
91 | |
92 | #[inline ] |
93 | fn into_glib(self) -> ffi::GQuark { |
94 | self.map(|s| s.0.get()).unwrap_or(default:0) |
95 | } |
96 | } |
97 | |
98 | #[cfg (test)] |
99 | mod tests { |
100 | use super::*; |
101 | |
102 | #[test ] |
103 | fn test_from_str() { |
104 | let q1 = Quark::from_str("some-quark" ); |
105 | let q2 = Quark::try_from_str("some-quark" ); |
106 | assert_eq!(Some(q1), q2); |
107 | assert_eq!(q1.as_str(), "some-quark" ); |
108 | } |
109 | } |
110 | |