Skip to content
This repository has been archived by the owner on May 29, 2022. It is now read-only.

Commit

Permalink
Modularize and add unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
aslamplr committed Mar 8, 2020
1 parent 82ebb81 commit 33a875f
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 73 deletions.
71 changes: 71 additions & 0 deletions src/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use std::collections::HashMap;
use std::error::Error;

pub fn convert_to_mltt(
text_to_convert: &str,
map: &HashMap<String, String>,
) -> Result<String, Box<dyn Error>> {
let mut text_to_convert = text_to_convert.to_owned();
let mut keys = map.keys().map(String::from).collect::<Vec<_>>();
keys.sort_by(|a, b| b.len().cmp(&a.len()));
let to_string_vec = |x: Vec<&str>| x.into_iter().map(String::from).collect::<Vec<_>>();
let _right_combinators = to_string_vec(vec!["ാ", "ി", "ീ", "ു", "ൂ", "ൃ", "ൄ", "ൌ", "്"]);
let left_combinators = to_string_vec(vec!["െ", "േ", "ൈ"]);
let combinators = to_string_vec(vec!["ൊ", "ോ"]);

for key in combinators {
if let Some(value) = map.get(&key) {
while let Some(index) = text_to_convert.find(&key) {
let middle_char = text_to_convert[index - 3..].chars().next().unwrap();
let mid_val = map.get(&format!("{}", middle_char)).unwrap();
let split_val = value.split("").collect::<Vec<_>>();
let new_key = format!("{}{}", middle_char, key);
let new_val = format!("{}{}{}", split_val[1], mid_val, split_val[2]);
text_to_convert = text_to_convert.replace(&new_key, &new_val);
}
}
}

for key in left_combinators {
if let Some(value) = map.get(&key) {
while let Some(index) = text_to_convert.find(&key) {
let right_char = text_to_convert[index - 3..].chars().next().unwrap();
let right_val = map.get(&format!("{}", right_char)).unwrap();
let new_key = format!("{}{}", right_char, key);
let new_val = format!("{}{}", value, right_val);
text_to_convert = text_to_convert.replace(&new_key, &new_val);
}
} else {
eprintln!("{:#?} not found in the map!", key);
}
}

for key in keys {
if let Some(value) = map.get(&key) {
text_to_convert = text_to_convert.replace(&key, value);
} else {
eprintln!("{:#?} not found in the map!", key);
}
}
Ok(text_to_convert)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::map_parser::{create_unicode_to_mltt_map, parse_content};

fn generate_map(content: &str) -> Result<HashMap<String, String>, Box<dyn Error>> {
let content = parse_content(&content)?;
create_unicode_to_mltt_map(&content)
}

#[test]
fn simple_convert() -> Result<(), Box<dyn Error>> {
assert_eq!(
convert_to_mltt("abc", &generate_map("a=a\nb=c\nc=d")?)?,
"abb"
);
Ok(())
}
}
84 changes: 12 additions & 72 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
mod convert;
mod map_parser;
mod utils;

use wasm_bindgen::prelude::*;

use std::collections::HashMap;
use std::error::Error;

use utils::set_panic_hook;
use crate::convert::convert_to_mltt;
use crate::map_parser::{create_unicode_to_mltt_map, parse_content};
use crate::utils::set_panic_hook;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
Expand Down Expand Up @@ -33,76 +36,13 @@ pub fn convert_text(text_to_convert: &str, map_content: &str) -> Result<String,
Ok(converted_text)
}

pub fn convert_to_mltt(
text_to_convert: &str,
map: &HashMap<String, String>,
) -> Result<String, Box<dyn Error>> {
let mut text_to_convert = text_to_convert.to_owned();
let mut keys = map.keys().map(String::from).collect::<Vec<_>>();
keys.sort_by(|a, b| b.len().cmp(&a.len()));
let to_string_vec = |x: Vec<&str>| x.into_iter().map(String::from).collect::<Vec<_>>();
let _right_combinators = to_string_vec(vec!["ാ", "ി", "ീ", "ു", "ൂ", "ൃ", "ൄ", "ൌ", "്"]);
let left_combinators = to_string_vec(vec!["െ", "േ", "ൈ"]);
let combinators = to_string_vec(vec!["ൊ", "ോ"]);
#[cfg(test)]
mod tests {
use super::*;

for key in combinators {
if let Some(value) = map.get(&key) {
while let Some(index) = text_to_convert.find(&key) {
let middle_char = text_to_convert[index - 3..].chars().next().unwrap();
let mid_val = map.get(&format!("{}", middle_char)).unwrap();
let split_val = value.split("").collect::<Vec<_>>();
let new_key = format!("{}{}", middle_char, key);
let new_val = format!("{}{}{}", split_val[1], mid_val, split_val[2]);
text_to_convert = text_to_convert.replace(&new_key, &new_val);
}
}
#[test]
fn simple_convert() -> Result<(), Box<dyn Error>> {
assert_eq!(convert_text("abc", "a=a\nb=c\nc=d")?, "abb");
Ok(())
}

for key in left_combinators {
if let Some(value) = map.get(&key) {
while let Some(index) = text_to_convert.find(&key) {
let right_char = text_to_convert[index - 3..].chars().next().unwrap();
let right_val = map.get(&format!("{}", right_char)).unwrap();
let new_key = format!("{}{}", right_char, key);
let new_val = format!("{}{}", value, right_val);
text_to_convert = text_to_convert.replace(&new_key, &new_val);
}
} else {
eprintln!("{:#?} not found in the map!", key);
}
}

for key in keys {
if let Some(value) = map.get(&key) {
text_to_convert = text_to_convert.replace(&key, value);
} else {
eprintln!("{:#?} not found in the map!", key);
}
}
Ok(text_to_convert)
}

pub fn parse_content(content: &str) -> Result<Vec<Vec<String>>, Box<dyn Error>> {
let content = content
.split("\n")
.filter(|x| x.len() >= 1 && x.chars().nth(0) != Some('#') && x.contains("="))
.map(|x| {
x.split("=")
.map(|s| s.trim())
.map(String::from)
.collect::<Vec<_>>()
})
.filter(|x| x[1].len() >= 1 && x[0].len() >= 1)
.collect::<Vec<_>>();
Ok(content)
}

pub fn create_unicode_to_mltt_map(
content: &Vec<Vec<String>>,
) -> Result<HashMap<String, String>, Box<dyn Error>> {
let map = content
.into_iter()
.map(|s| (s[1].to_owned(), s[0].to_owned()))
.collect::<HashMap<_, _>>();
Ok(map)
}
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ fn main() -> Result<(), Box<dyn Error>> {

Ok(())
}

75 changes: 75 additions & 0 deletions src/map_parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::collections::HashMap;
use std::error::Error;

pub fn parse_content(content: &str) -> Result<Vec<Vec<String>>, Box<dyn Error>> {
let content = content
.split("\n")
.filter(|x| x.len() >= 1 && x.chars().nth(0) != Some('#') && x.contains("="))
.map(|x| {
x.split("=")
.map(|s| s.trim())
.map(String::from)
.collect::<Vec<_>>()
})
.filter(|x| x[1].len() >= 1 && x[0].len() >= 1)
.collect::<Vec<_>>();
Ok(content)
}

pub fn create_unicode_to_mltt_map(
content: &Vec<Vec<String>>,
) -> Result<HashMap<String, String>, Box<dyn Error>> {
let map = content
.into_iter()
.map(|s| (s[1].to_owned(), s[0].to_owned()))
.collect::<HashMap<_, _>>();
Ok(map)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn simple_parse_content() -> Result<(), Box<dyn Error>> {
assert_eq!(parse_content("a=a")?, vec![vec!["a", "a"]]);
Ok(())
}

#[test]
fn complex_parse_content() -> Result<(), Box<dyn Error>> {
assert_eq!(
parse_content(
r#"# This is a comment string should not be parsed.
a=a
# b = b
b = c
c = d
a
b
c
."#
)?,
vec![vec!["a", "a"], vec!["b", "c"], vec!["c", "d"],]
);
Ok(())
}

fn get_parsed_content(dummy: Vec<Vec<&str>>) -> Vec<Vec<String>> {
dummy
.into_iter()
.map(|x| x.into_iter().map(String::from).collect::<Vec<_>>())
.collect::<Vec<_>>()
}

#[test]
fn simple_create_unicode_to_mltt_map() -> Result<(), Box<dyn Error>> {
let content = get_parsed_content(vec![vec!["a", "a"], vec!["b", "c"], vec!["c", "d"]]);
let mut expected_map: HashMap<String, String> = HashMap::new();
expected_map.insert(String::from("a"), String::from("a"));
expected_map.insert(String::from("c"), String::from("b"));
expected_map.insert(String::from("d"), String::from("c"));
assert_eq!(create_unicode_to_mltt_map(&content)?, expected_map);
Ok(())
}
}

0 comments on commit 33a875f

Please sign in to comment.