Struct Message

Source
pub struct Message { /* private fields */ }
Expand description

Container of binary data, which also includes a header. This header is a special collection of data which isn’t used in things like encoding, as it is meant to be a lightweight message with information about the contained data.

Implementations§

Source§

impl Message

Source

pub fn new() -> Message

Create an empty message.

Examples found in repository?
examples/message/main.rs (line 4)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}
More examples
Hide additional examples
examples/sound_channel/main.rs (line 18)
3fn main() {
4    let modulation = MFSK::new(16);
5    let channel = Sound::new(&modulation);
6
7    // let time = 20;
8
9    // channel.play(500, time);
10    // channel.play(1000, time);
11    // channel.play(500, time);
12    // channel.play(1000, time);
13    // channel.play(500, time);
14    // channel.play(1000, time);
15    // channel.play(500, time);
16    // channel.play(1000, time);
17
18    let msg = Message::new().data("Hello world!");
19    channel.send(&msg, 100);
20}
examples/wav_export/main.rs (line 4)
3fn main() {
4    let msg = Message::new().data(
5        "Peter Piper picked a peck of pickled peppers
6A peck of pickled peppers Peter Piper picked
7If Peter Piper picked a peck of pickled peppers
8Where's the peck of pickled peppers Peter Piper picked?",
9    );
10
11    let time = 100;
12
13    let fsk256 = MFSK::new(256).base_frequency(100).delta_frequency(30);
14    let channel256 = Sound::new(&fsk256);
15    channel256.export_wav(&msg, "examples/wav_export/audio/256fsk.wav", time);
16
17    let fsk16 = MFSK::new(16).base_frequency(100).delta_frequency(100);
18    let channel16 = Sound::new(&fsk16);
19    channel16.export_wav(&msg, "examples/wav_export/audio/16fsk.wav", time);
20
21    let qam256 = MQAM::new(256, 1000);
22    let channel256 = Sound::new(&qam256);
23    channel256.export_wav(&msg, "examples/wav_export/audio/256qam.wav", time);
24
25    let qam16 = MQAM::new(16, 1000);
26    let channel16 = Sound::new(&qam16);
27    channel16.export_wav(&msg, "examples/wav_export/audio/16qam.wav", time);
28}
Source

pub fn from_string(data: &str, header: &str) -> Message

Create a message from some given data and a header as strings. These are transformed into binary and then stored.

Examples found in repository?
examples/huffman_coding/main.rs (line 4)
3fn main() {
4    let small_msg = Message::from_string("Hello world!", "HEADER");
5    let encoder = HuffmanCoding::from_message(&small_msg);
6    let small_enc = encoder.encode(&small_msg);
7    let small_dec = encoder.decode(&small_enc, 0);
8    println!("{}", encoder);
9    println!("{} -> DECODED : {}", small_msg, small_dec);
10    println!(
11        "\n=== SIZES ===\n{} b -> ENCODED : {} b -> DECODED : {} b",
12        small_msg.bit_size(),
13        small_enc.bit_size(),
14        small_dec.bit_size()
15    );
16
17    println!("\n\n");
18
19    let msg1 = Message::from_string(
20        "Peter Piper picked a peck of pickled peppers
21    A peck of pickled peppers Peter Piper picked
22    If Peter Piper picked a peck of pickled peppers
23    Where's the peck of pickled peppers Peter Piper picked?",
24        "THIS IS A TEST HEADER",
25    );
26    let encoder1 = HuffmanCoding::from_message(&msg1);
27    let msg1_enc = encoder1.encode(&msg1);
28    let msg1_dec = encoder1.decode(&msg1_enc, 0);
29    println!("{}", encoder);
30    println!("{} -> DECODED : {}", msg1, msg1_dec);
31    println!(
32        "\n=== SIZES ===\n{} b -> ENCODED : {} b -> DECODED : {} b",
33        msg1.bit_size(),
34        msg1_enc.bit_size(),
35        msg1_dec.bit_size()
36    );
37}
Source

pub fn from_binary(data: &str, header: &str) -> Message

Create a message from some given data and header binaries.

Source

pub fn data(self, data: &str) -> Message

Set the data of the message.

Examples found in repository?
examples/message/main.rs (line 4)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}
More examples
Hide additional examples
examples/sound_channel/main.rs (line 18)
3fn main() {
4    let modulation = MFSK::new(16);
5    let channel = Sound::new(&modulation);
6
7    // let time = 20;
8
9    // channel.play(500, time);
10    // channel.play(1000, time);
11    // channel.play(500, time);
12    // channel.play(1000, time);
13    // channel.play(500, time);
14    // channel.play(1000, time);
15    // channel.play(500, time);
16    // channel.play(1000, time);
17
18    let msg = Message::new().data("Hello world!");
19    channel.send(&msg, 100);
20}
examples/wav_export/main.rs (lines 4-9)
3fn main() {
4    let msg = Message::new().data(
5        "Peter Piper picked a peck of pickled peppers
6A peck of pickled peppers Peter Piper picked
7If Peter Piper picked a peck of pickled peppers
8Where's the peck of pickled peppers Peter Piper picked?",
9    );
10
11    let time = 100;
12
13    let fsk256 = MFSK::new(256).base_frequency(100).delta_frequency(30);
14    let channel256 = Sound::new(&fsk256);
15    channel256.export_wav(&msg, "examples/wav_export/audio/256fsk.wav", time);
16
17    let fsk16 = MFSK::new(16).base_frequency(100).delta_frequency(100);
18    let channel16 = Sound::new(&fsk16);
19    channel16.export_wav(&msg, "examples/wav_export/audio/16fsk.wav", time);
20
21    let qam256 = MQAM::new(256, 1000);
22    let channel256 = Sound::new(&qam256);
23    channel256.export_wav(&msg, "examples/wav_export/audio/256qam.wav", time);
24
25    let qam16 = MQAM::new(16, 1000);
26    let channel16 = Sound::new(&qam16);
27    channel16.export_wav(&msg, "examples/wav_export/audio/16qam.wav", time);
28}
Source

