buildl-core 0.2.0

Domain data, ports, and pure pipeline logic for the buildl build system — no I/O, no Lua.
Documentation
//! The port through which Load evaluates one build file.
//!
//! Its own file because evaluating a build file is the one thing Load cannot do itself: it means
//! reading a file and running a language this crate does not know. Everything around that one call
//! — which files to evaluate, in what order, and what their results mean — stays in Load.
//!
//! Responsibilities: [`DeclarationSource`].
//!
//! Non-responsibilities: implementing it. The real implementation belongs in an adapter crate.
//!
//! This file holds only a trait definition, so it carries no logic to unit-test.

use crate::error::Result;
use crate::types::{BuildFile, Discovered, Evaluated};

/// Evaluates one build file and returns what it staged, exactly as written.
///
/// The [`Discovered`] map holds what discovery targets last produced; a build file that asks for
/// one gets its bytes, or nothing when the map holds none. Load passes the same map to every file
/// it evaluates.
///
/// An implementation reports a missing file as [`Evaluated::Absent`] rather than as an error:
/// what a missing file means depends on who asked for it, which only the caller knows.
///
/// `Sync` because several build files are evaluated at once, each on the thread that took it.
///
/// # Examples
///
/// An implementation needs nothing but this crate's public API:
///
/// ```
/// use std::path::PathBuf;
///
/// use buildl_core::{
///     BuildFile, DeclarationOrder, DeclarationSource, Directory, Discovered, Evaluated,
///     Provenance, Result, StagedFile, StagedSubdir, Written,
/// };
///
/// /// A source in which only the workspace root has a build file, requesting `lib`.
/// struct RootOnly;
///
/// impl DeclarationSource for RootOnly {
///     fn evaluate(&self, file: &BuildFile, _discovered: &Discovered) -> Result<Evaluated> {
///         if !file.directory().is_root() {
///             return Ok(Evaluated::Absent);
///         }
///         Ok(Evaluated::Staged(StagedFile {
///             declarations: Vec::new(),
///             subdirs: vec![StagedSubdir {
///                 path: Written::new("lib"),
///                 order: DeclarationOrder::new(0),
///             }],
///         }))
///     }
/// }
///
/// let lib = BuildFile::new(Provenance::new(
///     PathBuf::from("lib/build.lua"),
///     Directory::parse("lib")?,
/// ));
/// assert_eq!(RootOnly.evaluate(&lib, &Discovered::empty())?, Evaluated::Absent);
/// # Ok::<(), buildl_core::Error>(())
/// ```
pub trait DeclarationSource: Sync {
    /// Evaluates `file`, giving it `discovered` to read discovery results from.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Evaluation`](crate::Error::Evaluation) when the file exists but cannot be
    /// evaluated.
    fn evaluate(&self, file: &BuildFile, discovered: &Discovered) -> Result<Evaluated>;
}