Skip to main content

buildl_core/ports/
declaration_source.rs

1//! The port through which Load evaluates one build file.
2//!
3//! Its own file because evaluating a build file is the one thing Load cannot do itself: it means
4//! reading a file and running a language this crate does not know. Everything around that one call
5//! — which files to evaluate, in what order, and what their results mean — stays in Load.
6//!
7//! Responsibilities: [`DeclarationSource`].
8//!
9//! Non-responsibilities: implementing it. The real implementation belongs in an adapter crate.
10//!
11//! This file holds only a trait definition, so it carries no logic to unit-test.
12
13use crate::error::Result;
14use crate::types::{BuildFile, Discovered, Evaluated};
15
16/// Evaluates one build file and returns what it staged, exactly as written.
17///
18/// The [`Discovered`] map holds what discovery targets last produced; a build file that asks for
19/// one gets its bytes, or nothing when the map holds none. Load passes the same map to every file
20/// it evaluates.
21///
22/// An implementation reports a missing file as [`Evaluated::Absent`] rather than as an error:
23/// what a missing file means depends on who asked for it, which only the caller knows.
24///
25/// `Sync` because several build files are evaluated at once, each on the thread that took it.
26///
27/// # Examples
28///
29/// An implementation needs nothing but this crate's public API:
30///
31/// ```
32/// use std::path::PathBuf;
33///
34/// use buildl_core::{
35///     BuildFile, DeclarationOrder, DeclarationSource, Directory, Discovered, Evaluated,
36///     Provenance, Result, StagedFile, StagedSubdir, Written,
37/// };
38///
39/// /// A source in which only the workspace root has a build file, requesting `lib`.
40/// struct RootOnly;
41///
42/// impl DeclarationSource for RootOnly {
43///     fn evaluate(&self, file: &BuildFile, _discovered: &Discovered) -> Result<Evaluated> {
44///         if !file.directory().is_root() {
45///             return Ok(Evaluated::Absent);
46///         }
47///         Ok(Evaluated::Staged(StagedFile {
48///             declarations: Vec::new(),
49///             subdirs: vec![StagedSubdir {
50///                 path: Written::new("lib"),
51///                 order: DeclarationOrder::new(0),
52///             }],
53///         }))
54///     }
55/// }
56///
57/// let lib = BuildFile::new(Provenance::new(
58///     PathBuf::from("lib/build.lua"),
59///     Directory::parse("lib")?,
60/// ));
61/// assert_eq!(RootOnly.evaluate(&lib, &Discovered::empty())?, Evaluated::Absent);
62/// # Ok::<(), buildl_core::Error>(())
63/// ```
64pub trait DeclarationSource: Sync {
65    /// Evaluates `file`, giving it `discovered` to read discovery results from.
66    ///
67    /// # Errors
68    ///
69    /// Returns [`Error::Evaluation`](crate::Error::Evaluation) when the file exists but cannot be
70    /// evaluated.
71    fn evaluate(&self, file: &BuildFile, discovered: &Discovered) -> Result<Evaluated>;
72}