Skip to main content

EncodedString

Struct EncodedString 

Source
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

Source

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:

Source

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);
Source

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");
Source

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.

Source

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.

Source

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

Source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for EncodedString

Source§

fn clone(&self) -> EncodedString

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for EncodedString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for EncodedString

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for EncodedString

Source§

impl From<EncodedString> for String

Source§

fn from(encoded: EncodedString) -> Self

Converts to this type from the input type.
Source§

impl FromStr for EncodedString

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for EncodedString

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for EncodedString

Source§

fn eq(&self, other: &EncodedString) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for EncodedString

Source§

impl<'a> TryFrom<&'a str> for EncodedString

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(s: &'a str) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<String> for EncodedString

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(s: String) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.