pub mod lexer;
mod syntax;
pub use syntax::{AliasInvocation, AliasSyntaxError, Masked, mask};
use std::{
borrow::Cow,
fs::File,
io::{self, Read},
path::Path,
};
pub fn definition(bytes: &[u8]) -> Cow<'_, [u8]> {
let Ok(source) = std::str::from_utf8(bytes) else {
return Cow::Borrowed(bytes);
};
let Ok(lexed) = lexer::lex(source) else {
return Cow::Borrowed(bytes);
};
if lexed.comments.is_empty() {
return Cow::Borrowed(bytes);
}
let mut text = String::with_capacity(source.len());
let mut cursor = 0;
for comment in &lexed.comments {
text.push_str(source[cursor..comment.start].trim_end_matches([' ', '\t']));
cursor = comment.end;
}
text.push_str(&source[cursor..]);
Cow::Owned(text.into_bytes())
}
pub fn fingerprinted(bytes: &[u8]) -> Cow<'_, [u8]> {
let Ok(source) = std::str::from_utf8(bytes) else {
return Cow::Borrowed(bytes);
};
let Ok(lexed) = lexer::lex(source) else {
return Cow::Borrowed(bytes);
};
let tokens: Vec<&str> = lexed
.tokens
.iter()
.map(|token| &source[token.span.clone()])
.collect();
Cow::Owned(tokens.join(" ").into_bytes())
}
const DOCUMENT_BYTES: u64 = 8 * 1024 * 1024;
const OVERFLOW_PROBE_BYTES: u64 = 1;
pub(crate) fn reader(file: File, path: &Path) -> io::Result<Box<dyn Read>> {
if !path.extension().is_some_and(|ext| ext == "artf")
&& !path.file_name().is_some_and(|name| name == ".artf")
{
return Ok(Box::new(file));
}
let mut bytes = Vec::new();
file.take(DOCUMENT_BYTES + OVERFLOW_PROBE_BYTES)
.read_to_end(&mut bytes)?;
if bytes.len() as u64 > DOCUMENT_BYTES {
return Err(io::Error::other(
"Artifact definition exceeds the 8 MiB file-tool bound.",
));
}
Ok(Box::new(io::Cursor::new(definition(&bytes).into_owned())))
}