Advance native LaTeX execution and preview
This commit is contained in:
1 parent
91f9d76b16
commit
2741546ca8
414 files changed
+99872
-933
No files matched your search
@@ -0,0 +1,117 @@
|
||||
//! Project-owned generated files. Ownership is established by actual writes,
|
||||
//! independent of file names/extensions; content versions remain real inputs.
|
||||
use crate::resources::ResourceVersion;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{
|
||||
collections::BTreeSet,
|
||||
fs::File,
|
||||
io::{self, Read},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct GeneratedArtifact {
|
||||
pub producer: PathBuf,
|
||||
/// Last recorded output version, not a promise that external tools haven't edited it.
|
||||
pub version: ResourceVersion,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ArtifactLedger {
|
||||
schema: u32,
|
||||
root: PathBuf,
|
||||
records: Vec<GeneratedArtifact>,
|
||||
}
|
||||
|
||||
impl ArtifactLedger {
|
||||
pub fn storage_key(root: &Path) -> Result<String, String> {
|
||||
if !root.is_absolute() {
|
||||
return Err("artifact project root must be absolute".into());
|
||||
}
|
||||
Ok(format!(
|
||||
"{:x}",
|
||||
Sha256::digest(serde_json::to_vec(root).map_err(|e| e.to_string())?)
|
||||
))
|
||||
}
|
||||
pub fn new(root: PathBuf) -> Self {
|
||||
Self {
|
||||
schema: 1,
|
||||
root,
|
||||
records: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn decode(bytes: &[u8], root: &Path) -> Result<Self, String> {
|
||||
let ledger: Self = serde_json::from_slice(bytes)
|
||||
.map_err(|e| format!("invalid generated-file ledger: {e}"))?;
|
||||
ledger.validate(root)?;
|
||||
Ok(ledger)
|
||||
}
|
||||
fn validate(&self, root: &Path) -> Result<(), String> {
|
||||
let mut paths = BTreeSet::new();
|
||||
if self.schema != 1
|
||||
|| self.root != root
|
||||
|| !self.root.is_absolute()
|
||||
|| self.records.iter().any(|r| {
|
||||
!r.producer.is_absolute()
|
||||
|| !r.version.path.is_absolute()
|
||||
|| !paths.insert(&r.version.path)
|
||||
|| r.version.sha256.len() != 64
|
||||
|| !r.version.sha256.bytes().all(|c| c.is_ascii_hexdigit())
|
||||
})
|
||||
{
|
||||
return Err("incompatible generated-file ledger identity or versions".into());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
pub fn encode(&self) -> Result<Vec<u8>, String> {
|
||||
self.validate(&self.root)?;
|
||||
serde_json::to_vec_pretty(self).map_err(|e| e.to_string())
|
||||
}
|
||||
pub fn owns(&self, path: &Path) -> bool {
|
||||
self.records.iter().any(|r| r.version.path == path)
|
||||
}
|
||||
pub fn paths(&self) -> impl Iterator<Item = &Path> {
|
||||
self.records.iter().map(|r| r.version.path.as_path())
|
||||
}
|
||||
pub fn record(&mut self, producer: &Path, version: ResourceVersion) {
|
||||
self.forget(&version.path);
|
||||
self.records.push(GeneratedArtifact {
|
||||
producer: producer.to_owned(),
|
||||
version,
|
||||
});
|
||||
self.records
|
||||
.sort_by(|a, b| a.version.path.cmp(&b.version.path));
|
||||
}
|
||||
pub fn forget(&mut self, path: &Path) {
|
||||
self.records.retain(|r| r.version.path != path);
|
||||
}
|
||||
}
|
||||
|
||||
/// Policy/version inspection, separate from TeX's semantic input and stream reads.
|
||||
pub fn file_version(path: &Path) -> Result<Option<ResourceVersion>, String> {
|
||||
let mut file = match File::open(path) {
|
||||
Ok(file) => file,
|
||||
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(None),
|
||||
Err(e) => return Err(format!("generated-file version {}: {e}", path.display())),
|
||||
};
|
||||
let mut hash = Sha256::new();
|
||||
let mut buffer = [0; 16384];
|
||||
let mut bytes = 0;
|
||||
loop {
|
||||
let count = file
|
||||
.read(&mut buffer)
|
||||
.map_err(|e| format!("generated-file version {}: {e}", path.display()))?;
|
||||
if count == 0 {
|
||||
break;
|
||||
}
|
||||
hash.update(&buffer[..count]);
|
||||
bytes += count as u64;
|
||||
}
|
||||
Ok(Some(ResourceVersion {
|
||||
path: path.to_owned(),
|
||||
sha256: format!("{:x}", hash.finalize()),
|
||||
bytes,
|
||||
}))
|
||||
}
|
||||
Reference in new issue
Block a user