pub struct EncodedString { /* private fields */ }Expand description
A validated multibase-encoded string.
This type provides type-level guarantees that a string contains a valid base code prefix and can be decoded (though decoding may still fail if the data is malformed).
§Construction
Use EncodedString::new or parse from a string using core::str::FromStr:
use multi_base::EncodedString;
use std::str::FromStr;
// Using new()
let encoded = EncodedString::new("zCn8eVZg").unwrap();
// Using FromStr
let encoded: EncodedString = "md29ybGQ".parse().unwrap();§Errors
Construction fails if:
- The input string is empty
- The first character is not a valid base code
Note that construction only validates the base code prefix, not the
encoded data itself. Use decode() to fully validate
and decode the data.
Implementations§
Source§impl EncodedString
impl EncodedString
Sourcepub fn new<S: Into<String>>(s: S) -> Result<Self>
pub fn new<S: Into<String>>(s: S) -> Result<Self>
Create a new validated multibase-encoded string.
This function validates that the input string has a valid base code
prefix. It does not decode the full data, so malformed encoded data
will only be detected when calling decode().
§Examples
use multi_base::{EncodedString, Base};
// Valid multibase string
let encoded = EncodedString::new("zCn8eVZg").unwrap();
assert_eq!(encoded.base(), Base::Base58Btc);
// Empty string fails
assert!(EncodedString::new("").is_err());
// Invalid base code fails
assert!(EncodedString::new("xInvalid").is_err());§Errors
Returns an error if:
- The input string is empty (
Error::EmptyInput) - The first character is not a valid base code (
Error::UnknownBase)
Sourcepub fn base(&self) -> Base
pub fn base(&self) -> Base
Get the base encoding of this string.
§Examples
use multi_base::{EncodedString, Base};
let encoded = EncodedString::new("zCn8eVZg").unwrap();
assert_eq!(encoded.base(), Base::Base58Btc);
let encoded = EncodedString::new("md29ybGQ").unwrap();
assert_eq!(encoded.base(), Base::Base64);Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the encoded string as a string slice.
§Examples
use multi_base::EncodedString;
let encoded = EncodedString::new("zCn8eVZg").unwrap();
assert_eq!(encoded.as_str(), "zCn8eVZg");Sourcepub fn decode(&self) -> Result<Vec<u8>>
pub fn decode(&self) -> Result<Vec<u8>>
Decode this validated multibase string.
This performs the actual decoding of the data. Unlike construction, which only validates the base code prefix, this method validates and decodes the entire encoded data.
§Examples
use multi_base::EncodedString;
let encoded = EncodedString::new("zCn8eVZg").unwrap();
let decoded = encoded.decode().unwrap();
assert_eq!(decoded, b"hello");§Errors
Returns an error if the encoded data is malformed for the detected base.
Sourcepub fn decode_with_strictness(&self, strict: bool) -> Result<Vec<u8>>
pub fn decode_with_strictness(&self, strict: bool) -> Result<Vec<u8>>
Decode this validated multibase string with strictness control.
When strict is false, some bases allow case-insensitive decoding
or other permissive behaviors.
§Examples
use multi_base::EncodedString;
// Case-insensitive decoding for some bases
let encoded = EncodedString::new("FaB").unwrap(); // Base16Upper with mixed case
let decoded = encoded.decode_with_strictness(false).unwrap();
assert_eq!(decoded, b"\xab");§Errors
Returns an error if the encoded data is malformed for the detected base.
Sourcepub fn into_inner(self) -> String
pub fn into_inner(self) -> String
Consume the EncodedString and return the inner string.
§Examples
use multi_base::EncodedString;
let encoded = EncodedString::new("zCn8eVZg").unwrap();
let inner = encoded.into_inner();
assert_eq!(inner, "zCn8eVZg");Trait Implementations§
Source§impl AsRef<str> for EncodedString
impl AsRef<str> for EncodedString
Source§impl Clone for EncodedString
impl Clone for EncodedString
Source§fn clone(&self) -> EncodedString
fn clone(&self) -> EncodedString
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more