char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somunify public module registration and declarations6534c3e

main
1.5 KiB53 linesraw
1use avril_core::{Error, PublicModule, Runtime};
2
3const MODULES: &[&[PublicModule]] = &[
4    avril_ext_base::PUBLIC_MODULES,
5    avril_ext_file::PUBLIC_MODULES,
6    avril_ext_fjson::PUBLIC_MODULES,
7    avril_ext_kv::PUBLIC_MODULES,
8    avril_ext_task::PUBLIC_MODULES,
9    avril_ext_path::PUBLIC_MODULES,
10    avril_ffi::PUBLIC_MODULES,
11    avril_ext_test::PUBLIC_MODULES,
12];
13
14fn public_modules() -> impl Iterator<Item = &'static PublicModule> {
15    MODULES.iter().flat_map(|modules| modules.iter())
16}
17
18pub(crate) fn register(runtime: &mut Runtime) -> Result<(), Error> {
19    public_modules().try_for_each(|module| module.register(runtime))
20}
21
22pub fn declarations() -> impl Iterator<Item = (String, &'static str)> {
23    public_modules()
24        .map(|module| {
25            let name = module
26                .name
27                .strip_prefix("avril:")
28                .expect("Public modules use the avril: scheme");
29            (format!("{name}.d.ts"), module.declaration)
30        })
31        .chain(std::iter::once((
32            "globals.d.ts".into(),
33            include_str!("../stdlib/globals.d.ts"),
34        )))
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40
41    #[test]
42    fn public_module_names_are_unique_and_use_the_avril_scheme() {
43        let mut names = std::collections::HashSet::new();
44        for module in public_modules() {
45            assert!(module.name.starts_with("avril:"), "{}", module.name);
46            assert!(
47                names.insert(module.name),
48                "Duplicate module: {}",
49                module.name
50            );
51        }
52    }
53}