mindtape-eval 0.2.0

Typst evaluation and task extraction for MindTape
Documentation
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Mutex;

use chrono::Datelike;
use log::{debug, trace};
use typst::diag::{FileError, FileResult};
use typst::foundations::{Bytes, Datetime};
use typst::syntax::{FileId, Source, VirtualPath};
use typst::text::{Font, FontBook};
use typst::utils::LazyHash;
use typst::{Library, LibraryExt};

use crate::EvalError;

/// A minimal `World` implementation for evaluating `.typ` files.
///
/// This world resolves files from the local filesystem relative to a
/// project root directory. It caches loaded sources in memory and provides
/// an empty font book (eval-only, no layout/render).
///
/// It also tracks cross-file dependencies: whenever a file is loaded during
/// evaluation (via `source()` or `file()`), it records that dependency.
pub struct MindTapeWorld {
    /// Project root directory. All virtual paths resolve against this.
    root: PathBuf,
    /// The `FileId` of the main `.typ` file being evaluated.
    main_id: FileId,
    /// Standard Typst library.
    library: LazyHash<Library>,
    /// Empty font book (we never layout, so no fonts needed).
    book: LazyHash<FontBook>,
    /// Cache of loaded sources, keyed by `FileId`.
    sources: Mutex<HashMap<FileId, Source>>,
    /// Set of all file IDs accessed during evaluation (for dependency tracking).
    /// Excludes the main file itself.
    dependencies: Mutex<HashSet<FileId>>,
}

impl MindTapeWorld {
    /// Create a new world for evaluating the given `.typ` file.
    ///
    /// The file path may be relative (resolved against the current working
    /// directory). The project root is determined by walking up from the
    /// file's directory looking for project markers (`Cargo.toml`, `lib/`
    /// directory, or `.git`). If no marker is found, the file's parent
    /// directory is used as root.
    ///
    /// The main file's virtual path is computed relative to the discovered
    /// root so that relative imports (e.g. `../../lib/prelude.typ`) resolve
    /// correctly on disk.
    ///
    /// # Errors
    /// Returns `Err` if the file path cannot be resolved or canonicalized.
    pub fn new(file_path: &Path) -> Result<Self, EvalError> {
        let abs_path = if file_path.is_absolute() {
            file_path.to_path_buf()
        } else {
            std::env::current_dir()
                .map_err(|e| EvalError::World(format!("failed to get cwd: {e}")))?
                .join(file_path)
        };

        let abs_path = abs_path.canonicalize().map_err(|e| {
            EvalError::World(format!(
                "failed to canonicalize {}: {e}",
                file_path.display()
            ))
        })?;

        let file_dir = abs_path.parent().ok_or_else(|| {
            EvalError::World(format!(
                "file has no parent directory: {}",
                abs_path.display()
            ))
        })?;

        let root = find_project_root(file_dir);

        let rel_path = abs_path.strip_prefix(&root).map_err(|_| {
            EvalError::World(format!(
                "file {} is not under project root {}",
                abs_path.display(),
                root.display()
            ))
        })?;

        debug!("project root: {}", root.display());
        trace!("main file: {}", rel_path.display());

        let vpath = VirtualPath::new(rel_path);
        let main_id = FileId::new(None, vpath);

        Ok(Self {
            root,
            main_id,
            library: LazyHash::new(Library::default()),
            book: LazyHash::new(FontBook::new()),
            sources: Mutex::new(HashMap::new()),
            dependencies: Mutex::new(HashSet::new()),
        })
    }

    /// Resolve a `FileId` to an absolute filesystem path.
    ///
    /// For project-local files (`id.package()` is `None`), resolves against
    /// the project root. For `@mindtape` package files, resolves against
    /// `{root}/lib/` so that the package manifest and entry point map to
    /// `lib/typst.toml` and `lib/prelude.typ` respectively.
    ///
    /// For `@local/mindtape:VERSION` packages, resolves against
    /// `~/.local/share/typst/packages/local/mindtape/VERSION/` (following
    /// Typst's standard local package directory structure).
    fn resolve_path(&self, id: FileId) -> FileResult<PathBuf> {
        let root = match id.package() {
            Some(spec) if spec.namespace == "mindtape" => self.root.join("lib"),
            Some(spec) if spec.namespace == "local" && spec.name == "mindtape" => {
                // Resolve @local/mindtape:VERSION to ~/.local/share/typst/packages/local/mindtape/VERSION/
                let mut path = dirs::data_local_dir().ok_or_else(|| {
                    FileError::Other(Some("could not determine local data directory".into()))
                })?;
                path.push("typst");
                path.push("packages");
                path.push("local");
                path.push(spec.name.as_str());
                path.push(spec.version.to_string());
                path
            }
            Some(_) => return Err(FileError::NotFound(id.vpath().as_rooted_path().into())),
            None => self.root.clone(),
        };
        id.vpath()
            .resolve(&root)
            .ok_or_else(|| FileError::AccessDenied)
    }

