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
impl BinaryString
Sourcepub fn from_hex<T: AsRef<[u8]>>(n: T) -> Result<BinaryString, FromHexError>
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?
More examples
Source§impl BinaryString
impl BinaryString
Sourcepub fn assert_binary(&self) -> bool
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?
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}
Sourcepub fn assert_binary_string(&self) -> bool
pub fn assert_binary_string(&self) -> bool
Asserts the Input Is A Binary String
, or a String with only:
0
1
- No Whitespace
Sourcepub fn assert_binary_whitespace(&self) -> bool
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
Sourcepub fn assert_bytes(&self) -> bool
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
Sourcepub fn assert_bit_length(&self, len: usize) -> bool
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?
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}
Sourcepub fn assert_byte_length(&self, len: usize) -> bool
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?
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}
Sourcepub fn bits(&self) -> Result<usize, BinaryError>
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?
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}
Sourcepub fn bytes(&self) -> Result<usize, BinaryError>
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?
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}
Sourcepub fn remove_spaces(&self) -> BinaryString
pub fn remove_spaces(&self) -> BinaryString
Removes All Whitespace From Binary String And Returns A BinaryString
Examples found in repository?
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}
Sourcepub fn add_spaces(&self) -> Result<BinaryString, BinaryError>
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?
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
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
impl Clone for BinaryString
Source§fn clone(&self) -> BinaryString
fn clone(&self) -> BinaryString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more