Struct BinaryString

Source
pub struct BinaryString(pub String);
Expand description

§BinaryString (0bString + 0bWString)

This Struct is the Main Struct for Dealing with Binary Strings and Binary Whitespace Strings.

It is a Tuple Struct that only has a Single, Public Field which is a String and can be accessed with self.0.

§How To Access The String Field

use to_binary::BinaryString;

// Generate Binary String From &str
let x = BinaryString::from("Hello");

// Access The Public "String" Field And Assign It To bin_string using field
let bin_string: String = x.0;

// Print Out Information
println!("{}",bin_string);

Tuple Fields§

§0: String

Implementations§

Source§

impl BinaryString

Source

pub fn from_hex<T: AsRef<[u8]>>(n: T) -> Result<BinaryString, FromHexError>

Takes as input hexadecimal and outputs a Result containing a BinaryString and on Error, FromHexError

Examples found in repository?
examples/usage.rs (line 31)
30fn generate() -> BinaryString {
31    return BinaryString::from_hex("321155ED37271DE1A9C1914A92A5DFE4").unwrap();
32}
More examples
Hide additional examples
examples/conversion.rs (line 16)
11fn hexadecimal() {
12    // Hex-Digest from Blake2b-128bits of "TEST"
13    let input_hex = "5AECF81369E312D6A3A92DD14CEDCD66";
14
15    // Convert To Binary And Unwraps The Result | Input can be &str or String
16    let bin_string = BinaryString::from_hex(input_hex).unwrap();
17
18    // Prints Out Information
19    println!("Blake2b Hexadecimal: {}", input_hex);
20    println!("Binary String: {}", bin_string);
21}
Source§

impl BinaryString

Source

pub fn assert_binary(&self) -> bool

Asserts The Input Is A Binary String, or a string with only:

  • 0
  • 1

This function is the same as assert_binary_string() just with a different name

Examples found in repository?
examples/usage.rs (line 12)
4fn main() {
5    // A Demo For Working With Whitespace
6    work_with_whitespace();
7
8    // Generates A New BinaryString From Hexadecimal
9    let x = generate();
10
11    // Checks Whether The Input Is Binary
12    let check_if_binary: bool = x.assert_binary();
13
14    // Assert Input Is Binary
15    assert_eq!(check_if_binary, true);
16
17    // Retrieve Sizes Of Binary Input (Bits and Bytes)
18    let size_in_bits = x.bits().unwrap();
19    let size_in_bytes = x.bytes().unwrap();
20
21    // Verifies Sizes Of Binary Inputs
22    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
23    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);
24
25    // Assert Sizes Are Correct
26    assert_eq!(verify_bit_length, true);
27    assert_eq!(verify_byte_length, true);
28}
Source

pub fn assert_binary_string(&self) -> bool

Asserts the Input Is A Binary String, or a String with only:

  • 0
  • 1
  • No Whitespace
Source

pub fn assert_binary_whitespace(&self) -> bool

Asserts The Input Is A Binary Whitespace String, or a String with only:

  • 0
  • 1
  • whitespace

TODO: Add a check for whitespace to be every 8th character

Source

pub fn assert_bytes(&self) -> bool

Asserts The Input Has (8 * n) bits or contains full bytes using the remainder function

This function calls the bits() method to retrieve the number of bits then does operation % 8 and compares it against 0

Source

pub fn assert_bit_length(&self, len: usize) -> bool

Asserts The Number of Bits In The String

The function bits() is called to compare to the parameter

Examples found in repository?
examples/usage.rs (line 22)
4fn main() {
5    // A Demo For Working With Whitespace
6    work_with_whitespace();
7
8    // Generates A New BinaryString From Hexadecimal
9    let x = generate();
10
11    // Checks Whether The Input Is Binary
12    let check_if_binary: bool = x.assert_binary();
13
14    // Assert Input Is Binary
15    assert_eq!(check_if_binary, true);
16
17    // Retrieve Sizes Of Binary Input (Bits and Bytes)
18    let size_in_bits = x.bits().unwrap();
19    let size_in_bytes = x.bytes().unwrap();
20
21    // Verifies Sizes Of Binary Inputs
22    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
23    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);
24
25    // Assert Sizes Are Correct
26    assert_eq!(verify_bit_length, true);
27    assert_eq!(verify_byte_length, true);
28}
Source

pub fn assert_byte_length(&self, len: usize) -> bool

Asserts The Number of Bytes In The String

The function bytes() is called to compare to the parameter

Examples found in repository?
examples/usage.rs (line 23)
4fn main() {
5    // A Demo For Working With Whitespace
6    work_with_whitespace();
7
8    // Generates A New BinaryString From Hexadecimal
9    let x = generate();
10
11    // Checks Whether The Input Is Binary
12    let check_if_binary: bool = x.assert_binary();
13
14    // Assert Input Is Binary
15    assert_eq!(check_if_binary, true);
16
17    // Retrieve Sizes Of Binary Input (Bits and Bytes)
18    let size_in_bits = x.bits().unwrap();
19    let size_in_bytes = x.bytes().unwrap();
20
21    // Verifies Sizes Of Binary Inputs
22    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
23    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);
24
25    // Assert Sizes Are Correct
26    assert_eq!(verify_bit_length, true);
27    assert_eq!(verify_byte_length, true);
28}
Source

pub fn bits(&self) -> Result<usize, BinaryError>

Count number of bits for both a “Binary String” and “Binary Whitespace String” and returns a Result of either a usize or a BinaryError

