pub mod locales;
mod parsers;
mod url;
use locales::nwt_en::Site;
use locales::BibleError;
pub use parsers::surround::{ScriptSlice, ScriptureCollection, Locations};
#[allow(non_camel_case_types)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Locale {
en_us,
es_es,
}
pub fn surround<'a, S: Into<String> + Clone>(
text: S,
prefix: &'a str,
postfix: &'a str,
) -> Result<String, BibleError> {
Ok(parsers::surround::Script::new(text)
.prefix(prefix)
.postfix(postfix)
.surround()
.get_text())
}
pub fn url<S: Into<String> + Clone>(site: &Site, text: S) -> Result<String, BibleError> {
Ok(parsers::surround::Script::new(text)
.url(site)?
.get_text())
}
pub fn get_scriptures<S: Into<String> + Clone>(string: S) -> Result<ScriptureCollection, BibleError> {
parsers::surround::Script::new(string).get_scriptures()
}
pub fn get_locations<'a, S: Into<String> + Clone>(string: S) -> Locations {
parsers::surround::Script::new(string).get_locations()
}
#[cfg(test)]
mod lib_test {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn t_single_url() {
let input: &str = "A popular scriptures is Re 12:12. It is quoted often.";
let expect: String = "A popular scriptures is [Re 12:12](https://www.jw.org/en/library/bible/study-bible/books/revelation/12/#v66012012). It is quoted often.".to_string();
let got: String = url(&Site::JwOrg, input).unwrap();
assert_eq!(got, expect)
}
#[test]
fn t_single_ranged_url() {
let input: &str = "A popular scriptures is Job 36:26-28. It is quoted often.";
let expect: String = "A popular scriptures is [Job 36:26-28](https://www.jw.org/en/library/bible/study-bible/books/job/36/#v18036026-v18036028). It is quoted often.".to_string();
let got: String = url(&Site::JwOrg, input).unwrap();
assert_eq!(got, expect)
}
#[test]
fn t_multipal_url() {
let input: &str = "Three well-known Bible scriptures are Proverbs 3:5, John 3:16, and Romans 8:28";
let expect: String = "Three well-known Bible scriptures are [Proverbs 3:5](https://www.jw.org/en/library/bible/study-bible/books/proverbs/3/#v20003005), [John 3:16](https://www.jw.org/en/library/bible/study-bible/books/john/3/#v43003016), and [Romans 8:28](https://www.jw.org/en/library/bible/study-bible/books/romans/8/#v45008028)".to_string();
let got: String = url(&Site::JwOrg, input).unwrap();
assert_eq!(got, expect)
}
#[test]
fn t_add_element_prefix_single() {
let input: &str = "Another popular scripture is John 3:16, it's quoted often.";
let expect: &str = "Another popular scripture is **John 3:16]], it's quoted often.";
let got = surround(input, "**", "]]").unwrap();
assert_eq!(got, expect);
}
#[test]
fn t_add_element_prefix_single_to_string() {
let input: String = "Another popular scripture is John 3:16, it's quoted often.".into();
let expect: &str = "Another popular scripture is **John 3:16]], it's quoted often.";
let got = surround(input, "**", "]]").unwrap();
assert_eq!(got, expect);
}
#[test]
fn t_add_element_prefix_multi() {
let input:&str = "Other popular scriptures include John 3:16, Matthew 24:14, and Psalm 83:18, they are quoted often.";
let expect:&str = "Other popular scriptures include **John 3:16]], **Matthew 24:14]], and **Psalm 83:18]], they are quoted often.";
let got = surround(input, "**", "]]").unwrap();
assert_eq!(got, expect);
}
#[test]
fn t_add_element_prefix_ranged_multi() {
let input:&str = "Other popular scriptures include John 3:16, 17, Matthew 24:14-16, and Psalm 83:18, 17-20, they are quoted often.";
let expect:&str = "Other popular scriptures include **John 3:16, 17]], **Matthew 24:14-16]], and **Psalm 83:18, 17-20]], they are quoted often.";
let got = surround(input, "**", "]]").unwrap();
assert_eq!(got, expect);
}
#[test]
fn t_add_element_prefix_multi_no_scriptures() {
let input: &str = "There are no scriptures in this line.";
let expect: &str = "There are no scriptures in this line.";
let got = surround(input, "**", "]]").unwrap();
assert_eq!(got, expect);
}
#[test]
fn t_url_invalid_book() {
let input: &str = "A popular scriptures is Mary 12:12. It is quoted often.";
let result= url(&Site::JwOrg, input);
assert!(result.is_err());
assert_eq!(
result.unwrap_err(),
BibleError::BookNotFound("Mary".to_string())
);
}
#[test]
fn t_url_no_scripture() {
let input: &str = "This is not a scripture.";
let result = url(&Site::JwOrg, input);
assert!(result.is_ok());
}
#[test]
fn t_get_multiple_scriptures() {
let input: &str = "A popular scripture is John 3:16. Another is Matthew 24:14.";
let result = get_scriptures(input).unwrap();
assert_eq!(result, vec!["John 3:16", "Matthew 24:14"]);
}
}