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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//! Plan items as data (the editor thin slice's stream S2): each item's typed [`State`], the
//! edit that fixes a problem ([`Op`]), and one label formatter ([`items`], [`label`]), so a
//! caller (the editor, an MCP client) never reads an item's state from its `basis` sentence or
//! formats its text again. The `basis` stays, for a person (ADR R1 invariant 4).
//!
//! Every plan item carries its state: on the sheet ([`State::Shown`]); left out by the spec
//! ([`State::OmittedByUser`], [`State::NotAuthored`]); stated by another item
//! ([`State::Covered`]); or kept in the plan undrawn, with the code of the problem that says why
//! ([`State::NotDrawable`]: R1 invariant 3). Each is set where the plan decides it.
use serde::{Deserialize, Serialize};
use crate::model::Plan;
/// What became of a plan item.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
pub enum State {
/// Drawn on the sheet.
#[default]
Shown,
/// Left out by the spec (`dimensions/<id>/omit`), or with a member of its correlated set
/// that the spec omits (ARCH0015-2: the problem `correlated_set_whole` names it).
OmittedByUser,
/// No drawing requirement authored, or left out of an authored set
/// (`dimension_source: authored`) by not being listed.
NotAuthored,
/// Not drawn because another item states what it would (one measurement, one owner): `by`
/// is that item's ID.
Covered { by: String },
/// Kept in the plan but not drawn; `reason_code` is the code of the problem on its ID that
/// says why (`view_plan_incomplete`, `callout_dropped`, `pmi_not_drawn`, …).
NotDrawable { reason_code: String },
}
impl State {
/// [`State::NotDrawable`] for the problem *code*.
pub fn not_drawable(code: &str) -> State {
State::NotDrawable {
reason_code: code.into(),
}
}
/// An item drawn when *placed*, else not drawable for the problem *code*.
pub fn placed_or(placed: bool, code: &str) -> State {
if placed {
State::Shown
} else {
State::not_drawable(code)
}
}
}
/// One edit operation, as [`crate::model::edit`] takes it: a problem's `fix` is a list of them,
/// applied together.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Op {
/// `set`, `remove` or `add`.
pub op: String,
/// The path into the spec, a `/` inside a key written `~1` ([`key`]).
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub value: Option<serde_json::Value>,
}
impl Op {
/// Append an authored declaration, note or table through the common Spec editor.
pub fn add(path: impl Into<String>, value: serde_json::Value) -> Op {
Op {
op: "add".into(),
path: path.into(),
value: Some(value),
}
}
pub fn remove(path: impl Into<String>) -> Op {
Op {
op: "remove".into(),
path: path.into(),
value: None,
}
}
pub fn set(path: impl Into<String>, value: serde_json::Value) -> Op {
Op {
op: "set".into(),
path: path.into(),
value: Some(value),
}
}
}
/// *id* as one key of an edit path: `~` written `~0` and `/` written `~1` (RFC 6901).
pub fn key(id: &str) -> String {
id.replace('~', "~0").replace('/', "~1")
}
/// A plan item, as a caller lists it.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(from = "ItemWire", into = "ItemWire")]
pub struct Item {
pub sheet: u32,
pub(crate) explicit_sheet: bool,
pub id: String,
/// `sheet`, `view`, `dimension`, `control_frame`, `datum_feature`, `label`, `requirement`,
/// `note`, `caption`, `table`, `hole_table`, `hole_table_row`, `balloon` or
/// `characteristic_balloon`.
pub kind: String,
/// The view that carries it, where it has one.
#[serde(skip_serializing_if = "Option::is_none")]
pub view: Option<String>,
pub state: State,
/// The text draftwright draws for it, where it draws one line of text: a dimension's or
/// callout's label (an omitted one's as it would print), a frame's compartments joined by
/// ` | `, a datum's letter, a PMI label's text, a requirement's text, a note's rows (joined by
/// a line break), a balloon's tag.
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
/// All file-owned characteristics stated by this item, in source order.
pub characteristic_numbers: Vec<String>,
}
// Ownership is explicit on every multi-sheet item, including sheet 1. The optional wire
// field preserves the old single-sheet bytes and remembers that distinction on replay.
#[derive(Serialize, Deserialize)]
struct ItemWire {
#[serde(
default,
deserialize_with = "read_sheet",
skip_serializing_if = "Option::is_none"
)]
sheet: Option<u32>,
id: String,
kind: String,
#[serde(skip_serializing_if = "Option::is_none")]
view: Option<String>,
state: State,
#[serde(skip_serializing_if = "Option::is_none")]
label: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
characteristic_numbers: Vec<String>,
}
fn read_sheet<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<u32>, D::Error> {
u32::deserialize(d).map(Some)
}
impl From<Item> for ItemWire {
fn from(i: Item) -> Self {
Self {
sheet: (i.explicit_sheet || i.sheet != 1).then_some(i.sheet),
id: i.id,
kind: i.kind,
view: i.view,
state: i.state,
label: i.label,
characteristic_numbers: i.characteristic_numbers,
}
}
}
impl From<ItemWire> for Item {
fn from(i: ItemWire) -> Self {
Self {
sheet: i.sheet.unwrap_or(1),
explicit_sheet: i.sheet.is_some(),
id: i.id,
kind: i.kind,
view: i.view,
state: i.state,
label: i.label,
characteristic_numbers: i.characteristic_numbers,
}
}
}
/// A feature control frame's compartments as one line.
pub fn frame_label(cells: &[String]) -> String {
cells.join(" | ")
}
/// Every item of *plan*, once each by its ID, in the plan's order: the sheet (ID `sheet`), the
/// views, the dimensions (drawn, then omitted), the GD&T items, the drawing-wide requirements
/// (one carried by a general note is listed as that note), the notes, the tables, the hole table
/// and the balloons.
pub fn items(plan: &Plan) -> Vec<Item> {
let item =
|id: &str, kind: &str, view: Option<&str>, state: &State, label: Option<String>| Item {
explicit_sheet: !plan.sheets.is_empty(),
sheet: view
.and_then(|id| plan.views.iter().find(|v| v.id == id))
.map_or(1, |v| v.sheet),
id: id.into(),
kind: kind.into(),
view: view.map(String::from),
state: state.clone(),
label,
characteristic_numbers: plan
.characteristics
.iter()
.filter(|c| {
c.item.as_deref() == Some(id)
|| c.drawn_item.as_deref() == Some(id)
|| plan
.inspection_balloons
.iter()
.any(|b| b.source == c.source && b.item == id)
})
.map(|c| c.number.clone())
.collect(),
};
let mut out = vec![item("sheet", "sheet", None, &plan.sheet.state, None)];
for sheet in &plan.sheets {
let mut i = item(
&format!("sheet:{}", sheet.number),
"sheet",
None,
&sheet.state,
None,
);
i.sheet = sheet.number;
out.push(i);
}
for v in &plan.views {
let mut i = item(&v.id, "view", None, &v.state, None);
i.sheet = v.sheet;
out.push(i);
}
for d in &plan.dimensions {
let label = Some(d.label.clone());
out.push(item(&d.id, "dimension", Some(&d.view), &d.state, label));
}
for o in &plan.omitted {
let label = Some(o.label.clone());
out.push(item(&o.id, "dimension", None, &o.state, label));
}
// A feature note has two physical carriers (block text and flag), but one
// editable requirement ID. Its note inventory entry represents both.
for g in plan
.gdt
.iter()
.filter(|g| !plan.notes.iter().any(|n| n.id == g.id))
{
let label = Some(g.label.clone());
out.push(item(&g.id, &g.kind, Some(&g.view), &g.state, label));
}
for r in plan.requirements.iter().filter(|r| r.carrier != "notes") {
let label = Some(r.text.clone());
out.push(item(&r.id, "requirement", None, &r.state, label));
}
for c in &plan.captions {
let mut i = item(
&c.id,
"caption",
Some(&c.view),
&c.state,
Some(c.text.clone()),
);
i.sheet = c.sheet;
out.push(i);
}
for n in &plan.notes {
let label = Some(n.rows.join("\n"));
let view = plan
.gdt
.iter()
.find(|g| g.id == n.id && g.placed.is_some())
.map(|g| g.view.as_str());
out.push(item(&n.id, "note", view, &n.state, label));
}
for t in &plan.tables {
let mut i = item(&t.id, "table", None, &t.state, None);
i.sheet = t.sheet;
out.push(i);
}
if let Some(t) = &plan.radius_table {
out.push(item(&t.table.id, "table", None, &t.table.state, None));
}
for t in plan.hole_table.iter().chain(&plan.hole_tables) {
let mut i = item(
&t.id,
"hole_table",
Some(t.view.as_deref().unwrap_or("v:plan")),
&t.state,
None,
);
i.sheet = t.sheet;
out.push(i);
}
for b in &plan.balloons {
let label = Some(b.tag.clone());
out.push(item(&b.id, "balloon", Some(&b.view), &b.state, label));
}
for id in plan
.characteristics
.iter()
.filter_map(|c| c.drawn_item.as_ref())
.chain(plan.inspection_balloons.iter().map(|b| &b.item))
{
if !out.iter().any(|i| &i.id == id)
&& let Some((t, r)) = plan
.hole_table
.iter()
.chain(&plan.hole_tables)
.find_map(|t| {
t.rows
.iter()
.find(|r| *id == format!("{}/{}", t.id, r.key()))
.map(|r| (t, r))
})
{
let mut i = item(
id,
"hole_table_row",
t.view.as_deref(),
&State::Shown,
Some(r.cells.join(" | ")),
);
i.sheet = t.sheet;
out.push(i);
}
}
for b in &plan.inspection_balloons {
let mut i = item(
&b.id,
"characteristic_balloon",
b.view.as_deref(),
&State::Shown,
Some(b.number.clone()),
);
i.sheet = b.sheet;
out.push(i);
}
for c in &plan.characteristics {
for id in &c.balloon_ids {
if out.iter().any(|i| &i.id == id) {
continue;
}
let suppressed = plan
.spec
.balloons
.items
.get(&c.source)
.or_else(|| {
c.item
.as_ref()
.and_then(|i| plan.spec.balloons.items.get(i))
})
.is_some_and(|s| s.suppress);
let state = if suppressed {
State::OmittedByUser
} else {
State::not_drawable("characteristic_balloon_missing")
};
let mut i = item(
id,
"characteristic_balloon",
c.view.as_deref(),
&state,
Some(c.number.clone()),
);
i.sheet = c.sheet.unwrap_or(1);
out.push(i);
}
}
out
}
/// The text draftwright draws for the item *id* of *plan* ([`Item::label`]); none for an item
/// that draws no one line of text, or an ID the plan does not have.
pub fn label(plan: &Plan, id: &str) -> Option<String> {
items(plan).into_iter().find(|i| i.id == id)?.label
}