Examples found in repository?
examples/usage.rs (line 18)
4fn main() {
5    // A Demo For Working With Whitespace
6    work_with_whitespace();
7
8    // Generates A New BinaryString From Hexadecimal
9    let x = generate();
10
11    // Checks Whether The Input Is Binary
12    let check_if_binary: bool = x.assert_binary();
13
14    // Assert Input Is Binary
15    assert_eq!(check_if_binary, true);
16
17    // Retrieve Sizes Of Binary Input (Bits and Bytes)
18    let size_in_bits = x.bits().unwrap();
19    let size_in_bytes = x.bytes().unwrap();
20
21    // Verifies Sizes Of Binary Inputs
22    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
23    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);
24
25    // Assert Sizes Are Correct
26    assert_eq!(verify_bit_length, true);
27    assert_eq!(verify_byte_length, true);
28}
Source

pub fn bytes(&self) -> Result<usize, BinaryError>

Count number of bytes for both a “Binary String” and “Binary Whitespace String” and returns a Result of either a usize or on error, empty response

Examples found in repository?
examples/usage.rs (line 19)
4fn main() {
5    // A Demo For Working With Whitespace
6    work_with_whitespace();
7
8    // Generates A New BinaryString From Hexadecimal
9    let x = generate();
10
11    // Checks Whether The Input Is Binary
12    let check_if_binary: bool = x.assert_binary();
13
14    // Assert Input Is Binary
15    assert_eq!(check_if_binary, true);
16
17    // Retrieve Sizes Of Binary Input (Bits and Bytes)
18    let size_in_bits = x.bits().unwrap();
19    let size_in_bytes = x.bytes().unwrap();
20
21    // Verifies Sizes Of Binary Inputs
22    let verify_bit_length: bool = x.assert_bit_length(size_in_bits);
23    let verify_byte_length: bool = x.assert_byte_length(size_in_bytes);
24
25    // Assert Sizes Are Correct
26    assert_eq!(verify_bit_length, true);
27    assert_eq!(verify_byte_length, true);
28}
Source

pub fn remove_spaces(&self) -> BinaryString

Removes All Whitespace From Binary String And Returns A BinaryString

Examples found in repository?
examples/usage.rs (line 42)
34fn work_with_whitespace() {
35    // Generate From &str "Hello"
36    let initial = BinaryString::from("Hello");
37
38    // Add Spaces Between Each Byte In The `BinaryString` And Unwraps Result For Error-Checking Using `BinaryError`
39    let spaces = initial.add_spaces().unwrap();
40
41    // Removes All Whitespace In The `BinaryString`
42    let removed_spaces = spaces.remove_spaces();
43
44    // Asserts The Initial Result And The One With Removed Spaces Are The Same
45    assert_eq!(initial, removed_spaces);
46}
Source

pub fn add_spaces(&self) -> Result<BinaryString, BinaryError>

Adds Whitespace Between Every Byte (8 bits)

§Example Usage
// Imports
use to_binary::{BinaryString,BinaryError};
 
// Generate `BinaryString` from &str "Test String"
let x = BinaryString::from("Test String");
 
// Returns a `BinaryString` With Spaces Between Every Byte (8 bits)
let bin_with_spaces = x.add_spaces().unwrap();
 
Examples found in repository?
examples/conversion.rs (line 71)
63fn byte() {
64    // Single u8 Byte
65    let byte: u8 = 111u8;
66
67    // `BinaryString` from a Single Byte
68    let bin_string = BinaryString::from(byte);
69
70    // Attempt To Add Spaces
71    let spaces = bin_string.add_spaces().unwrap();
72
73    assert_eq!(bin_string, spaces);
74}
More examples
Hide additional examples
examples/usage.rs (line 39)
34fn work_with_whitespace() {
35    // Generate From &str "Hello"
36    let initial = BinaryString::from("Hello");
37
38    // Add Spaces Between Each Byte In The `BinaryString` And Unwraps Result For Error-Checking Using `BinaryError`
39    let spaces = initial.add_spaces().unwrap();
40
41    // Removes All Whitespace In The `BinaryString`
42    let removed_spaces = spaces.remove_spaces();
43
44    // Asserts The Initial Result And The One With Removed Spaces Are The Same
45    assert_eq!(initial, removed_spaces);
46}

Trait Implementations§

Source§

impl Clone for BinaryString

Source§

fn clone(&self) -> BinaryString

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for BinaryString

Source§

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

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

impl Default for BinaryString

Source§

fn default() -> BinaryString

Returns the “default value” for a type. Read more
Source§

impl Display for BinaryString

Source§

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

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

impl From<&[u8]> for BinaryString

Source§

fn from(byte_slice: &[u8]) -> Self

Converts to this type from the input type.
Source§

impl From<&str> for BinaryString

Source§

fn from(n: &str) -> Self

Converts to this type from the input type.
Source§

impl From<[u8; 32]> for BinaryString

Source§

fn from(array: [u8; 32]) -> Self

Converts to this type from the input type.
Source§

impl From<String> for BinaryString

Source§

fn from(n: String) -> Self

Converts to this type from the input type.
Source§

impl From<Vec<u8>> for BinaryString

Source§

fn from(bytes: Vec<u8>) -> BinaryString

Converts to this type from the input type.
Source§

impl From<u8> for BinaryString

Source§

fn from(byte: u8) -> BinaryString

Converts to this type from the input type.
Source§

impl Hash for BinaryString

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 BinaryString

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BinaryString

Source§

fn partial_cmp(&self, other: &BinaryString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for BinaryString

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.