1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use std::io;

use crate::conf::Configuration;
use anyhow::{bail, Context, Result};
use fs_err::File;

pub fn get_module_name(conf: &Configuration) -> Result<String> {
    let mm = conf.cargo_section.include_dir.join("module.modulemap");
    let file = File::open(&mm)?;
    let content = io::read_to_string(&file)?;

    parse_module_name(&content).context(format!(
        "Failed to parse module name from modulemap file: {mm}"
    ))
}

fn parse_module_name(content: &str) -> Result<String> {
    let found_start = content.lines().find_map(|line| {
        line.strip_prefix("framework module ")
            .or_else(|| line.strip_prefix("module "))
    });

    let Some(found_start) = found_start else {
        bail!("No 'module' declaration found");
    };

    let mut module = found_start.trim_end();
    if module.ends_with('{') {
        module = module[..module.len() - 1].trim_end();
    } else {
        bail!("Expected `module <name> {{` not `{module}`");
    }
    Ok(module.to_string())
}