store value as GString, implement as_gstring, to_gstring methods

This commit is contained in:
yggverse 2025-01-18 03:37:26 +02:00
parent cac544043e
commit 946ff485be

View file

@ -4,12 +4,14 @@
pub mod error; pub mod error;
pub use error::Error; pub use error::Error;
use glib::GString;
/// Meta **data** holder /// Meta **data** holder
/// ///
/// For example, `value` could contain: /// For example, `value` could contain:
/// * placeholder text for 10, 11 status /// * placeholder text for 10, 11 status
/// * URL string for 30, 31 status /// * URL string for 30, 31 status
pub struct Data(String); pub struct Data(GString);
impl Data { impl Data {
// Constructors // Constructors
@ -43,7 +45,7 @@ impl Data {
} }
// Assumes the bytes are valid UTF-8 // Assumes the bytes are valid UTF-8
match String::from_utf8(bytes) { match GString::from_utf8(bytes) {
Ok(data) => Ok(match data.is_empty() { Ok(data) => Ok(match data.is_empty() {
false => Some(Self(data)), false => Some(Self(data)),
true => None, true => None,
@ -61,6 +63,16 @@ impl Data {
pub fn as_str(&self) -> &str { pub fn as_str(&self) -> &str {
self.0.as_str() self.0.as_str()
} }
/// Get `Self` as `glib::GString`
pub fn as_gstring(&self) -> &GString {
&self.0
}
/// Get `glib::GString` copy of `Self`
pub fn to_gstring(&self) -> GString {
self.0.clone()
}
} }
impl std::fmt::Display for Data { impl std::fmt::Display for Data {