Crates.io | maybe-string |
lib.rs | maybe-string |
version | 0.1.0 |
source | src |
created_at | 2021-02-25 17:34:35.384241 |
updated_at | 2021-02-25 17:34:35.384241 |
description | Provides a newtype wrapper that represents a byte vector that may be a valid UTF-8 string |
homepage | https://sr.ht/~jpastuszek/maybe-string/ |
repository | https://git.sr.ht/~jpastuszek/maybe-string |
max_upload_size | |
id | 360562 |
size | 11,260 |
Provides a newtype wrapper MaybeString
and its slice counterpart MaybeStr
that represents a byte vector that may be a valid UTF-8 string.
These wrappers are useful when working with data that may be a valid UTF-8 string and you want to delay or conditionally skip its conversion to the string.
They are also useful for debugging data that may be displayed as a string.
The Debug
output will provide string representation when the wrapped byte vector is a valid UTF-8 string.
use maybe_string::MaybeString;
// invalid UTF-8 bytes
let ms = MaybeString(vec![0, 159, 146, 150]);
assert_eq!(&format!("{:?}", ms), "[00, 9f, 92, 96]");
// valid UTF-8 bytes
let ms = MaybeString(vec![240, 159, 146, 150]);
assert_eq!(&format!("{:?}", ms), "\"💖\"");
use maybe_string::MaybeString;
// invalid UTF-8 bytes
let ms = MaybeString(vec![0, 159, 146, 150]);
assert_eq!(ms.into_string(), Err(vec![0, 159, 146, 150]));
// valid UTF-8 bytes
let ms = MaybeString(vec![240, 159, 146, 150]);
assert_eq!(ms.into_string(), Ok("💖".to_string()));