Skip to main content

lectio_wasm/
wasm.rs

1pub mod config;
2pub mod generate_liturgy;
3use generate_liturgy::{LiturgyGenerator, LiturgicalSeason, search_bible, Readings};
4use wasm_bindgen::prelude::*;
5use std::collections::HashMap;
6use serde::{Deserialize, Serialize};
7use chrono::{NaiveDate};
8
9// Built in bible translations
10// static NABRE: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/bibles/NABRE.txt"));
11// static RSVCE: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/bibles/RSVCE.txt"));
12// static DRA: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/bibles/DRA.txt"));
13// static HWP: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/bibles/HWP.txt"));
14
15#[derive(Deserialize, Serialize, Debug, Clone)]
16struct LiturgyAndSeason {
17    liturgy: Vec<Readings>,
18    season: HashMap<String, LiturgicalSeason>,
19}
20
21#[wasm_bindgen]
22pub fn wasm_generate_liturgy(date: &str, bible: &str) -> String {
23    // let bible = match bible {
24    //     "NABRE" => NABRE,
25    //     "RSVCE" => RSVCE,
26    //     "DRA" => DRA,
27    //     "HWP" => HWP,
28    //     _ => "",
29    // };
30    let year: i32 = date.split("-").next().unwrap().parse().unwrap();
31    let generator = match LiturgyGenerator::new(year) {
32        Ok(content) => content,
33        Err(e) => {println!("Liturgy Init error: {e}"); return "".to_string();},
34    };
35    let (liturgy, season) = match generator.generate() {
36        Ok(content) => content,
37        Err(e) => {println!("Generator error: {e}"); return "".to_string();}
38    };
39
40    let mut search_liturgy = liturgy[date].clone();
41    search_bible(bible, &mut search_liturgy);
42
43    let liturgy_and_season = LiturgyAndSeason {
44        liturgy: search_liturgy,
45        season: season,
46    };
47
48    let serialized_liturgy = match serde_json::to_string_pretty(&liturgy_and_season) {
49        Ok(liturgy) => liturgy,
50        Err(e) => return "".to_string(),
51    };
52    println!("{}", serialized_liturgy);
53    return serialized_liturgy;
54}
55
56#[cfg(test)]
57mod wasm_tests {
58    use super::*;
59
60    #[test]
61    fn test_generate_liturgy() {
62        // let NABRE: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/data/bibles/NABRE.txt"));
63        // wasm_generate_liturgy("2025-06-29", NABRE);
64    }
65
66}