use super::dialect::{Dialect, Import};
use super::prose::Prose;
pub(super) struct Zig;
const PROSE: Prose = Prose {
line_comment: "//",
block_comment: None,
line_string: Some("\\\\"),
quotes: b"\"'",
triple_quotes: &[],
line_join: false,
};
impl Dialect for Zig {
fn name(&self) -> &'static str {
"zig"
}
fn extensions(&self) -> &'static [&'static str] {
&["zig"]
}
fn manifests(&self) -> &'static [&'static str] {
&["build.zig", "build.zig.zon"]
}
fn vendored(&self, manifest: &str) -> Vec<String> {
let code = PROSE.code_only(manifest);
let mut out = Vec::new();
let mut i = 0;
while let Some(k) = find(&code[i..], b".path") {
let head = i + k;
i = head + b".path".len();
let mut j = i;
while matches!(code.get(j), Some(b' ' | b'\t')) {
j += 1;
}
if code.get(j) != Some(&b'=') {
continue;
}
if let Some(dir) = literal(manifest.as_bytes(), j + 1) {
out.push(dir.trim_end_matches('/').to_owned());
}
}
out
}
fn declared(&self, manifest: &str) -> Option<String> {
let code = PROSE.code_only(manifest);
let head = find(&code, b".name")?;
let mut j = head + b".name".len();
while matches!(code.get(j), Some(b' ' | b'\t')) {
j += 1;
}
if code.get(j) != Some(&b'=') {
return None;
}
j += 1;
if let Some(quoted) = literal(manifest.as_bytes(), j) {
return Some(quoted);
}
let raw = manifest.as_bytes();
while raw.get(j).is_some_and(u8::is_ascii_whitespace) {
j += 1;
}
if raw.get(j) != Some(&b'.') {
return None;
}
let word = &manifest[j + 1..];
let end = word.find(|c: char| !c.is_alphanumeric() && c != '_').unwrap_or(word.len());
(end > 0).then(|| word[..end].to_owned())
}
fn ambient(&self) -> &'static [&'static str] {
&["std", "builtin", "root"]
}
fn prose(&self) -> &Prose {
&PROSE
}
fn imports(
&self,
_path: &str,
_roots: &[&str],
_own: &str,
source: &str,
code: &[u8],
) -> Vec<Import> {
let raw = source.as_bytes();
let mut out = Vec::new();
let mut i = 0;
while let Some(k) = find(&code[i..], b"@import") {
let head = i + k;
let mut j = head + b"@import".len();
while matches!(code.get(j), Some(b' ' | b'\t')) {
j += 1;
}
i = head + 1;
if code.get(j) != Some(&b'(') {
continue;
}
if let Some(spec) = literal(raw, j + 1) {
out.push(Import { offset: head, spec });
i = j;
}
}
out
}
#[allow(clippy::case_sensitive_file_extension_comparisons, reason = "import specs are literal")]
fn is_local(&self, spec: &str) -> bool {
spec.ends_with(".zig")
}
fn escape_remedy(&self) -> &'static str {
"Zig cannot follow a path across a module boundary; declare the dependency \
as a named module in build.zig"
}
}
fn find(haystack: &[u8], needle: &[u8]) -> Option<usize> {
haystack.windows(needle.len()).position(|w| w == needle)
}
fn literal(raw: &[u8], from: usize) -> Option<String> {
let mut i = from;
while raw.get(i).is_some_and(u8::is_ascii_whitespace) {
i += 1;
}
if raw.get(i) != Some(&b'"') {
return None;
}
i += 1;
let mut out = Vec::new();
while let Some(&ch) = raw.get(i) {
match ch {
b'\\' if i + 1 < raw.len() => {
out.push(raw[i + 1]);
i += 2;
}
b'"' => return String::from_utf8(out).ok(),
b'\n' => return None,
_ => {
out.push(ch);
i += 1;
}
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
fn specs(source: &str) -> Vec<String> {
let code = PROSE.code_only(source);
Zig.imports("test.zig", &[], "", source, &code).into_iter().map(|i| i.spec).collect()
}
#[test]
fn reads_real_imports_and_ignores_quoted_ones() {
let src = "\
const std = @import(\"std\");
const a = @import(\"../a/b.zig\");
// const c = @import(\"comment.zig\");
const doc =
\\\\@import(\"prose.zig\")
;
const d = @import( \"spaced.zig\" );
const e = @import(comptime_value);
";
assert_eq!(specs(src), ["std", "../a/b.zig", "spaced.zig"]);
}
#[test]
fn reads_in_tree_dependencies_and_not_the_publish_paths() {
let zon = "\
.{
.name = .acme,
.dependencies = .{
// .vendor = .{ .path = \"commented\" },
.vendor = .{ .path = \"vendor\" },
.fetched = .{ .url = \"https://example/x.tar.gz\", .hash = \"…\" },
},
.paths = .{ \"build.zig\", \"vendor\", \"README.md\" },
}
";
assert_eq!(Zig.vendored(zon), ["vendor"]);
}
#[test]
fn reads_the_declared_name_in_either_spelling() {
assert_eq!(
Zig.declared(".{ .name = .acme, .version = \"0.1.0\" }").as_deref(),
Some("acme")
);
assert_eq!(Zig.declared(".{ .name = \"acme\" }").as_deref(), Some("acme"));
let zon = "\
.{
// .name = .wrong,
.version = \"0.1.0\",
.name = .right,
}
";
assert_eq!(Zig.declared(zon).as_deref(), Some("right"));
assert_eq!(Zig.declared(".{ .version = \"0.1.0\" }"), None);
}
#[test]
fn a_named_module_is_not_a_path() {
assert!(Zig.is_local("a/b.zig"));
assert!(!Zig.is_local("std"));
assert!(!Zig.is_local("acme"));
}
}