Skip to content

Instantly share code, notes, and snippets.

@fancellu
Created February 5, 2024 18:09
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save fancellu/12e7c4a2fbfac5abc67931d92a03782e to your computer and use it in GitHub Desktop.
Rust application to translate into morse code
use std::fmt::Formatter;
trait MorseCode {
fn to_morse_code(&self) -> Message;
}
impl MorseCode for String {
fn to_morse_code(&self) -> Message {
self.chars()
.flat_map(|c| c.to_ascii_lowercase().to_morse_code())
.collect()
}
}
impl MorseCode for char {
// Yes. I know I could use a HashMap
fn to_morse_code(&self) -> Message {
let pulses = match self {
'a' => vec![Pulse::Short, Pulse::Long],
'b' => vec![Pulse::Long, Pulse::Short, Pulse::Short, Pulse::Short],
'c' => vec![Pulse::Long, Pulse::Short, Pulse::Long, Pulse::Short],
'd' => vec![Pulse::Long, Pulse::Short, Pulse::Short],
'e' => vec![Pulse::Short],
'f' => vec![Pulse::Short, Pulse::Short, Pulse::Long, Pulse::Short],
'g' => vec![Pulse::Long, Pulse::Long, Pulse::Short],
'h' => vec![Pulse::Short, Pulse::Short, Pulse::Short, Pulse::Short],
'i' => vec![Pulse::Short, Pulse::Short],
'j' => vec![Pulse::Short, Pulse::Long, Pulse::Long, Pulse::Long],
'k' => vec![Pulse::Long, Pulse::Short, Pulse::Long],
'l' => vec![Pulse::Short, Pulse::Long, Pulse::Short, Pulse::Short],
'm' => vec![Pulse::Long, Pulse::Long],
'n' => vec![Pulse::Long, Pulse::Short],
'o' => vec![Pulse::Long, Pulse::Long, Pulse::Long],
'p' => vec![Pulse::Short, Pulse::Long, Pulse::Long, Pulse::Short],
'q' => vec![Pulse::Long, Pulse::Long, Pulse::Short, Pulse::Long],
'r' => vec![Pulse::Short, Pulse::Long, Pulse::Short],
's' => vec![Pulse::Short, Pulse::Short, Pulse::Short],
't' => vec![Pulse::Long],
'u' => vec![Pulse::Short, Pulse::Short, Pulse::Long],
'v' => vec![Pulse::Short, Pulse::Short, Pulse::Short, Pulse::Long],
'w' => vec![Pulse::Short, Pulse::Long, Pulse::Long],
'x' => vec![Pulse::Long, Pulse::Short, Pulse::Short, Pulse::Long],
'y' => vec![Pulse::Long, Pulse::Short, Pulse::Long, Pulse::Long],
'z' => vec![Pulse::Long, Pulse::Long, Pulse::Short, Pulse::Short],
'0' => vec![
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Long,
],
'1' => vec![
Pulse::Short,
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Long,
],
'2' => vec![
Pulse::Short,
Pulse::Short,
Pulse::Long,
Pulse::Long,
Pulse::Long,
],
'3' => vec![
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Long,
Pulse::Long,
],
'4' => vec![
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Long,
],
'5' => vec![
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Short,
],
'6' => vec![
Pulse::Long,
Pulse::Short,
Pulse::Short,
Pulse::Short,
Pulse::Short,
],
'7' => vec![
Pulse::Long,
Pulse::Long,
Pulse::Short,
Pulse::Short,
Pulse::Short,
],
'8' => vec![
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Short,
Pulse::Short,
],
'9' => vec![
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Long,
Pulse::Short,
],
_ => vec![],
};
vec![pulses]
}
}
type Message = Vec<Letter>;
type Letter = Vec<Pulse>;
#[derive(Debug, PartialEq)]
enum Pulse {
Short,
Long,
}
impl std::fmt::Display for Pulse {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
match self {
Pulse::Short => write!(f, "."),
Pulse::Long => write!(f, "-"),
}
}
}
fn print_morse_code(message: Message) {
for letter in message {
for pulse in letter {
print!("{}", pulse);
}
print!(" ");
}
println!();
}
fn main() {
let message = String::from("Hello, World 0123456789").to_morse_code();
print_morse_code(message);
}
.... . .-.. .-.. --- .-- --- .-. .-.. -.. ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----.
@fancellu
Copy link
Author

fancellu commented Feb 5, 2024

You can decode here https://morsedecoder.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment