-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
178 additions
and
83 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
use std::collections::HashMap; | ||
|
||
pub fn parse_json(json: &str) -> Option<HashMap<String, String>> { | ||
let mut map = HashMap::new(); | ||
|
||
// check if json have more than two properties | ||
// return None if it does | ||
|
||
let (first_key_value, second_key_value) = json.split_at(json.find(',').unwrap()); | ||
let (first_key, first_value) = first_key_value.split_at(first_key_value.find(':').unwrap()); | ||
let (second_key, second_value) = second_key_value.split_at(second_key_value.find(':').unwrap()); | ||
let first_key = | ||
first_key.chars().filter(|c| c.is_alphabetic()).collect::<String>().trim().to_string(); | ||
let first_value = first_value | ||
.chars() | ||
.filter(|c| c.is_alphabetic() || c == &'-' || c == &' ' || c == &'.') | ||
.collect::<String>() | ||
.trim() | ||
.to_string(); | ||
let second_key = | ||
second_key.chars().filter(|c| c.is_alphabetic()).collect::<String>().trim().to_string(); | ||
let second_value = second_value | ||
.chars() | ||
.filter(|c| c.is_alphabetic() || c == &'-' || c == &' ' || c == &'.') | ||
.collect::<String>() | ||
.trim() | ||
.to_string(); | ||
|
||
map.insert(second_key, second_value); | ||
map.insert(first_key, first_value); | ||
Some(map) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parse_json() { | ||
let json = r"errorCode: 'unsupported-version', message: 'This version of payjoin is not supported.'"; | ||
let map = parse_json(json).unwrap(); | ||
dbg!(&map); | ||
assert_eq!(map.get("errorCode"), Some(&"unsupported-version".to_string())); | ||
assert_eq!( | ||
map.get("message"), | ||
Some(&"This version of payjoin is not supported.".to_string()) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.