pub fn data_from_binary(self, data: &str) -> Message

Set the data of the message as a binary.

Source

pub fn header(self, header: &str) -> Message

Set the header of the message.

Examples found in repository?
examples/message/main.rs (line 4)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}
Source

pub fn header_from_binary(self, header: &str) -> Message

Set the header of the message as a binary.

Source

pub fn get_data(&self) -> &str

Get the data of the message as binary.

Source

pub fn get_header(&self) -> &str

Get the header of the message as binary.

Source

pub fn bit_size(&self) -> u32

Get the total amount of bits in the message.

Examples found in repository?
examples/message/main.rs (line 6)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}
More examples
Hide additional examples
examples/huffman_coding/main.rs (line 12)
3fn main() {
4    let small_msg = Message::from_string("Hello world!", "HEADER");
5    let encoder = HuffmanCoding::from_message(&small_msg);
6    let small_enc = encoder.encode(&small_msg);
7    let small_dec = encoder.decode(&small_enc, 0);
8    println!("{}", encoder);
9    println!("{} -> DECODED : {}", small_msg, small_dec);
10    println!(
11        "\n=== SIZES ===\n{} b -> ENCODED : {} b -> DECODED : {} b",
12        small_msg.bit_size(),
13        small_enc.bit_size(),
14        small_dec.bit_size()
15    );
16
17    println!("\n\n");
18
19    let msg1 = Message::from_string(
20        "Peter Piper picked a peck of pickled peppers
21    A peck of pickled peppers Peter Piper picked
22    If Peter Piper picked a peck of pickled peppers
23    Where's the peck of pickled peppers Peter Piper picked?",
24        "THIS IS A TEST HEADER",
25    );
26    let encoder1 = HuffmanCoding::from_message(&msg1);
27    let msg1_enc = encoder1.encode(&msg1);
28    let msg1_dec = encoder1.decode(&msg1_enc, 0);
29    println!("{}", encoder);
30    println!("{} -> DECODED : {}", msg1, msg1_dec);
31    println!(
32        "\n=== SIZES ===\n{} b -> ENCODED : {} b -> DECODED : {} b",
33        msg1.bit_size(),
34        msg1_enc.bit_size(),
35        msg1_dec.bit_size()
36    );
37}
Source

pub fn data_size(&self) -> u32

Get the amount of bits in the data part of the message.

Source

pub fn header_size(&self) -> u32

Get the amount of bits in the header part of the message.

Source

pub fn group_string(message: &String, num: u32) -> Vec<String>

Group a string in packets of a given size.

Source

pub fn group(&self, num: u32) -> Vec<String>

Group the message in packets of the given size.

Source

pub fn group_data(&self, num: u32) -> Vec<String>

Group the data in packets of the given size.

Source

pub fn group_header(&self, num: u32) -> Vec<String>

Group the header in packets of the given size.

Source

pub fn as_u8_array(&self) -> Vec<u8>

Get the message as an array of 8-bit integers.

Source

pub fn data_as_u8_array(&self) -> Vec<u8>

Get the data as an array of 8-bit integers.

Source

pub fn header_as_u8_array(&self) -> Vec<u8>

Get the header as an array of 8-bit integers.

Source

pub fn as_binary(&self) -> &str

Get the message as a binary string.

Source

pub fn data_as_binary(&self) -> &str

Get the data as a binary string.

Source

pub fn header_as_binary(&self) -> &str

Get the header as a binary string.

Source

pub fn as_string(&self) -> String

Get the message as a decoded string.

Source

pub fn data_as_string(&self) -> String

Get the data as a decoded string.

Examples found in repository?
examples/message/main.rs (line 8)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}
Source

pub fn header_as_string(&self) -> String

Get the header as a decoded string.

Examples found in repository?
examples/message/main.rs (line 7)
3fn main() {
4    let msg = Message::new().data(&"Hello world!").header(&"HEADER");
5    println!("MESSAGE {}", msg);
6    println!("BIT SIZE {}", msg.bit_size());
7    println!("HEADER {}", msg.header_as_string());
8    println!("DATA {}", msg.data_as_string());
9}

Trait Implementations§

Source§

impl Display for Message

Source§

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

Formats the value using the given formatter. Read more

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> 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> Same for T

Source§

type Output = T

Should always be Self
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.