diff options
Diffstat (limited to 'src/util/mod.rs')
-rw-r--r-- | src/util/mod.rs | 14 |
1 files changed, 13 insertions, 1 deletions
diff --git a/src/util/mod.rs b/src/util/mod.rs index d12337c..5dde295 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -21,7 +21,7 @@ use crate::{ common::AsCborValue, CoseError, Result, }; -use alloc::{boxed::Box, vec::Vec}; +use alloc::{boxed::Box, string::String, vec::Vec}; #[cfg(test)] mod tests; @@ -71,6 +71,9 @@ where /// Extractor for [`Value::Tag`] fn try_as_tag(self) -> Result<(u64, Box<Value>)>; + + /// Extractor for [`Value::Text`] + fn try_as_string(self) -> Result<String>; } impl ValueTryAs for Value { @@ -131,6 +134,14 @@ impl ValueTryAs for Value { cbor_type_error(&self, "tag") } } + + fn try_as_string(self) -> Result<String> { + if let Value::Text(s) = self { + Ok(s) + } else { + cbor_type_error(&self, "tstr") + } + } } /// Convert each item of an iterator to CBOR, and wrap the lot in @@ -153,6 +164,7 @@ pub fn expect_err<T: core::fmt::Debug, E: core::fmt::Debug + core::fmt::Display> result: Result<T, E>, err_msg: &str, ) { + #[cfg(not(feature = "std"))] use alloc::format; match result { Ok(_) => { |