    /// Read and cache a source file.
    fn load_source(&self, id: FileId) -> FileResult<Source> {
        let path = self.resolve_path(id)?;
        let text = std::fs::read_to_string(&path).map_err(|e| FileError::from_io(e, &path))?;
        Ok(Source::new(id, text))
    }

    /// Record a file as a dependency (if it's not the main file).
    fn record_dependency(&self, id: FileId) {
        if id != self.main_id {
            let mut deps = self
                .dependencies
                .lock()
                .expect("dependencies lock poisoned");
            deps.insert(id);
        }
    }

    /// Get all file dependencies discovered during evaluation.
    /// Returns relative paths (from project root) of files that were imported.
    ///
    /// # Errors
    /// Returns `Err` if a dependency's path cannot be resolved.
    ///
    /// # Panics
    /// Panics if the dependencies lock is poisoned (should never happen in normal operation).
    pub fn get_dependencies(&self) -> Result<Vec<PathBuf>, EvalError> {
        let deps = self
            .dependencies
            .lock()
            .expect("dependencies lock poisoned");
        let mut paths = Vec::new();
        for id in deps.iter() {
            let abs_path = self
                .resolve_path(*id)
                .map_err(|e| EvalError::World(format!("failed to resolve dependency: {e}")))?;

            // Skip external dependencies (e.g. @local/mindtape packages) —
            // only track project-local files that might change.
            if let Ok(rel_path) = abs_path.strip_prefix(&self.root) {
                paths.push(rel_path.to_path_buf());
            } else {
                trace!("skipping external dependency: {}", abs_path.display());
            }
        }
        paths.sort();
        Ok(paths)
    }
}

impl typst::World for MindTapeWorld {
    fn library(&self) -> &LazyHash<Library> {
        &self.library
    }

    fn book(&self) -> &LazyHash<FontBook> {
        &self.book
    }

    fn main(&self) -> FileId {
        self.main_id
    }

    fn source(&self, id: FileId) -> FileResult<Source> {
        // Record this file as a dependency (unless it's the main file).
        self.record_dependency(id);

        {
            let cache = self.sources.lock().expect("source cache poisoned");
            if let Some(source) = cache.get(&id) {
                trace!(
                    "source cache hit: {}",
                    id.vpath().as_rooted_path().display()
                );
                return Ok(source.clone());
            }
        }

        trace!("loading source: {}", id.vpath().as_rooted_path().display());
        let source = self.load_source(id)?;

        let mut cache = self.sources.lock().expect("source cache poisoned");
        cache.insert(id, source.clone());
        Ok(source)
    }

    fn file(&self, id: FileId) -> FileResult<Bytes> {
        // Record this file as a dependency (unless it's the main file).
        self.record_dependency(id);

        let path = self.resolve_path(id)?;
        let data = std::fs::read(&path).map_err(|e| FileError::from_io(e, &path))?;
        Ok(Bytes::new(data))
    }

    fn font(&self, _index: usize) -> Option<Font> {
        None
    }

    fn today(&self, _offset: Option<i64>) -> Option<Datetime> {
        let now = chrono::Local::now();
        #[allow(clippy::cast_possible_truncation)]
        {
            Datetime::from_ymd(now.year(), now.month() as u8, now.day() as u8)
        }
    }
}

