Skip to main content

rakata_formats/twoda/
source.rs

1//! The capability of handing back a 2DA table by name.
2//!
3//! Decoders that resolve engine integers into typed semantics need to
4//! read tables, but they do not need to know where those tables came
5//! from. Taking a concrete cache instead drags the whole resource-
6//! mounting layer into every crate that only wanted to parse and
7//! resolve bytes.
8//!
9//! This trait is the seam. It lives in `rakata-formats` because it
10//! returns a [`TwoDa`], and formats is the lowest crate that can name
11//! that type.
12
13use super::TwoDa;
14
15/// Something that can produce a parsed 2DA table by name.
16///
17/// Implementors decide where tables come from and whether to cache
18/// them; `&mut self` is there so a lazy implementation can populate
19/// itself on a miss without interior mutability.
20pub trait TwoDaSource {
21    /// Returns the named table, or `None` if it cannot be produced.
22    ///
23    /// `name` is a bare table name with no `.2da` extension, so both a
24    /// `&str` literal and a `TwoDaName` constant work at the call
25    /// site.
26    ///
27    /// The `None` case merges "no such table" with "the table would
28    /// not parse". A decoder falls back to raw values either way, and
29    /// a caller that needs to tell the two apart should ask the
30    /// implementation directly rather than route that through
31    /// resolution.
32    fn twoda(&mut self, name: impl AsRef<str>) -> Option<&TwoDa>;
33}