diff --git a/src/dec/mod.rs b/src/dec/mod.rs index 827c268..1953830 100644 --- a/src/dec/mod.rs +++ b/src/dec/mod.rs @@ -84,6 +84,20 @@ impl<'a, 'b> Term<'a, 'b> { self.0.raw() } + /// Get the term's raw representation as an str + /// + /// Example: + /// + /// ``` + /// use nettext::dec::decode; + /// + /// let term = decode(b"hello { a = x, b = y }").unwrap(); + /// assert_eq!(term.raw_str().unwrap(), "hello { a = x, b = y }"); + /// ``` + pub fn raw_str(&self) -> Result<&'a str, TypeError> { + Ok(std::str::from_utf8(self.0.raw())?) + } + /// If the term is a single string, get that string /// /// Example: diff --git a/src/enc/mod.rs b/src/enc/mod.rs index 9a06f10..c2b74c9 100644 --- a/src/enc/mod.rs +++ b/src/enc/mod.rs @@ -268,6 +268,13 @@ impl<'a> Term<'a> { self.0.encode_aux(&mut buf, 0, true); buf } + + /// Generate the nettext representation of a term, as a String + pub fn encode_string(self) -> String { + unsafe { + String::from_utf8_unchecked(self.encode()) + } + } } impl<'a> T<'a> {