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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//! File locations
//!
//! See section 511 of the TeXBook.

use std::path;

use crate::traits::*;
use crate::*;

/// Representation of a file location in TeX
#[derive(PartialEq, Eq, Debug)]
pub struct FileLocation {
    pub path: String,
    pub extension: Option<String>,
    pub area: Option<String>,
}

impl<S: TexlangState> Parsable<S> for FileLocation {
    fn parse_impl(input: &mut vm::ExpandedStream<S>) -> Result<Self, Box<error::Error>> {
        let mut raw_string = String::new();
        let mut area_delimiter = None;
        let mut ext_delimiter = None;
        loop {
            let t = match input.peek()? {
                None => break,
                Some(t) => t,
            };
            if let token::Value::Space(_) = t.value() {
                let _ = input.consume();
                break;
            }
            let c = match t.char() {
                None => break,
                Some(c) => c,
            };
            let _ = input.consume();
            match c {
                '>' | ':' => {
                    area_delimiter = Some(raw_string.len() + 1);
                    ext_delimiter = None;
                }
                '.' => {
                    ext_delimiter = Some(raw_string.len());
                }
                _ => (),
            }
            raw_string.push(c);
        }

        Ok(FileLocation {
            path: raw_string
                [area_delimiter.unwrap_or(0)..ext_delimiter.unwrap_or(raw_string.len())]
                .into(),
            extension: ext_delimiter.map(|j| raw_string[j + 1..].into()),
            area: area_delimiter.map(|i| raw_string[..i].into()),
        })
    }
}

impl FileLocation {
    pub fn determine_full_path(
        &self,
        working_directory: Option<&path::Path>,
        default_extension: &str,
    ) -> path::PathBuf {
        let mut path: path::PathBuf = match self.area {
            None => match working_directory {
                None => Default::default(),
                Some(working_directory) => working_directory.into(),
            },
            Some(_) => {
                // TODO: support file areas.
                // Probably we just need to extend the vm::FileSystem trait to accept areas.
                // Then in production TeX engines, we provide a map of areas to base path for
                // that area. There is still an error case when an undefined area is referenced.
                panic!("Texlang does not have support for file areas yet");
            }
        };
        path.push(std::ffi::OsString::from(&self.path));
        path.set_extension(std::ffi::OsString::from(
            self.extension.as_deref().unwrap_or(default_extension),
        ));
        if !path.is_absolute() {
            panic!("TODO: handle this error (path is relative and no working directory set)");
        }
        path
    }
}

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

    parse_success_tests![
        (
            path_only,
            "path/to/file",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: None,
            },
        ),
        (
            path_only_newline,
            "path/to/file\n",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: None,
            },
        ),
        (
            path_only_control_sequence,
            r"path/to/file\relax more",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: None,
            },
        ),
        (
            path_only_trailing_word,
            "path/to/file something",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: None,
            },
        ),
        (
            extension_only,
            ".tex",
            FileLocation {
                path: "".to_string(),
                extension: Some("tex".to_string()),
                area: None,
            },
        ),
        (
            path_and_extension,
            "path/to/file.tex",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: Some("tex".to_string()),
                area: None,
            },
        ),
        (
            path_and_area_with_langle,
            "area>path/to/file",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: Some("area>".to_string()),
            },
        ),
        (
            path_and_area_with_colon,
            "area:path/to/file",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: None,
                area: Some("area:".to_string()),
            },
        ),
        (
            area_only,
            "area:",
            FileLocation {
                path: "".to_string(),
                extension: None,
                area: Some("area:".to_string()),
            },
        ),
        (
            path_and_extension_and_area_with_colon,
            "area:path/to/file.tex",
            FileLocation {
                path: "path/to/file".to_string(),
                extension: Some("tex".to_string()),
                area: Some("area:".to_string()),
            },
        ),
    ];
}