mirror of
https://github.com/typst/typst
synced 2025-05-17 02:25:27 +08:00
Simplify source file loading logic
This commit is contained in:
parent
5594868f8b
commit
72eb243e26
@ -63,7 +63,7 @@ impl SourceStore {
|
|||||||
io::Error::new(io::ErrorKind::InvalidData, "file is not valid utf-8")
|
io::Error::new(io::ErrorKind::InvalidData, "file is not valid utf-8")
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(self.insert(path, src, Some(hash)))
|
Ok(self.provide(path, src))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Directly provide a source file.
|
/// Directly provide a source file.
|
||||||
@ -75,28 +75,23 @@ impl SourceStore {
|
|||||||
/// If the path is resolvable and points to an existing source file, it is
|
/// If the path is resolvable and points to an existing source file, it is
|
||||||
/// overwritten.
|
/// overwritten.
|
||||||
pub fn provide(&mut self, path: &Path, src: String) -> SourceId {
|
pub fn provide(&mut self, path: &Path, src: String) -> SourceId {
|
||||||
if let Ok(hash) = self.loader.resolve(path) {
|
let hash = self.loader.resolve(path).ok();
|
||||||
if let Some(&id) = self.files.get(&hash) {
|
|
||||||
// Already loaded, so we replace it.
|
|
||||||
self.sources[id.0 as usize] = SourceFile::new(id, path, src);
|
|
||||||
id
|
|
||||||
} else {
|
|
||||||
// Not loaded yet.
|
|
||||||
self.insert(path, src, Some(hash))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Not known to the loader.
|
|
||||||
self.insert(path, src, None)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Insert a new source file.
|
// Check for existing file and replace if one exists.
|
||||||
fn insert(&mut self, path: &Path, src: String, hash: Option<FileHash>) -> SourceId {
|
if let Some(&id) = hash.and_then(|hash| self.files.get(&hash)) {
|
||||||
|
self.sources[id.0 as usize] = SourceFile::new(id, path, src);
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No existing file yet.
|
||||||
let id = SourceId(self.sources.len() as u32);
|
let id = SourceId(self.sources.len() as u32);
|
||||||
|
self.sources.push(SourceFile::new(id, path, src));
|
||||||
|
|
||||||
|
// Register in file map if the path was known to the loader.
|
||||||
if let Some(hash) = hash {
|
if let Some(hash) = hash {
|
||||||
self.files.insert(hash, id);
|
self.files.insert(hash, id);
|
||||||
}
|
}
|
||||||
self.sources.push(SourceFile::new(id, path, src));
|
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user