/// Walk up from `start_dir` looking for project root markers.
///
/// Recognized markers (checked in order):
/// - `Cargo.toml` file
/// - `lib/` directory
/// - `.git` directory or file
///
/// Returns the first ancestor directory containing a marker, or `start_dir`
/// itself if no marker is found.
pub fn find_project_root(start_dir: &Path) -> PathBuf {
    let mut dir = start_dir.to_path_buf();
    loop {
        if dir.join("Cargo.toml").exists() {
            return dir;
        }
        if dir.join("lib").is_dir() {
            return dir;
        }
        if dir.join(".git").exists() {
            return dir;
        }
        if !dir.pop() {
            // Reached filesystem root without finding a marker.
            return start_dir.to_path_buf();
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::indexing_slicing)]
mod tests {
    use super::*;
    use typst::World;

    // --- find_project_root ---

    #[test]
    fn find_project_root_cargo_toml() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        assert_eq!(find_project_root(dir.path()), dir.path());
    }

    #[test]
    fn find_project_root_git() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        assert_eq!(find_project_root(dir.path()), dir.path());
    }

    #[test]
    fn find_project_root_lib_dir() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::create_dir(dir.path().join("lib")).unwrap();
        assert_eq!(find_project_root(dir.path()), dir.path());
    }

    #[test]
    fn find_project_root_walks_up() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let child = dir.path().join("sub").join("deep");
        std::fs::create_dir_all(&child).unwrap();
        assert_eq!(find_project_root(&child), dir.path());
    }

    #[test]
    fn find_project_root_no_marker_returns_nearest_ancestor_with_marker() {
        // On most systems, the walk will eventually find some marker (e.g. /lib).
        // What matters is that it never panics and returns a valid directory.
        let dir = tempfile::tempdir().unwrap();
        let child = dir.path().join("empty");
        std::fs::create_dir_all(&child).unwrap();
        let root = find_project_root(&child);
        assert!(root.is_dir());
    }

    #[test]
    fn find_project_root_cargo_toml_has_priority_over_git() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        std::fs::create_dir(dir.path().join(".git")).unwrap();
        // Cargo.toml is checked first, so it should match this dir
        assert_eq!(find_project_root(dir.path()), dir.path());
    }

    // --- MindTapeWorld ---

    #[test]
    fn world_new_with_valid_file() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "= Hello").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        // Main file ID should be set
        let _main = world.main();
        // Should be able to load the source
        let source = world.source(world.main()).unwrap();
        assert!(source.text().contains("Hello"));
    }

    #[test]
    fn world_new_nonexistent_file() {
        let result = MindTapeWorld::new(Path::new("/nonexistent/path/test.typ"));
        assert!(result.is_err());
    }

    #[test]
    fn world_source_caching() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "= Cached").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        let main_id = world.main();

        // First call loads from disk
        let s1 = world.source(main_id).unwrap();
        // Second call should return cached version
        let s2 = world.source(main_id).unwrap();
        assert_eq!(s1.text(), s2.text());
    }

    #[test]
    fn world_new_with_absolute_path() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "= Abs").unwrap();

        // Pass the already-absolute path
        let abs = typ_file.canonicalize().unwrap();
        let world = MindTapeWorld::new(&abs).unwrap();
        let source = world.source(world.main()).unwrap();
        assert!(source.text().contains("Abs"));
    }

    #[test]
    fn world_file_reads_bytes() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "= Bytes Test").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        let bytes = world.file(world.main()).unwrap();
        assert!(
            std::str::from_utf8(bytes.as_slice())
                .unwrap()
                .contains("Bytes Test")
        );
    }

    #[test]
    fn world_font_returns_none() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        assert!(world.font(0).is_none());
    }

    #[test]
    fn world_today_returns_valid_date() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        let today = world.today(None);
        assert!(today.is_some());
        let dt = today.unwrap();
        assert!(dt.year().unwrap() >= 2024);
    }

    #[test]
    fn world_resolve_unknown_package_fails() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        // Try to import from an unknown package — should fail during eval
        std::fs::write(&typ_file, r#"#import "@unknown/pkg:1.0.0": foo"#).unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        let result = crate::eval_file(&world);
        assert!(result.is_err());
    }

    #[test]
    fn world_source_nonexistent_file_errors() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("Cargo.toml"), "").unwrap();
        let typ_file = dir.path().join("test.typ");
        std::fs::write(&typ_file, "").unwrap();

        let world = MindTapeWorld::new(&typ_file).unwrap();
        // Create a FileId for a file that doesn't exist on disk
        let missing_id = FileId::new(None, VirtualPath::new("nonexistent.typ"));
        assert!(world.source(missing_id).is_err());
    }
}