summaryrefslogtreecommitdiff
path: root/support/texlab/src/syntax/latex/analysis/distro_file.rs
blob: 4c747ce760b0201e4140bc636cd24518bf77dfae (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::{distro::Resolver, Uri};

pub fn resolve_distro_file(resolver: &Resolver, stem: &str, extensions: &[&str]) -> Option<Uri> {
    let mut document = resolver.files_by_name.get(stem);
    for extension in extensions {
        document = document.or_else(|| {
            let full_name = format!("{}.{}", stem, extension);
            resolver.files_by_name.get(full_name.as_str())
        });
    }
    document.and_then(|path| Uri::from_file_path(path).ok())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[cfg(windows)]
    fn test() {
        let mut resolver = Resolver::default();
        resolver
            .files_by_name
            .insert("foo.tex".into(), "C:/distro/foo.tex".into());
        resolver
            .files_by_name
            .insert("foo.sty".into(), "C:/distro/foo.sty".into());
        resolver
            .files_by_name
            .insert("bar.tex".into(), "C:/distro/bar.tex".into());

        assert_eq!(
            resolve_distro_file(&resolver, "foo", &["tex"]),
            Some(Uri::from_file_path("C:/distro/foo.tex").unwrap())
        );

        assert_eq!(
            resolve_distro_file(&resolver, "foo", &["sty"]),
            Some(Uri::from_file_path("C:/distro/foo.sty").unwrap())
        );

        assert_eq!(resolve_distro_file(&resolver, "foo", &["cls"]), None);
    }

    #[test]
    #[cfg(unix)]
    fn test() {
        let mut resolver = Resolver::default();
        resolver
            .files_by_name
            .insert("foo.tex".into(), "/distro/foo.tex".into());
        resolver
            .files_by_name
            .insert("foo.sty".into(), "/distro/foo.sty".into());
        resolver
            .files_by_name
            .insert("bar.tex".into(), "/distro/bar.tex".into());

        assert_eq!(
            resolve_distro_file(&resolver, "foo", &["tex"]),
            Some(Uri::from_file_path("/distro/foo.tex").unwrap())
        );

        assert_eq!(
            resolve_distro_file(&resolver, "foo", &["sty"]),
            Some(Uri::from_file_path("/distro/foo.sty").unwrap())
        );

        assert_eq!(resolve_distro_file(&resolver, "foo", &["cls"]), None);
    }
}