-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from cpwood/main
Telepen support
- Loading branch information
Showing
39 changed files
with
939 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use crate::Exceptions; | ||
use crate::common::Result; | ||
|
||
pub fn calculate_checksum(contents: &str) -> char { | ||
let mut sum = 0; | ||
|
||
for c in contents.chars() { | ||
sum += c as u32; | ||
} | ||
|
||
let remainder = sum % 127; | ||
let diff = 127 - remainder; | ||
return if diff != 127 { | ||
diff as u8 as char | ||
} | ||
else { | ||
0 as char | ||
}; | ||
} | ||
|
||
pub fn ascii_to_numeric(contents: &str) -> String { | ||
let mut number = String::new(); | ||
|
||
for c in contents.chars().map(|x| x as u32) { | ||
if c >= 27 { | ||
number.push_str(&format!("{:0>2}", (c - 27))); | ||
} | ||
else { | ||
number.push_str(&format!("{:0>2}", (c - 17))); | ||
} | ||
} | ||
|
||
return number; | ||
} | ||
|
||
pub fn numeric_to_ascii(contents: &str) -> Result<String> { | ||
if contents.len() % 2 != 0 { | ||
return Err(Exceptions::illegal_argument_with("Input must contain an even number of characters.")); | ||
} | ||
|
||
let mut ascii = String::new(); | ||
let mut i = 0; | ||
|
||
while i < contents.len() { | ||
let first = contents.chars().nth(i).unwrap() as u8; | ||
let second = contents.chars().nth(i + 1).unwrap() as u8; | ||
|
||
if second == 88 && first >= 48 && first <= 57 { | ||
ascii.push((17 + first - 48) as char); | ||
} | ||
else if second >= 48 && second <= 57 && first >= 48 && first <= 57 { | ||
ascii.push((27 + (first - 48) * 10 + (second - 48)) as char); | ||
} | ||
else { | ||
return Err(Exceptions::illegal_argument_with(format!("Input contains an invalid character around position {}.", i.to_string()))); | ||
} | ||
|
||
i += 2; | ||
} | ||
|
||
return Ok(ascii); | ||
} | ||
|
||
#[test] | ||
fn telepen_checksum_test1() { | ||
let contents = "Hello world!"; | ||
let checksum = calculate_checksum(contents); | ||
assert_eq!('\u{1a}', checksum); | ||
} | ||
|
||
#[test] | ||
fn telepen_checksum_test2() { | ||
let contents = "ABC123456"; | ||
let checksum = calculate_checksum(contents); | ||
assert_eq!('\u{1}', checksum); | ||
} | ||
|
||
#[test] | ||
fn telepen_alpha_to_numeric_test() { | ||
let mut ascii = "'=Siu"; | ||
let mut result = ascii_to_numeric(ascii); | ||
assert_eq!("1234567890", result); | ||
|
||
ascii = "& oe"; | ||
result = ascii_to_numeric(ascii); | ||
assert_eq!("11058474", result); | ||
} | ||
|
||
#[test] | ||
fn telepen_numeric_to_ascii_test() { | ||
let mut numeric = "1234567890"; | ||
let mut result = numeric_to_ascii(numeric).unwrap(); | ||
assert_eq!("'=Siu", result); | ||
|
||
numeric = "11058474"; | ||
result = numeric_to_ascii(numeric).unwrap(); | ||
assert_eq!("& oe", result); | ||
} |
Oops, something went wrong.