oxedyne/fe2o3/fe2o3_austenite/src/eval/package.rs
27.5 KiB, 3 runs
created by r1870400018:63506, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Original work copyright (c) the Typst project authors (typst-syntax `package.rs` and typst-eval `import.rs`, version 0.15.1). |
| 3 | // Modified for Austenite: ported to Hematite's types, vfs and error handling. |
| 4 | // U10 owns this file. A package is supplied by the host and never fetched: in Daimond its origin serves |
| 5 | // the package corpus, mirrored ahead of use, and the engine reads what the host hands it. An archive's |
| 6 | // SHA-256 is the host's to check, since the engine receives files and never the archive; what the engine |
| 7 | // holds to is that a version, once supplied, does not change. The host hands packages over in one of two |
| 8 | // ways: as files in memory, through `supply` (the wasm surface does this), or as directories on disc laid |
| 9 | // out `<dir>/<namespace>/<name>/<version>/`, through `add_dir` (a native host's package path and cache). |
| 10 | // Either way each package appears in the vfs under its own root, `@<namespace>/<name>:<version>`, a |
| 11 | // relative path that no project path can reach, so a package's files and a project's never mix and a |
| 12 | // diagnostic inside a package names its file as Typst does, `@preview/cetz:0.3.4/src/lib.typ:1:8`. |
| 13 | // |
| 14 | // Semantics and wording are Typst 0.15.1's: `typst-syntax/src/package.rs` (specification, manifest, |
| 15 | // version bound) and `typst-eval/src/import.rs` (manifest validation), Apache-2.0. A manifest error is the |
| 16 | // text Typst's serde derive would give, since that text is what a document's author sees. |
| 17 | |
| 18 | use crate::eval::lib::data::toml_parse; |
| 19 | use crate::eval::value::Value; |
| 20 | use crate::syntax::lexer::is_ident; |
| 21 | |
| 22 | use oxedyne_fe2o3_core::prelude::*; |
| 23 | |
| 24 | use std::collections::BTreeMap; |
| 25 | use std::fmt; |
| 26 | use std::io; |
| 27 | use std::path::{ |
| 28 | Component, |
| 29 | Path, |
| 30 | PathBuf, |
| 31 | }; |
| 32 | use std::sync::{ |
| 33 | Arc, |
| 34 | RwLock, |
| 35 | }; |
| 36 | |
| 37 | pub const DEFAULT_NAMESPACE: &str = "preview"; // the registry's namespace |
| 38 | pub const MANIFEST: &str = "typst.toml"; |
| 39 | pub const TYPST_VERSION: (u32, u32, u32) = (0, 15, 1); // the Typst whose language is evaluated |
| 40 | |
| 41 | #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 42 | pub struct PackageSpec { |
| 43 | pub namespace: String, // `preview`, `local` |
| 44 | pub name: String, |
| 45 | pub version: (u32, u32, u32), |
| 46 | } |
| 47 | |
| 48 | impl PackageSpec { |
| 49 | /// The package's root in the vfs, `@preview/cetz:0.3.4`: relative, so no project path can name it. |
| 50 | pub fn root(&self) -> PathBuf { |
| 51 | PathBuf::from(fmt!("@{}", self.namespace)).join(fmt!("{}:{}", self.name, version_text(self.version))) |
| 52 | } |
| 53 | |
| 54 | /// The package whose vfs root `path` lies under, and the path within it (`src/lib.typ`, empty at the |
| 55 | /// root itself). |
| 56 | pub fn of_path(path: &Path) -> Option<(PackageSpec, String)> { |
| 57 | let mut comps = path.components(); |
| 58 | let ns = match comps.next().and_then(normal) { |
| 59 | Some(c) if c.starts_with('@') => c, |
| 60 | _ => return None, |
| 61 | }; |
| 62 | let named = match comps.next().and_then(normal) { |
| 63 | Some(c) => c, |
| 64 | None => return None, |
| 65 | }; |
| 66 | let spec = match parse_spec(&fmt!("{}/{}", ns, named)) { |
| 67 | Ok(s) => s, |
| 68 | Err(_) => return None, |
| 69 | }; |
| 70 | let mut rel = String::new(); |
| 71 | for c in comps { |
| 72 | match normal(c) { |
| 73 | Some(s) => { |
| 74 | if !rel.is_empty() { |
| 75 | rel.push('/'); |
| 76 | } |
| 77 | rel.push_str(s); |
| 78 | } |
| 79 | None => return None, |
| 80 | } |
| 81 | } |
| 82 | Some((spec, rel)) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | impl fmt::Display for PackageSpec { |
| 87 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 88 | write!(f, "@{}/{}:{}", self.namespace, self.name, version_text(self.version)) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | pub fn version_text(v: (u32, u32, u32)) -> String { |
| 93 | fmt!("{}.{}.{}", v.0, v.1, v.2) |
| 94 | } |
| 95 | |
| 96 | // A path component that is an ordinary UTF-8 name. |
| 97 | fn normal(c: Component<'_>) -> Option<&str> { |
| 98 | match c { |
| 99 | Component::Normal(s) => s.to_str(), |
| 100 | _ => None, |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /// Parses `@namespace/name:1.2.3` as Typst does, with Typst's messages for each malformed part. |
| 105 | pub fn parse_spec(spec: &str) -> Outcome<PackageSpec> { |
| 106 | let rest = match spec.strip_prefix('@') { |
| 107 | Some(r) => r, |
| 108 | None => return Err(bad("package specification must start with '@'".to_string())), |
| 109 | }; |
| 110 | let (namespace, rest) = rest.split_once('/').unwrap_or((rest, "")); |
| 111 | if namespace.is_empty() { |
| 112 | return Err(bad("package specification is missing namespace".to_string())); |
| 113 | } |
| 114 | if !is_ident(namespace) { |
| 115 | return Err(bad(fmt!("`{}` is not a valid package namespace", namespace))); |
| 116 | } |
| 117 | let (name, version) = rest.split_once(':').unwrap_or((rest, "")); |
| 118 | if name.is_empty() { |
| 119 | return Err(bad("package specification is missing name".to_string())); |
| 120 | } |
| 121 | if !is_ident(name) { |
| 122 | return Err(bad(fmt!("`{}` is not a valid package name", name))); |
| 123 | } |
| 124 | if version.is_empty() { |
| 125 | return Err(bad("package specification is missing version".to_string())); |
| 126 | } |
| 127 | let version = match parse_version(version) { |
| 128 | Ok(v) => v, |
| 129 | Err(m) => return Err(bad(m)), |
| 130 | }; |
| 131 | Ok(PackageSpec { |
| 132 | namespace: namespace.to_string(), |
| 133 | name: name.to_string(), |
| 134 | version, |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | /// A `major.minor.patch` version, as a specification and a manifest both write it. |
| 139 | pub fn parse_version(s: &str) -> Result<(u32, u32, u32), String> { |
| 140 | let mut parts = s.split('.'); |
| 141 | let mut next = |which: &str| -> Result<u32, String> { |
| 142 | match parts.next().filter(|p| !p.is_empty()) { |
| 143 | Some(p) => p.parse::<u32>().map_err(|_| fmt!("`{}` is not a valid {} version", p, which)), |
| 144 | None => Err(fmt!("version number is missing {} version", which)), |
| 145 | } |
| 146 | }; |
| 147 | let major = ok!(next("major")); |
| 148 | let minor = ok!(next("minor")); |
| 149 | let patch = ok!(next("patch")); |
| 150 | if let Some(extra) = parts.next() { |
| 151 | return Err(fmt!("version number has unexpected fourth component: `{}`", extra)); |
| 152 | } |
| 153 | Ok((major, minor, patch)) |
| 154 | } |
| 155 | |
| 156 | fn bad(msg: String) -> Error<ErrTag> { |
| 157 | err!("{}", msg; Input, Invalid) |
| 158 | } |
| 159 | |
| 160 | /// A lower bound on the Typst version a package needs: `0.12`, `0.12.1`, or just `1`. |
| 161 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 162 | pub struct VersionBound { |
| 163 | pub major: u32, |
| 164 | pub minor: Option<u32>, |
| 165 | pub patch: Option<u32>, |
| 166 | } |
| 167 | |
| 168 | impl VersionBound { |
| 169 | pub fn parse(s: &str) -> Result<Self, String> { |
| 170 | let mut parts = s.split('.'); |
| 171 | let mut next = |which: &str| -> Result<Option<u32>, String> { |
| 172 | match parts.next() { |
| 173 | Some(p) => p.parse::<u32>().map(Some).map_err(|_| fmt!("`{}` is not a valid {} version bound", p, which)), |
| 174 | None => Ok(None), |
| 175 | } |
| 176 | }; |
| 177 | let major = match ok!(next("major")) { |
| 178 | Some(m) => m, |
| 179 | None => return Err("version bound is missing major version".to_string()), |
| 180 | }; |
| 181 | let minor = ok!(next("minor")); |
| 182 | let patch = ok!(next("patch")); |
| 183 | if let Some(extra) = parts.next() { |
| 184 | return Err(fmt!("version bound has unexpected fourth component: `{}`", extra)); |
| 185 | } |
| 186 | Ok(Self { major, minor, patch }) |
| 187 | } |
| 188 | |
| 189 | /// Does `v` reach the bound, comparing only the parts the bound names? |
| 190 | pub fn admits(&self, v: (u32, u32, u32)) -> bool { |
| 191 | if v.0 != self.major { |
| 192 | return v.0 > self.major; |
| 193 | } |
| 194 | let minor = match self.minor { |
| 195 | Some(m) => m, |
| 196 | None => return true, |
| 197 | }; |
| 198 | if v.1 != minor { |
| 199 | return v.1 > minor; |
| 200 | } |
| 201 | match self.patch { |
| 202 | Some(p) => v.2 >= p, |
| 203 | None => true, |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | impl fmt::Display for VersionBound { |
| 209 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 210 | let mut s = fmt!("{}", self.major); |
| 211 | if let Some(m) = self.minor { |
| 212 | s.push_str(&fmt!(".{}", m)); |
| 213 | } |
| 214 | if let Some(p) = self.patch { |
| 215 | s.push_str(&fmt!(".{}", p)); |
| 216 | } |
| 217 | write!(f, "{}", s) |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // Manifest |
| 222 | |
| 223 | /// What a package's `typst.toml` says about it, as far as importing it needs. |
| 224 | #[derive(Clone, Debug, PartialEq)] |
| 225 | pub struct Manifest { |
| 226 | pub name: String, |
| 227 | pub version: (u32, u32, u32), |
| 228 | pub entrypoint: String, |
| 229 | pub compiler: Option<VersionBound>, |
| 230 | } |
| 231 | |
| 232 | impl Manifest { |
| 233 | /// Reads a manifest's text. An error is the reason Typst puts inside "package manifest is malformed |
| 234 | /// (..)". |
| 235 | pub fn parse(text: &str) -> Result<Self, String> { |
| 236 | match toml_parse(text) { |
| 237 | Ok(t) => Self::from_table(&t), |
| 238 | Err((_, m)) => Err(m), |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /// Reads a manifest from its parsed TOML table. Typst deserialises the manifest with serde, so an |
| 243 | /// entry of the wrong type is reported in document order, and a missing field after every entry |
| 244 | /// present has been read. |
| 245 | pub fn from_table(table: &Value) -> Result<Self, String> { |
| 246 | let top = match table { |
| 247 | Value::Dict(d) => d, |
| 248 | other => return Err(invalid_type(other, "struct PackageManifest")), |
| 249 | }; |
| 250 | let mut package = None; |
| 251 | for (key, v) in top.iter() { |
| 252 | match key { |
| 253 | "package" => package = Some(ok!(Self::package(v))), |
| 254 | "template" => ok!(template(v)), |
| 255 | "tool" => ok!(tool(v)), |
| 256 | _ => (), |
| 257 | } |
| 258 | } |
| 259 | match package { |
| 260 | Some(m) => Ok(m), |
| 261 | None => Err(missing("package")), |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | fn package(v: &Value) -> Result<Self, String> { |
| 266 | let d = match v { |
| 267 | Value::Dict(d) => d, |
| 268 | other => return Err(invalid_type(other, "struct PackageInfo")), |
| 269 | }; |
| 270 | let mut name = None; |
| 271 | let mut version = None; |
| 272 | let mut entrypoint = None; |
| 273 | let mut compiler = None; |
| 274 | for (key, v) in d.iter() { |
| 275 | match key { |
| 276 | "name" => name = Some(ok!(want_str(v))), |
| 277 | "version" => version = Some(ok!(parse_version(&ok!(want_str(v))))), |
| 278 | "entrypoint" => entrypoint = Some(ok!(want_str(v))), |
| 279 | "compiler" => compiler = Some(ok!(VersionBound::parse(&ok!(want_str(v))))), |
| 280 | "license" | "description" | "homepage" | "repository" => { |
| 281 | let _ = ok!(want_str(v)); |
| 282 | } |
| 283 | "authors" | "keywords" | "categories" | "disciplines" | "exclude" => ok!(want_strs(v)), |
| 284 | _ => (), |
| 285 | } |
| 286 | } |
| 287 | Ok(Self { |
| 288 | name: ok!(name.ok_or_else(|| missing("name"))), |
| 289 | version: ok!(version.ok_or_else(|| missing("version"))), |
| 290 | entrypoint: ok!(entrypoint.ok_or_else(|| missing("entrypoint"))), |
| 291 | compiler, |
| 292 | }) |
| 293 | } |
| 294 | |
| 295 | /// Is this the manifest of `spec`, and can this Typst run it? The error is Typst's own message. |
| 296 | pub fn validate(&self, spec: &PackageSpec) -> Result<(), String> { |
| 297 | if self.name != spec.name { |
| 298 | return Err(fmt!("package manifest contains mismatched name `{}`", self.name)); |
| 299 | } |
| 300 | if self.version != spec.version { |
| 301 | return Err(fmt!("package manifest contains mismatched version {}", version_text(self.version))); |
| 302 | } |
| 303 | if let Some(needed) = &self.compiler { |
| 304 | if !needed.admits(TYPST_VERSION) { |
| 305 | return Err(fmt!("package requires Typst {} or newer (current version is {})", |
| 306 | needed, version_text(TYPST_VERSION))); |
| 307 | } |
| 308 | } |
| 309 | Ok(()) |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // A `[template]` table: read only so that a malformed one is refused as Typst refuses it. |
| 314 | fn template(v: &Value) -> Result<(), String> { |
| 315 | let d = match v { |
| 316 | Value::Dict(d) => d, |
| 317 | other => return Err(invalid_type(other, "struct TemplateInfo")), |
| 318 | }; |
| 319 | let mut path = false; |
| 320 | let mut entrypoint = false; |
| 321 | for (key, v) in d.iter() { |
| 322 | match key { |
| 323 | "path" => { let _ = ok!(want_str(v)); path = true; } |
| 324 | "entrypoint" => { let _ = ok!(want_str(v)); entrypoint = true; } |
| 325 | "thumbnail" => { let _ = ok!(want_str(v)); } |
| 326 | _ => (), |
| 327 | } |
| 328 | } |
| 329 | if !path { |
| 330 | return Err(missing("path")); |
| 331 | } |
| 332 | if !entrypoint { |
| 333 | return Err(missing("entrypoint")); |
| 334 | } |
| 335 | Ok(()) |
| 336 | } |
| 337 | |
| 338 | // A `[tool]` table: each entry is a tool's own table. |
| 339 | fn tool(v: &Value) -> Result<(), String> { |
| 340 | let d = match v { |
| 341 | Value::Dict(d) => d, |
| 342 | other => return Err(invalid_type(other, "struct ToolInfo")), |
| 343 | }; |
| 344 | for (_, v) in d.iter() { |
| 345 | if !matches!(v, Value::Dict(_)) { |
| 346 | return Err(invalid_type(v, "a map")); |
| 347 | } |
| 348 | } |
| 349 | Ok(()) |
| 350 | } |
| 351 | |
| 352 | fn want_str(v: &Value) -> Result<String, String> { |
| 353 | match v { |
| 354 | Value::Str(s) => Ok(s.to_string()), |
| 355 | other => Err(invalid_type(other, "a string")), |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | fn want_strs(v: &Value) -> Result<(), String> { |
| 360 | match v { |
| 361 | Value::Array(a) => { |
| 362 | for x in a.iter() { |
| 363 | let _ = ok!(want_str(x)); |
| 364 | } |
| 365 | Ok(()) |
| 366 | } |
| 367 | other => Err(invalid_type(other, "a sequence")), |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | fn missing(field: &str) -> String { |
| 372 | fmt!("missing field `{}`", field) |
| 373 | } |
| 374 | |
| 375 | // serde's "invalid type" message: the value found as serde names it, then what was expected. A TOML |
| 376 | // datetime reaches serde as a map. |
| 377 | fn invalid_type(v: &Value, expected: &str) -> String { |
| 378 | let found = match v { |
| 379 | Value::Int(i) => fmt!("integer `{}`", i), |
| 380 | Value::Float(f) => fmt!("floating point `{}`", serde_float(*f)), |
| 381 | Value::Bool(b) => fmt!("boolean `{}`", b), |
| 382 | Value::Str(s) => fmt!("string {:?}", s.as_str()), |
| 383 | Value::Array(_) => "sequence".to_string(), |
| 384 | _ => "map".to_string(), |
| 385 | }; |
| 386 | fmt!("invalid type: {}, expected {}", found, expected) |
| 387 | } |
| 388 | |
| 389 | // A float as serde's `Unexpected::Float` shows it: Rust's display, given a decimal point when it has none. |
| 390 | fn serde_float(f: f64) -> String { |
| 391 | let s = fmt!("{}", f); |
| 392 | if f.is_finite() && !s.contains('.') { |
| 393 | fmt!("{}.0", s) |
| 394 | } else { |
| 395 | s |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // The store |
| 400 | |
| 401 | /// The files a host handed over for one package, by package-relative path (`src/lib.typ`). |
| 402 | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
| 403 | pub struct PackageFiles { |
| 404 | files: BTreeMap<String, Arc<Vec<u8>>>, |
| 405 | } |
| 406 | |
| 407 | impl PackageFiles { |
| 408 | pub fn len(&self) -> usize { self.files.len() } |
| 409 | |
| 410 | pub fn is_empty(&self) -> bool { self.files.is_empty() } |
| 411 | |
| 412 | pub fn get(&self, rel: &str) -> Option<&Arc<Vec<u8>>> { self.files.get(rel) } |
| 413 | |
| 414 | /// Is `rel` a directory of the package: its root, or a prefix of some file's path? |
| 415 | pub fn is_dir(&self, rel: &str) -> bool { |
| 416 | if rel.is_empty() { |
| 417 | return true; |
| 418 | } |
| 419 | let prefix = fmt!("{}/", rel); |
| 420 | self.files.range(prefix.clone()..).next().map(|(k, _)| k.starts_with(&prefix)).unwrap_or(false) |
| 421 | } |
| 422 | |
| 423 | pub fn paths(&self) -> impl Iterator<Item = &str> { self.files.keys().map(|k| k.as_str()) } |
| 424 | } |
| 425 | |
| 426 | // Every package a host supplied in memory, and the directories on disc searched for any other, in order. |
| 427 | #[derive(Debug)] |
| 428 | struct Store { |
| 429 | supplied: BTreeMap<PackageSpec, Arc<PackageFiles>>, |
| 430 | dirs: Vec<PathBuf>, |
| 431 | } |
| 432 | |
| 433 | static STORE: RwLock<Store> = RwLock::new(Store { supplied: BTreeMap::new(), dirs: Vec::new() }); |
| 434 | |
| 435 | /// Where one package was found. |
| 436 | #[derive(Clone, Debug)] |
| 437 | pub enum Found { |
| 438 | Supplied(Arc<PackageFiles>), // handed over in memory |
| 439 | Dir(PathBuf), // its own directory on disc, `<dir>/<namespace>/<name>/<version>` |
| 440 | } |
| 441 | |
| 442 | /// Hands a package's files to the engine and returns how many files it holds. Paths are package-relative; |
| 443 | /// a leading `/` means the package root, as it does inside the package. The files serve every later |
| 444 | /// compile, in memory, until withdrawn. A version is immutable: supplying the same files again changes |
| 445 | /// nothing, and supplying different ones is an error until the package is withdrawn. |
| 446 | pub fn supply(spec: &PackageSpec, files: Vec<(String, Vec<u8>)>) -> Outcome<usize> { |
| 447 | let mut held = PackageFiles::default(); |
| 448 | for (path, bytes) in files { |
| 449 | let rel = match normalise_rel(&path) { |
| 450 | Some(r) => r, |
| 451 | None => return Err(err!( |
| 452 | "The file path {:?} supplied for {} does not name a file within the package.", path, spec; |
| 453 | Input, Invalid)), |
| 454 | }; |
| 455 | held.files.insert(rel, Arc::new(bytes)); |
| 456 | } |
| 457 | let n = held.len(); |
| 458 | let mut store = lock_write!(STORE, "While supplying a package"); |
| 459 | if let Some(have) = store.supplied.get(spec) { |
| 460 | if **have == held { |
| 461 | return Ok(n); |
| 462 | } |
| 463 | return Err(err!( |
| 464 | "The package {} is already supplied with different files. A version is immutable, so withdraw it \ |
| 465 | before supplying another.", spec; |
| 466 | Input, Invalid, Exists)); |
| 467 | } |
| 468 | store.supplied.insert(spec.clone(), Arc::new(held)); |
| 469 | Ok(n) |
| 470 | } |
| 471 | |
| 472 | /// Withdraws a package supplied in memory; false when none was held for it. |
| 473 | pub fn withdraw(spec: &PackageSpec) -> Outcome<bool> { |
| 474 | let mut store = lock_write!(STORE, "While withdrawing a package"); |
| 475 | Ok(store.supplied.remove(spec).is_some()) |
| 476 | } |
| 477 | |
| 478 | /// Every package supplied in memory, in order. |
| 479 | pub fn supplied() -> Outcome<Vec<PackageSpec>> { |
| 480 | let store = lock_read!(STORE, "While listing the supplied packages"); |
| 481 | Ok(store.supplied.keys().cloned().collect()) |
| 482 | } |
| 483 | |
| 484 | /// Adds a directory on disc to search for packages not supplied in memory, laid out as Typst lays out |
| 485 | /// its package path and cache: `<dir>/<namespace>/<name>/<version>/`. Directories are searched in the |
| 486 | /// order added; one already present is not added twice. |
| 487 | pub fn add_dir(dir: PathBuf) -> Outcome<()> { |
| 488 | let mut store = lock_write!(STORE, "While adding a package directory"); |
| 489 | if !store.dirs.contains(&dir) { |
| 490 | store.dirs.push(dir); |
| 491 | } |
| 492 | Ok(()) |
| 493 | } |
| 494 | |
| 495 | pub fn dirs() -> Outcome<Vec<PathBuf>> { |
| 496 | let store = lock_read!(STORE, "While listing the package directories"); |
| 497 | Ok(store.dirs.clone()) |
| 498 | } |
| 499 | |
| 500 | /// Where the package is, if the host supplied it. |
| 501 | pub fn find(spec: &PackageSpec) -> Option<Found> { |
| 502 | let store = match STORE.read() { |
| 503 | Ok(s) => s, |
| 504 | Err(_) => return None, |
| 505 | }; |
| 506 | if let Some(files) = store.supplied.get(spec) { |
| 507 | return Some(Found::Supplied(files.clone())); |
| 508 | } |
| 509 | for dir in &store.dirs { |
| 510 | let own = dir.join(&spec.namespace).join(&spec.name).join(version_text(spec.version)); |
| 511 | if native_is_dir(&own) { |
| 512 | return Some(Found::Dir(own)); |
| 513 | } |
| 514 | } |
| 515 | None |
| 516 | } |
| 517 | |
| 518 | /// Every version of `namespace/name` the host supplied, oldest first. |
| 519 | pub fn versions(namespace: &str, name: &str) -> Vec<(u32, u32, u32)> { |
| 520 | let mut out = Vec::new(); |
| 521 | if let Ok(store) = STORE.read() { |
| 522 | for spec in store.supplied.keys() { |
| 523 | if spec.namespace == namespace && spec.name == name { |
| 524 | out.push(spec.version); |
| 525 | } |
| 526 | } |
| 527 | for dir in &store.dirs { |
| 528 | let dir = dir.join(namespace).join(name); |
| 529 | for v in native_subdirs(&dir) { |
| 530 | if let Ok(parsed) = parse_version(&v) { |
| 531 | out.push(parsed); |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | } |
| 536 | out.sort(); |
| 537 | out.dedup(); |
| 538 | out |
| 539 | } |
| 540 | |
| 541 | // A supplied path within a package, normalised as a package-relative path. `None` when it would leave the |
| 542 | // package, names the root itself, or holds a backslash. |
| 543 | fn normalise_rel(path: &str) -> Option<String> { |
| 544 | let mut segs: Vec<&str> = Vec::new(); |
| 545 | for s in path.split('/') { |
| 546 | match s { |
| 547 | "" | "." => (), |
| 548 | ".." => if segs.pop().is_none() { return None; }, |
| 549 | s if s.contains('\\') => return None, |
| 550 | s => segs.push(s), |
| 551 | } |
| 552 | } |
| 553 | if segs.is_empty() { |
| 554 | return None; |
| 555 | } |
| 556 | Some(segs.join("/")) |
| 557 | } |
| 558 | |
| 559 | // The vfs's view of package paths. A path under a package root is served from the package, wherever the |
| 560 | // host put it, and never from the project's files or the real filesystem. |
| 561 | |
| 562 | /// The bytes at a package path, or `None` when `path` is not under a package root. |
| 563 | pub fn read(path: &Path) -> Option<io::Result<Vec<u8>>> { |
| 564 | let (spec, rel) = PackageSpec::of_path(path)?; |
| 565 | Some(match find(&spec) { |
| 566 | Some(Found::Supplied(files)) => match files.get(&rel) { |
| 567 | Some(b) => Ok((**b).clone()), |
| 568 | None if files.is_dir(&rel) => Err(io::Error::new(io::ErrorKind::IsADirectory, "is a directory")), |
| 569 | None => Err(not_found(path)), |
| 570 | }, |
| 571 | Some(Found::Dir(dir)) => { |
| 572 | let real = dir.join(&rel); |
| 573 | if native_is_dir(&real) { |
| 574 | Err(io::Error::new(io::ErrorKind::IsADirectory, "is a directory")) |
| 575 | } else { |
| 576 | native_read(&real) |
| 577 | } |
| 578 | } |
| 579 | None => Err(not_found(path)), |
| 580 | }) |
| 581 | } |
| 582 | |
| 583 | /// Is the package path a file? `None` when `path` is not under a package root. |
| 584 | pub fn is_file(path: &Path) -> Option<bool> { |
| 585 | let (spec, rel) = PackageSpec::of_path(path)?; |
| 586 | Some(match find(&spec) { |
| 587 | Some(Found::Supplied(files)) => files.get(&rel).is_some(), |
| 588 | Some(Found::Dir(dir)) => native_is_file(&dir.join(&rel)), |
| 589 | None => false, |
| 590 | }) |
| 591 | } |
| 592 | |
| 593 | /// Is the package path a directory, the package root included? `None` when `path` is not under one. |
| 594 | pub fn is_dir(path: &Path) -> Option<bool> { |
| 595 | let (spec, rel) = PackageSpec::of_path(path)?; |
| 596 | Some(match find(&spec) { |
| 597 | Some(Found::Supplied(files)) => files.is_dir(&rel), |
| 598 | Some(Found::Dir(dir)) => native_is_dir(&dir.join(&rel)), |
| 599 | None => false, |
| 600 | }) |
| 601 | } |
| 602 | |
| 603 | /// Every file beneath a package path, as vfs paths, or `None` when `path` is not under a package root. |
| 604 | pub fn list(path: &Path) -> Option<Vec<PathBuf>> { |
| 605 | let (spec, rel) = PackageSpec::of_path(path)?; |
| 606 | let root = spec.root(); |
| 607 | let mut out = Vec::new(); |
| 608 | match find(&spec) { |
| 609 | Some(Found::Supplied(files)) => { |
| 610 | let prefix = if rel.is_empty() { String::new() } else { fmt!("{}/", rel) }; |
| 611 | for p in files.paths() { |
| 612 | if p.starts_with(&prefix) { |
| 613 | out.push(root.join(p)); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | Some(Found::Dir(dir)) => { |
| 618 | let mut found = Vec::new(); |
| 619 | native_list(&dir.join(&rel), &mut found, LIST_DEPTH); |
| 620 | for f in found { |
| 621 | if let Ok(within) = f.strip_prefix(&dir) { |
| 622 | out.push(root.join(within)); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | None => (), |
| 627 | } |
| 628 | out.sort(); |
| 629 | Some(out) |
| 630 | } |
| 631 | |
| 632 | /// The file on disc that a vfs path names: a path under a package kept on disc, by its real path, and any |
| 633 | /// other path as it is. A package supplied in memory has no file behind it, nor has one nobody supplied, so |
| 634 | /// those give `None`. |
| 635 | pub fn on_disc(path: &Path) -> Option<PathBuf> { |
| 636 | match PackageSpec::of_path(path) { |
| 637 | None => Some(path.to_path_buf()), |
| 638 | Some((spec, rel)) => match find(&spec) { |
| 639 | Some(Found::Dir(dir)) => Some(dir.join(rel)), |
| 640 | _ => None, |
| 641 | }, |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | /// How a diagnostic names the file at a vfs path: a file in a package kept on disc by its real path, as |
| 646 | /// Typst names it; any other file by its vfs path. |
| 647 | pub fn display(path: &Path) -> String { |
| 648 | if let Some((spec, rel)) = PackageSpec::of_path(path) { |
| 649 | if let Some(Found::Dir(dir)) = find(&spec) { |
| 650 | return dir.join(rel).display().to_string(); |
| 651 | } |
| 652 | } |
| 653 | path.display().to_string() |
| 654 | } |
| 655 | |
| 656 | fn not_found(path: &Path) -> io::Error { |
| 657 | io::Error::new(io::ErrorKind::NotFound, fmt!("{} was not supplied", path.display())) |
| 658 | } |
| 659 | |
| 660 | // How deep a package directory on disc is listed. |
| 661 | const LIST_DEPTH: u32 = 16; |
| 662 | |
| 663 | // The real filesystem, for packages kept on disc. The wasm target has none, so there a package can only |
| 664 | // be supplied in memory. |
| 665 | |
| 666 | #[cfg(not(target_arch = "wasm32"))] |
| 667 | fn native_read(path: &Path) -> io::Result<Vec<u8>> { |
| 668 | std::fs::read(path) |
| 669 | } |
| 670 | |
| 671 | #[cfg(target_arch = "wasm32")] |
| 672 | fn native_read(path: &Path) -> io::Result<Vec<u8>> { |
| 673 | Err(not_found(path)) |
| 674 | } |
| 675 | |
| 676 | #[cfg(not(target_arch = "wasm32"))] |
| 677 | fn native_is_file(path: &Path) -> bool { |
| 678 | path.is_file() |
| 679 | } |
| 680 | |
| 681 | #[cfg(target_arch = "wasm32")] |
| 682 | fn native_is_file(_path: &Path) -> bool { |
| 683 | false |
| 684 | } |
| 685 | |
| 686 | #[cfg(not(target_arch = "wasm32"))] |
| 687 | fn native_is_dir(path: &Path) -> bool { |
| 688 | path.is_dir() |
| 689 | } |
| 690 | |
| 691 | #[cfg(target_arch = "wasm32")] |
| 692 | fn native_is_dir(_path: &Path) -> bool { |
| 693 | false |
| 694 | } |
| 695 | |
| 696 | #[cfg(not(target_arch = "wasm32"))] |
| 697 | fn native_subdirs(dir: &Path) -> Vec<String> { |
| 698 | let mut out = Vec::new(); |
| 699 | if let Ok(entries) = std::fs::read_dir(dir) { |
| 700 | for e in entries.flatten() { |
| 701 | if e.path().is_dir() { |
| 702 | if let Some(n) = e.file_name().to_str() { |
| 703 | out.push(n.to_string()); |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | out |
| 709 | } |
| 710 | |
| 711 | #[cfg(target_arch = "wasm32")] |
| 712 | fn native_subdirs(_dir: &Path) -> Vec<String> { |
| 713 | Vec::new() |
| 714 | } |
| 715 | |
| 716 | #[cfg(not(target_arch = "wasm32"))] |
| 717 | fn native_list(dir: &Path, out: &mut Vec<PathBuf>, depth: u32) { |
| 718 | if depth == 0 { |
| 719 | return; |
| 720 | } |
| 721 | let entries = match std::fs::read_dir(dir) { |
| 722 | Ok(e) => e, |
| 723 | Err(_) => return, |
| 724 | }; |
| 725 | for entry in entries.flatten() { |
| 726 | let path = entry.path(); |
| 727 | if path.is_dir() { |
| 728 | native_list(&path, out, depth - 1); |
| 729 | } else if path.is_file() { |
| 730 | out.push(path); |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | #[cfg(target_arch = "wasm32")] |
| 736 | fn native_list(_dir: &Path, _out: &mut Vec<PathBuf>, _depth: u32) {} |
| 737 | |
| 738 | #[cfg(test)] |
| 739 | mod tests { |
| 740 | use super::*; |
| 741 | use crate::eval::value::Dict; |
| 742 | |
| 743 | fn table(entries: &[(&str, Value)]) -> Value { |
| 744 | let mut d = Dict::new(); |
| 745 | for (k, v) in entries { |
| 746 | d.insert(k, v.clone()); |
| 747 | } |
| 748 | Value::dict(d) |
| 749 | } |
| 750 | |
| 751 | fn pkg(entries: &[(&str, Value)]) -> Value { |
| 752 | table(&[("package", table(entries))]) |
| 753 | } |
| 754 | |
| 755 | #[test] |
| 756 | fn roots_are_relative_and_round_trip() -> Outcome<()> { |
| 757 | let spec = res!(parse_spec("@preview/cetz:0.3.4")); |
| 758 | assert_eq!(spec.root(), PathBuf::from("@preview/cetz:0.3.4")); |
| 759 | assert!(spec.root().is_relative()); |
| 760 | assert_eq!(fmt!("{}", spec), "@preview/cetz:0.3.4"); |
| 761 | let (back, rel) = res!(PackageSpec::of_path(&spec.root().join("src/lib.typ")) |
| 762 | .ok_or_else(|| err!("No package in the path."; Missing))); |
| 763 | assert_eq!(back, spec); |
| 764 | assert_eq!(rel, "src/lib.typ"); |
| 765 | assert!(PackageSpec::of_path(Path::new("/@preview/cetz:0.3.4/lib.typ")).is_none()); |
| 766 | assert!(PackageSpec::of_path(Path::new("@preview/cetz/lib.typ")).is_none()); |
| 767 | Ok(()) |
| 768 | } |
| 769 | |
| 770 | #[test] |
| 771 | fn version_bounds_admit_as_typst_does() -> Outcome<()> { |
| 772 | let b = |s: &str| VersionBound::parse(s); |
| 773 | let v = (1, 1, 1); |
| 774 | assert!(res!(b("1").map_err(|m| err!("{}", m; Invalid))).admits(v)); |
| 775 | assert!(res!(b("1.1").map_err(|m| err!("{}", m; Invalid))).admits(v)); |
| 776 | assert!(!res!(b("1.2").map_err(|m| err!("{}", m; Invalid))).admits(v)); |
| 777 | assert!(res!(b("1.0").map_err(|m| err!("{}", m; Invalid))).admits(v)); |
| 778 | assert!(res!(b("0.15.1").map_err(|m| err!("{}", m; Invalid))).admits(TYPST_VERSION)); |
| 779 | assert!(!res!(b("0.15.2").map_err(|m| err!("{}", m; Invalid))).admits(TYPST_VERSION)); |
| 780 | assert!(!res!(b("0.16").map_err(|m| err!("{}", m; Invalid))).admits(TYPST_VERSION)); |
| 781 | assert_eq!(b("abc"), Err("`abc` is not a valid major version bound".to_string())); |
| 782 | assert_eq!(b(""), Err("`` is not a valid major version bound".to_string())); |
| 783 | assert_eq!(b("0.15.1.2"), Err("version bound has unexpected fourth component: `2`".to_string())); |
| 784 | assert_eq!(fmt!("{}", res!(b("0.16").map_err(|m| err!("{}", m; Invalid)))), "0.16"); |
| 785 | Ok(()) |
| 786 | } |
| 787 | |
| 788 | // Each message is the one typst 0.15.1 gives for the same manifest (checked by hand against the |
| 789 | // oracle; `tests/eval_package.rs` checks them end to end). |
| 790 | #[test] |
| 791 | fn manifests_are_read_with_serde_wording() { |
| 792 | let s = |x: &str| Value::str(x); |
| 793 | let good = Manifest::from_table(&pkg(&[ |
| 794 | ("name", s("demo")), ("version", s("0.1.0")), ("entrypoint", s("lib.typ")), |
| 795 | ("authors", Value::array(vec![s("me")])), ("compiler", s("0.12")), |
| 796 | ])); |
| 797 | assert_eq!(good.map(|m| (m.name, m.version, m.entrypoint)), |
| 798 | Ok(("demo".to_string(), (0, 1, 0), "lib.typ".to_string()))); |
| 799 | let cases: Vec<(Value, &str)> = vec![ |
| 800 | (table(&[("name", s("x"))]), "missing field `package`"), |
| 801 | (table(&[("package", s("x"))]), "invalid type: string \"x\", expected struct PackageInfo"), |
| 802 | (pkg(&[("name", s("x")), ("version", s("0.1.0"))]), "missing field `entrypoint`"), |
| 803 | (pkg(&[("name", Value::Int(5)), ("version", s("0.1.0")), ("entrypoint", s("l"))]), |
| 804 | "invalid type: integer `5`, expected a string"), |
| 805 | (pkg(&[("version", Value::Float(1.0))]), "invalid type: floating point `1.0`, expected a string"), |
| 806 | (pkg(&[("version", Value::Bool(true))]), "invalid type: boolean `true`, expected a string"), |
| 807 | (pkg(&[("version", Value::array(vec![]))]), "invalid type: sequence, expected a string"), |
| 808 | (pkg(&[("version", s("0.1"))]), "version number is missing patch version"), |
| 809 | (pkg(&[("authors", s("me"))]), "invalid type: string \"me\", expected a sequence"), |
| 810 | (pkg(&[("entrypoint", Value::Int(5)), ("name", Value::Int(7))]), |
| 811 | "invalid type: integer `5`, expected a string"), |
| 812 | (pkg(&[("compiler", s("abc"))]), "`abc` is not a valid major version bound"), |
| 813 | (table(&[("package", table(&[("name", s("x")), ("version", s("0.1.0")), ("entrypoint", s("l"))])), |
| 814 | ("tool", table(&[("x", Value::Int(1))]))]), "invalid type: integer `1`, expected a map"), |
| 815 | (table(&[("package", table(&[("name", s("x")), ("version", s("0.1.0")), ("entrypoint", s("l"))])), |
| 816 | ("template", table(&[("path", s("t"))]))]), "missing field `entrypoint`"), |
| 817 | ]; |
| 818 | for (t, want) in cases { |
| 819 | assert_eq!(Manifest::from_table(&t).map(|m| m.name), Err(want.to_string())); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | #[test] |
| 824 | fn validation_uses_typst_messages() -> Outcome<()> { |
| 825 | let spec = res!(parse_spec("@local/demo:0.1.0")); |
| 826 | let m = |name: &str, version: (u32, u32, u32), compiler: Option<&str>| Manifest { |
| 827 | name: name.to_string(), |
| 828 | version, |
| 829 | entrypoint: "lib.typ".to_string(), |
| 830 | compiler: compiler.and_then(|c| VersionBound::parse(c).ok()), |
| 831 | }; |
| 832 | assert_eq!(m("demo", (0, 1, 0), None).validate(&spec), Ok(())); |
| 833 | assert_eq!(m("other", (0, 1, 0), None).validate(&spec), |
| 834 | Err("package manifest contains mismatched name `other`".to_string())); |
| 835 | assert_eq!(m("demo", (0, 1, 1), None).validate(&spec), |
| 836 | Err("package manifest contains mismatched version 0.1.1".to_string())); |
| 837 | assert_eq!(m("demo", (0, 1, 0), Some("0.99.0")).validate(&spec), |
| 838 | Err("package requires Typst 0.99.0 or newer (current version is 0.15.1)".to_string())); |
| 839 | assert_eq!(m("demo", (0, 1, 0), Some("0.15")).validate(&spec), Ok(())); |
| 840 | Ok(()) |
| 841 | } |
| 842 | |
| 843 | #[test] |
| 844 | fn supplied_files_are_package_relative() { |
| 845 | assert_eq!(normalise_rel("typst.toml"), Some("typst.toml".to_string())); |
| 846 | assert_eq!(normalise_rel("/src/./lib.typ"), Some("src/lib.typ".to_string())); |
| 847 | assert_eq!(normalise_rel("src//a/../lib.typ"), Some("src/lib.typ".to_string())); |
| 848 | assert_eq!(normalise_rel("../x"), None); |
| 849 | assert_eq!(normalise_rel("/"), None); |
| 850 | assert_eq!(normalise_rel("a\\b"), None); |
| 851 | } |
| 852 | } |