1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! External tables, the values that a caller installs into them when it
//! constructs a run, and the stamp that records what such a value was produced
//! under.
//!
//! An external table is a table that no stage of the run writes: the caller
//! installs its value when the run is constructed ([`Externals`]). External
//! tables bring data from outside one document into the stages of a run, for
//! example an index of the labels defined in the other documents of a
//! collection, so that references between the documents resolve. A feature
//! declares an external table with
//! [`FeatureBuilder::external_table`](crate::core::registry::FeatureBuilder::external_table);
//! [`FlmRegistry::external_tables`](crate::core::registry::FlmRegistry::external_tables)
//! lists those a registry declares, and
//! [`Plan::externals`](crate::core::plan::Plan::externals) those a plan needs.
//!
//! The installed values are inputs of the run and do not change during its
//! lifetime. [`Run::new`](crate::core::run::Run::new) refuses to construct a run
//! when a value is missing for an external table that the plan needs, when an
//! install refers to a table that the registry does not declare or does not
//! declare as external, when a value is not of the table's declared type, or when a value's
//! stamp differs from the run's own (see [`RunError`](crate::core::run::RunError)).
//!
//! # Stamps
//!
//! A value that an earlier run of this library produced can carry a [`Stamp`]:
//! the digest of the registry it was produced under
//! ([`crate::model::digest`]), the rewrite stages that ran, and a digest of the
//! values of the options those rewrite stages read.
//! [`Run::new`](crate::core::run::Run::new) compares the stamp of each
//! installed value with the stamp of the run being constructed and refuses a
//! mismatch, so that a run never uses data computed under another registry,
//! other rewrite stages or other option values.
//!
//! Data that did not come from a run of this library has no stamp, and is
//! installed with [`Externals::install_unstamped`]. Nothing then checks that it
//! agrees with the document; the caller is responsible for that.
//!
//! See [external tables and two-run
//! composition](crate::guide::embedding#external-tables-and-two-run-composition)
//! for how an embedding program uses them.
use Any;
use BTreeMap;
use fmt;
use Arc;
use crate;
use crate;
/// A record of what a value produced by a run of this library was produced
/// under: the registry digest, the rewrite stages that ran, and a digest of
/// the option values that those rewrite stages read.
///
/// Two runs with equal stamps read a document the same way: the same registry,
/// the same rewrite stages in the same order, and the same values for the
/// options those rewrite stages read, so they produce the same frozen trees
/// from the same document. A value whose stamp equals a run's own stamp
/// ([`Run::stamp`](crate::core::run::Run::stamp)) is therefore data that the
/// run could have computed itself. Install such a value with
/// [`Externals::install`]; [`Run::new`](crate::core::run::Run::new) refuses it
/// with [`RunError::StampMismatch`](crate::core::run::RunError::StampMismatch)
/// when the stamps differ.
///
/// Its `Display` form lists the three parts, the two digests in hexadecimal.
/// One value installed into one external table, with the value's stamp.
///
/// [`Externals`] holds one `Install` for each table, and
/// [`Run::new`](crate::core::run::Run::new) checks it.
/// The values that a caller installs into a run's external tables, by table
/// name.
///
/// An external table is a table that no stage of the run writes: the caller
/// installs its value when the run is constructed. See the [module
/// documentation](self) for what external tables are for and for the checks
/// that [`Run::new`](crate::core::run::Run::new) performs.
///
/// Create an empty set with [`Externals::none`], add values with
/// [`Externals::install`] (a value produced by a run of this library, with its
/// [`Stamp`]) or [`Externals::install_unstamped`] (any other value), and pass
/// the set to [`Run::new`](crate::core::run::Run::new). Installing twice into
/// one table keeps the later value.
///
/// # Examples
///
/// ```
/// use flm_core::core::run::Externals;
/// use flm_core::core::data::table::{Table, TableName};
///
/// #[derive(Debug, Default)]
/// struct KnownLabels(Vec<String>);
///
/// impl Table for KnownLabels {
/// fn absorb(&mut self, piece: KnownLabels) {
/// self.0.extend(piece.0);
/// }
/// }
///
/// const KNOWN: TableName<KnownLabels> = TableName::new("example.known-labels");
///
/// // Data from outside this library: nothing checks it against the document.
/// let externals = Externals::none()
/// .install_unstamped(KNOWN, KnownLabels(vec!["thm:main".to_string()]));
///
/// assert_eq!(externals.len(), 1);
/// assert!(externals.get("example.known-labels").is_some());
/// ```