/
mod.rs
102 lines (88 loc) · 3.13 KB
/
mod.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::syntax::Language;
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;
use std::sync::Arc;
mod miktex;
mod texlive;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Error {
KpsewhichNotFound,
UnsupportedTexDistribution,
CorruptFileDatabase,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum TexDistributionKind {
Texlive,
Miktex,
}
pub static TEX_RESOLVER: Lazy<Result<Arc<TexResolver>>> =
Lazy::new(|| TexResolver::load().map(Arc::new));
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct TexResolver {
pub files_by_name: HashMap<String, PathBuf>,
}
impl TexResolver {
fn load() -> Result<Self> {
let root_directories = Self::find_root_directories()?;
let kind = Self::detect_distribution(&root_directories)?;
let reader = match kind {
TexDistributionKind::Texlive => texlive::read_database,
TexDistributionKind::Miktex => miktex::read_database,
};
let mut files_by_name = HashMap::new();
for directory in &root_directories {
let database = reader(directory)?
.into_iter()
.filter(|path| {
path.extension()
.and_then(OsStr::to_str)
.and_then(Language::by_extension)
.is_some()
})
.filter_map(|path| {
root_directories
.iter()
.rev()
.find_map(move |dir| dir.join(&path).canonicalize().ok())
})
.map(|path| (path.file_name().unwrap().to_str().unwrap().to_owned(), path));
files_by_name.extend(database);
}
Ok(Self { files_by_name })
}
fn find_root_directories() -> Result<Vec<PathBuf>> {
let texmf = Self::run_kpsewhich(&["-var-value", "TEXMF"])?;
let expanded = Self::run_kpsewhich(&[&format!("--expand-braces={}", texmf)])?;
let directories = env::split_paths(&expanded.replace("!", ""))
.filter(|x| x.exists())
.collect();
Ok(directories)
}
fn run_kpsewhich(args: &[&str]) -> Result<String> {
let output = Command::new("kpsewhich")
.args(args)
.output()
.map_err(|_| Error::KpsewhichNotFound)?;
Ok(String::from_utf8(output.stdout)
.expect("Could not decode output from kpsewhich")
.lines()
.next()
.expect("Invalid output from kpsewhich")
.to_owned())
}
fn detect_distribution(directories: &[PathBuf]) -> Result<TexDistributionKind> {
for directory in directories {
if directory.join(texlive::DATABASE_PATH).exists() {
return Ok(TexDistributionKind::Texlive);
} else if directory.join(miktex::DATABASE_PATH).exists() {
return Ok(TexDistributionKind::Miktex);
}
}
Err(Error::UnsupportedTexDistribution)
}
}