Persistent names read by one parser; four misreadings fixed (#5814) (#5966)

* doc: persistent names read as their structure; joint.inspect splits an edge with a blend face first

`persistent_name::{FaceName, EdgeName}` parse a face or edge name once (blends, inner walls,
copies, pieces, numbered pieces, `~k`) and write it back as the same text, bounded against
hostile input. A suffix after a blend's edge naming a later feature belongs to the blend face.

joint.inspect split an edge at its first `|`, so an edge whose first face is a blend
(`F4:blend:F3:+x|F3:+z#[…]|F5:-y`) came back as a cut-off blend name and a string that is no
face. It now uses `EdgeName::parse`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* doc: persistent names as typed values: features, roles, suffixes, a lexer and errors that say where

`FaceName` is a base (made by a feature in a role, a blend and the edge it rounds, an inner
wall, a body's face by index, other text) and its suffixes (copy, piece, numbered piece).
Roles are typed where the vocabulary is known (top, side:<curve>, +x, face:<k>, …) and kept as
written otherwise. The text form is lexed into tokens and read by the grammar in the module's
doc; a name that doesn't read says where and what was expected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* names: read what made a face, its kind and its feature ids through persistent_name

The places that took a name apart by hand to find the feature that made a face, whether it is
a blend, a copy, a numbered piece or a wall, or to remap feature ids on copy, now ask the typed
name (`made_by`, `split_feature`, `is_blend`, `is_copy`, `is_numbered_piece`, `is_wall`,
`features`, `map`). A name that doesn't read falls back to the old text check.

- An offset of a face that only names a blend inside it (`F2:top#[F5:blend:…]`) is no longer
  taken for a fillet's.
- Copying a component remaps feature ids inside a piece's `#[…]` too.
- A shell's inner wall split by a later cut keeps the qualifier on the wall.
- joint.inspect finds the sketch curve of a copied side (`side:l1@F7.2`).

Finding a face by name (strip_piece, parent_piece) is unchanged: it reads a trailing
qualifier as the whole name's and moves once the text form says where it belongs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* engine: regression tests for press-pull on a wall that only names a fillet and joint.inspect on a copied side

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* names: an edge holds one face or more; made_by reads only the leading token

An open boundary's edge has one face and an edge several faces meet at has more, so an edge name
is a list of faces (a blend's own edge still takes two at most, as before). `made_by` reads just
the leading `F<n>:`, which every name a feature made starts with, so asking it of every face
each frame stays cheap. Names are read up to 1 MiB: a face engraved with text has piece names
past 4 KiB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* persistent_name: split into lexer, parser, write, text and tests

No code changed; callers' paths stay the same (`pub use text::*`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
JIJINBEI
2026-10-25 01:48:26 +00:00
committed by GitHub
parent 3f6b9fe89f
commit cdde7d62b2
23 changed files with 1179 additions and 61 deletions
+93
View File
@@ -0,0 +1,93 @@
//! The lexer: a name's text as tokens.
/// A plain decimal number: digits only, no leading zero (so it writes back the same).
pub(super) fn number<T: std::str::FromStr>(d: &str) -> Option<T> {
let ok = !d.is_empty() && d.bytes().all(|b| b.is_ascii_digit()) && (d.len() == 1 || !d.starts_with('0'));
if ok { d.parse().ok() } else { None }
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Kind {
/// `F<n>` before a `:`.
Feature(u64),
/// Text between separators.
Word,
Colon,
Pipe,
Comma,
/// `#[`
Open,
/// `]`
Close,
/// `#<n>`
Hash(u32),
/// `~<n>`
Tie(u32),
/// `@F<n>.<k>`
Copy(u64, u32),
}
#[derive(Clone, Copy, Debug)]
pub(super) struct Tok {
pub(super) kind: Kind,
pub(super) start: usize,
pub(super) end: usize,
}
/// The digits of `s` from byte `i` as a number, and where they end.
fn digits_at<T: std::str::FromStr>(s: &str, i: usize) -> Option<(T, usize)> {
let rest = s.get(i..)?;
let n = rest.bytes().take_while(u8::is_ascii_digit).count();
Some((number(rest.get(..n)?)?, i + n))
}
/// The separator token starting at byte `i`, if one does.
fn separator_at(s: &str, i: usize) -> Option<Tok> {
let b = s.as_bytes();
let tok = |kind, end| Some(Tok { kind, start: i, end });
match b.get(i)? {
b':' => tok(Kind::Colon, i + 1),
b'|' => tok(Kind::Pipe, i + 1),
b',' => tok(Kind::Comma, i + 1),
b']' => tok(Kind::Close, i + 1),
b'#' if b.get(i + 1) == Some(&b'[') => tok(Kind::Open, i + 2),
b'#' => digits_at(s, i + 1).and_then(|(k, end)| tok(Kind::Hash(k), end)),
b'~' => digits_at(s, i + 1).and_then(|(k, end)| tok(Kind::Tie(k), end)),
b'@' if b.get(i + 1) == Some(&b'F') => {
let (feature, dot) = digits_at(s, i + 2)?;
if b.get(dot) != Some(&b'.') {
return None;
}
let (instance, end) = digits_at(s, dot + 1)?;
tok(Kind::Copy(feature, instance), end)
}
_ => None,
}
}
pub(super) fn lex(s: &str) -> Vec<Tok> {
let mut toks = Vec::new();
let mut i = 0;
while i < s.len() {
if let Some(t) = separator_at(s, i) {
toks.push(t);
i = t.end;
continue;
}
// `F<n>` right before a `:` names a feature.
if s.as_bytes().get(i) == Some(&b'F')
&& let Some((n, end)) = digits_at::<u64>(s, i + 1)
&& s.as_bytes().get(end) == Some(&b':')
{
toks.push(Tok { kind: Kind::Feature(n), start: i, end });
i = end;
continue;
}
let start = i;
while i < s.len() && separator_at(s, i).is_none() {
i += s.get(i..).and_then(|r| r.chars().next()).map_or(1, char::len_utf8);
}
toks.push(Tok { kind: Kind::Word, start, end: i });
}
toks
}
+245
View File
@@ -0,0 +1,245 @@
//! The parser: the grammar in the module's doc, by recursive descent.
use super::lexer::*;
use super::*;
impl FaceName {
pub fn parse(s: &str) -> Result<FaceName, NameError> {
let mut p = Parser::new(s)?;
let f = p.face(0)?;
p.end()?;
Ok(f)
}
}
impl EdgeName {
pub fn parse(s: &str) -> Result<EdgeName, NameError> {
let mut p = Parser::new(s)?;
// A `~k` ending the text numbers this edge.
let tie = match p.toks.last() {
Some(Tok { kind: Kind::Tie(k), .. }) => {
let k = *k;
p.toks.pop();
Some(k)
}
_ => None,
};
let e = p.edge(0, usize::MAX)?;
p.end()?;
Ok(EdgeName { tie, ..e })
}
}
impl Role {
fn from_text(t: &str) -> Role {
match t {
"top" => return Role::Top,
"bottom" => return Role::Bottom,
"start" => return Role::Start,
"end" => return Role::End,
"base" => return Role::Base,
"section" => return Role::Section,
"side" => return Role::Wall,
_ => {}
}
if let Some(rest) = t.strip_prefix("side:").filter(|r| !r.is_empty()) {
let profiled =
rest.strip_prefix('s').and_then(|r| r.split_once('.')).and_then(|(k, c)| Some((number(k)?, c))).filter(|(_, c)| !c.is_empty());
return match profiled {
Some((k, c)) => Role::Side { profile: Some(k), curve: c.to_string() },
None => Role::Side { profile: None, curve: rest.to_string() },
};
}
if let Some(k) = t.strip_prefix("face:").and_then(number) {
return Role::Index(k);
}
let mut c = t.chars();
let box_face = match (c.next(), c.next(), c.next()) {
(Some(sign @ ('+' | '-')), Some(a), None) => match a {
'x' => Some(Axis::X),
'y' => Some(Axis::Y),
'z' => Some(Axis::Z),
_ => None,
}
.map(|axis| Role::BoxFace { axis, high: sign == '+' }),
_ => None,
};
box_face.unwrap_or_else(|| Role::Other(t.to_string()))
}
}
/// The trailing suffixes of `inner`, the last name nested in a face of `feature`, that name a
/// later feature, taken off it: they are the outer face's (a later cut split it, a later pattern
/// copied it).
fn later_suffixes(feature: FeatureId, inner: &mut FaceName) -> Vec<Suffix> {
let later = |s: &Suffix| match s {
Suffix::Copy { feature: f, .. } => *f > feature,
Suffix::Piece(q) => q.around.iter().filter_map(FaceName::feature).max().is_some_and(|f| f > feature),
Suffix::Numbered(_) => false,
};
let keep = inner.suffixes.iter().rposition(|s| !later(s)).map_or(0, |i| i + 1);
inner.suffixes.split_off(keep)
}
struct Parser<'a> {
s: &'a str,
toks: Vec<Tok>,
at: usize,
}
impl<'a> Parser<'a> {
fn new(s: &'a str) -> Result<Parser<'a>, NameError> {
if s.len() > MAX_NAME_LEN {
return Err(NameError::TooLong);
}
Ok(Parser { s, toks: lex(s), at: 0 })
}
fn peek(&self, ahead: usize) -> Option<Tok> {
self.toks.get(self.at + ahead).copied()
}
fn byte(&self) -> usize {
self.peek(0).map_or(self.s.len(), |t| t.start)
}
fn expected<T>(&self, what: &'static str) -> Result<T, NameError> {
Err(NameError::Expected { at: self.byte(), what })
}
fn end(&self) -> Result<(), NameError> {
if self.peek(0).is_some() { self.expected("the end of the name") } else { Ok(()) }
}
fn word(&self, ahead: usize) -> Option<&'a str> {
self.peek(ahead).filter(|t| t.kind == Kind::Word).and_then(|t| self.s.get(t.start..t.end))
}
/// `<word>:` at the cursor.
fn keyword(&self, w: &str) -> bool {
self.word(0) == Some(w) && self.peek(1).is_some_and(|t| t.kind == Kind::Colon)
}
/// An edge: its faces joined by `|`, at most `max` of them.
fn edge(&mut self, depth: usize, max: usize) -> Result<EdgeName, NameError> {
let mut faces = vec![self.face(depth + 1)?];
while faces.len() < max && self.peek(0).map(|t| t.kind) == Some(Kind::Pipe) {
self.at += 1;
faces.push(self.face(depth + 1)?);
}
let tie = match self.peek(0).map(|t| t.kind) {
Some(Kind::Tie(k)) => {
self.at += 1;
Some(k)
}
_ => None,
};
Ok(EdgeName { faces, tie })
}
fn face(&mut self, depth: usize) -> Result<FaceName, NameError> {
if depth > MAX_DEPTH {
return Err(NameError::TooDeep);
}
let mut face = match (self.peek(0).map(|t| t.kind), self.peek(1).map(|t| t.kind)) {
(Some(Kind::Feature(n)), Some(Kind::Colon)) => {
self.at += 2;
let feature = FeatureId(n);
if self.keyword("blend") {
self.at += 2;
self.blend(feature, depth)?
} else if self.keyword("inner") {
self.at += 2;
let mut offsets = self.face(depth + 1)?;
let moved = later_suffixes(feature, &mut offsets);
FaceName { base: Base::Inner { feature, offsets: Box::new(offsets) }, suffixes: moved }
} else {
let role = Role::from_text(self.text()?);
FaceName { base: Base::Made { feature, role }, suffixes: Vec::new() }
}
}
_ => {
let t = self.text()?;
let body_face = t.rsplit_once(":face:").and_then(|(b, k)| Some((b, number(k)?))).filter(|(b, _)| !b.is_empty());
let base = match body_face {
Some((body, index)) => Base::BodyFace { body: body.to_string(), index },
None => Base::Other(t.to_string()),
};
FaceName { base, suffixes: Vec::new() }
}
};
self.suffixes(&mut face, depth)?;
Ok(face)
}
/// A blend face of `feature`, the cursor after `blend:`.
fn blend(&mut self, feature: FeatureId, depth: usize) -> Result<FaceName, NameError> {
let ends_face = |k: Option<Kind>| !matches!(k, Some(Kind::Word | Kind::Colon | Kind::Feature(_)));
if let Some(k) = self.word(0).and_then(number::<u32>)
&& ends_face(self.peek(1).map(|t| t.kind))
{
self.at += 1;
return Ok(FaceName { base: Base::Blend { feature, rounds: Rounds::Index(k) }, suffixes: Vec::new() });
}
// A blend names the edge it rounds by its faces, two at most: the rest is the outer edge's.
let mut edge = self.edge(depth + 1, 2)?;
let moved = edge.faces.last_mut().map(|b| later_suffixes(feature, b)).unwrap_or_default();
Ok(FaceName { base: Base::Blend { feature, rounds: Rounds::Edge(Box::new(edge)) }, suffixes: moved })
}
/// Words and colons up to the next other token, as written.
fn text(&mut self) -> Result<&'a str, NameError> {
let start = self.byte();
let mut end = start;
while let Some(t) = self.peek(0).filter(|t| matches!(t.kind, Kind::Word | Kind::Colon | Kind::Feature(_))) {
end = t.end;
self.at += 1;
}
match self.s.get(start..end).filter(|t| !t.is_empty()) {
Some(t) => Ok(t),
None => self.expected("a face"),
}
}
fn suffixes(&mut self, face: &mut FaceName, depth: usize) -> Result<(), NameError> {
while let Some(t) = self.peek(0) {
match t.kind {
Kind::Copy(feature, instance) => {
self.at += 1;
face.suffixes.push(Suffix::Copy { feature: FeatureId(feature), instance });
}
Kind::Open => {
self.at += 1;
let mut around = vec![self.face(depth + 1)?];
loop {
match self.peek(0).map(|t| t.kind) {
Some(Kind::Comma) => {
self.at += 1;
around.push(self.face(depth + 1)?);
}
Some(Kind::Close) => {
self.at += 1;
break;
}
_ => return self.expected("`,` or `]` in a piece's faces"),
}
}
let tie = match self.peek(0).map(|t| t.kind) {
Some(Kind::Hash(k)) => {
self.at += 1;
Some(k)
}
_ => None,
};
face.suffixes.push(Suffix::Piece(Qualifier { around, tie }));
}
Kind::Hash(k) => {
self.at += 1;
face.suffixes.push(Suffix::Numbered(k));
}
_ => return Ok(()),
}
}
Ok(())
}
}
+238
View File
@@ -0,0 +1,238 @@
use super::*;
fn face(s: &str) -> FaceName {
let f = FaceName::parse(s).unwrap_or_else(|e| panic!("{s}: {e}"));
assert_eq!(f.to_string(), s, "writes back");
f
}
fn edge(s: &str) -> EdgeName {
let e = EdgeName::parse(s).unwrap_or_else(|e| panic!("{s}: {e}"));
assert_eq!(e.to_string(), s, "writes back");
e
}
fn made(feature: u64, role: Role) -> FaceName {
FaceName { base: Base::Made { feature: FeatureId(feature), role }, suffixes: Vec::new() }
}
fn other(role: &str) -> Role {
Role::Other(role.into())
}
#[test]
fn roles_are_typed_and_the_rest_kept_as_written() {
assert_eq!(face("F2:top"), made(2, Role::Top));
assert_eq!(face("F4:side#1").stem(), made(4, Role::Wall));
assert_eq!(face("F2:side:l4"), made(2, Role::Side { profile: None, curve: "l4".into() }));
assert_eq!(face("F2:side:s1.l4"), made(2, Role::Side { profile: Some(1), curve: "l4".into() }));
assert_eq!(face("F3:+x"), made(3, Role::BoxFace { axis: Axis::X, high: true }));
assert_eq!(face("F3:-z"), made(3, Role::BoxFace { axis: Axis::Z, high: false }));
assert_eq!(face("F2:face:7"), made(2, Role::Index(7)));
assert_eq!(face("F9:cap:high:0"), made(9, other("cap:high:0")));
assert_eq!(face("F12:boss:side:3"), made(12, other("boss:side:3")));
assert_eq!(face("Body1:face:3").base, Base::BodyFace { body: "Body1".into(), index: 3 });
assert_eq!(face("Plate:rim.end").base, Base::Other("Plate:rim.end".into()));
// Numbers that would not write back the same stay text.
assert_eq!(face("F2:side:s01.l4"), made(2, Role::Side { profile: None, curve: "s01.l4".into() }));
}
#[test]
fn pieces_copies_and_numbered_pieces() {
let p = face("F2:top#[F2:side:l2,F4:side:l2]");
let Some(Suffix::Piece(q)) = p.suffixes.first() else { panic!("{p:?}") };
assert_eq!(q.around().len(), 2);
assert_eq!(p.stem(), made(2, Role::Top));
assert!(matches!(face("F2:top#[F4:side:l2]#1").suffixes.as_slice(), [Suffix::Piece(Qualifier { tie: Some(1), .. })]));
assert_eq!(face("F2:top#2").suffixes, vec![Suffix::Numbered(2)]);
let c = face("F2:side:l1@F7.2");
assert_eq!(c.suffixes, vec![Suffix::Copy { feature: FeatureId(7), instance: 2 }]);
assert_eq!(c.feature(), Some(FeatureId(7)));
assert_eq!(face("F2:side:l1@F7.2#[F8:x]").stem(), c);
assert!(Qualifier::new(Vec::new(), None).is_none());
}
/// The fillet rounds an edge of a piece the slot made before it: the qualifier is the
/// piece's (corpus `naming-split-pieces`).
#[test]
fn a_blend_of_an_earlier_piece_keeps_the_qualifier_inside() {
let f = face("F5:blend:F2:side:l2|F2:top#[F2:side:l2,F4:side:l2]");
let Base::Blend { feature: FeatureId(5), rounds: Rounds::Edge(e) } = &f.base else { panic!("{f:?}") };
assert!(f.suffixes.is_empty());
assert_eq!(e.faces[1].stem(), made(2, Role::Top));
assert_eq!(e.faces[1].suffixes.len(), 1);
}
/// A slot cut after the fillet splits the fillet face: the qualifier, naming the later slot,
/// is the fillet face's.
#[test]
fn a_blend_split_by_a_later_cut_takes_the_qualifier() {
let f = face("F4:blend:F3:+x|F3:+z#[F3:-y,F5:-y]");
let Base::Blend { feature: FeatureId(4), rounds: Rounds::Edge(e) } = &f.base else { panic!("{f:?}") };
assert_eq!(e.faces[1], made(3, Role::BoxFace { axis: Axis::Z, high: true }));
assert!(matches!(f.suffixes.as_slice(), [Suffix::Piece(_)]));
// Copied by a later pattern, then split: both are the blend face's, in order.
let g = face("F4:blend:F3:+x|F3:+z@F6.1#[F7:a]");
assert!(matches!(g.suffixes.as_slice(), [Suffix::Copy { feature: FeatureId(6), .. }, Suffix::Piece(_)]));
assert_eq!(g.feature(), Some(FeatureId(6)));
}
/// What the text cannot tell: a later cut told apart only by older faces reads as a piece of
/// the inner face.
#[test]
fn a_later_cut_told_apart_by_older_faces_reads_as_the_inner_faces() {
let f = face("F4:blend:F3:+x|F3:+z#[F3:-y]");
let Base::Blend { rounds: Rounds::Edge(e), .. } = &f.base else { panic!("{f:?}") };
assert!(f.suffixes.is_empty() && e.faces[1].suffixes.len() == 1);
}
#[test]
fn edges_split_into_their_two_faces() {
let e = edge("F2:side:l1|F2:side:l2");
assert_eq!(e.faces[0], made(2, Role::Side { profile: None, curve: "l1".into() }));
assert_eq!(edge("F2:side:l1|F2:top~1").tie, Some(1));
// The blend face first: the whole blend name, then the slot's wall.
let e = edge("F4:blend:F3:+x|F3:+z#[F3:-y,F5:-y]|F5:-y");
assert_eq!(e.faces[0].to_string(), "F4:blend:F3:+x|F3:+z#[F3:-y,F5:-y]");
assert_eq!(e.faces[1], made(5, Role::BoxFace { axis: Axis::Y, high: false }));
// The blend face second.
let e = edge("F2:side:l1|F5:blend:F2:side:l2|F2:top#[F2:side:l2,F4:side:l2]");
assert!(matches!(e.faces[1].base, Base::Blend { feature: FeatureId(5), .. }));
// A blend of a blend's edge.
let e = edge("F6:blend:F4:blend:a|b|c|d");
let Base::Blend { rounds: Rounds::Edge(inner), .. } = &e.faces[0].base else { panic!("{e:?}") };
assert!(matches!(inner.faces[0].base, Base::Blend { feature: FeatureId(4), .. }));
assert_eq!(e.faces[1].base, Base::Other("d".into()));
// A `~k` ending the edge numbers it, not the blend's edge.
let e = edge("F2:x|F5:blend:a|b~1");
assert_eq!(e.tie, Some(1));
assert!(matches!(&e.faces[1].base, Base::Blend { rounds: Rounds::Edge(b), .. } if b.tie.is_none()));
// A blend named by its edge's index.
assert_eq!(edge("F5:blend:3|F2:top").faces[0].base, Base::Blend { feature: FeatureId(5), rounds: Rounds::Index(3) });
}
#[test]
fn inner_walls_nest_a_face() {
let f = face("F3:inner:F2:top#[F2:side:l2]");
let Base::Inner { feature: FeatureId(3), offsets } = &f.base else { panic!("{f:?}") };
assert_eq!(offsets.stem(), made(2, Role::Top));
}
#[test]
fn what_made_a_face_and_its_kind() {
assert_eq!(made_by("F2:side:l1@F7.2"), Some(2));
assert_eq!(made_by("F3:inner:F5:blend:a|b"), Some(3));
assert_eq!(made_by("Body1:face:3"), None);
assert_eq!(split_feature("F12:boss:side:3"), Some((12, "boss:side:3")));
// A blend named only inside another name is not one.
assert!(is_blend("F5:blend:F2:x|F2:y#[F6:z]"));
assert!(!is_blend("F2:top#[F5:blend:a|b]"));
assert!(!is_blend("F3:inner:F5:blend:a|b"));
// A copy only inside a blend's edge doesn't make the blend face a copy.
assert!(is_copy("F2:side:l1@F7.2#[F8:x]"));
assert!(!is_copy("F5:blend:F2:x@F3.1|F2:y"));
assert!(is_numbered_piece("F2:top#2") && is_numbered_piece("F2:top#[F4:x]#1"));
assert!(!is_numbered_piece("F5:blend:F2:top#2|F2:x") && !is_numbered_piece("F2:top#[F4:x]"));
assert!(is_wall("F4:side") && !is_wall("F4:side#1") && !is_wall("F5:blend:F2:x|F4:side"));
}
/// A shell's inner wall split by a later cut: the qualifier, naming the cut, is the wall's.
#[test]
fn an_inner_wall_split_by_a_later_cut_takes_the_qualifier() {
let f = face("F3:inner:F2:top#[F2:side:l2,F6:x]");
assert!(matches!(f.suffixes.as_slice(), [Suffix::Piece(_)]));
assert_eq!(f.stem().to_string(), "F3:inner:F2:top");
}
#[test]
fn features_in_text_order() {
let ids = |s: &str| face(s).features().into_iter().map(|f| f.0).collect::<Vec<_>>();
assert_eq!(ids("F3:inner:F2:blend:F1:+y|F1:-x"), vec![3, 2, 1, 1]);
assert_eq!(ids("F82:-z@F84.2"), vec![82, 84]);
assert_eq!(ids("F2:top#[F2:side:l2,F4:side:l2]"), vec![2, 2, 4]);
assert_eq!(ids("F1:side:曲線F10"), vec![1]);
assert!(ids("Body1:face:3").is_empty());
}
#[test]
fn feature_ids_map_everywhere_but_role_text() {
let ids = std::collections::BTreeMap::from([(2, 12), (5, 15)]);
let mapped = map_features("F5:blend:F2:side:l2|F2:top#[F2:side:l2,F4:side:l2]@F2.1", &ids);
assert_eq!(mapped.as_deref(), Some("F15:blend:F12:side:l2|F12:top#[F12:side:l2,F4:side:l2]@F12.1"));
// Where an id sits: a name's ids must map, a qualifier's may stay.
let f = face("F2:top#[F9:x]");
let strict = f.map(
&mut |id, place| match place {
Place::Name => ids.get(&id.0).map(|n| FeatureId(*n)),
Place::Qualifier => Some(ids.get(&id.0).map_or(id, |n| FeatureId(*n))),
},
&mut |_| None,
);
assert_eq!(strict.map(|f| f.to_string()).as_deref(), Some("F12:top#[F9:x]"));
assert!(face("F9:top").map(&mut |id, _| ids.get(&id.0).map(|n| FeatureId(*n)), &mut |_| None).is_none());
let body = face("Body1:face:3").map(&mut |id, _| Some(id), &mut |b| (b == "Body1").then(|| "Body2".to_string()));
assert_eq!(body.map(|f| f.to_string()).as_deref(), Some("Body2:face:3"));
}
#[test]
fn errors_say_where() {
assert_eq!(EdgeName::parse("F2:top|"), Err(NameError::Expected { at: 7, what: "a face" }));
assert_eq!(FaceName::parse("F2:top#[F3:x"), Err(NameError::Expected { at: 12, what: "`,` or `]` in a piece's faces" }));
assert_eq!(FaceName::parse("a|b"), Err(NameError::Expected { at: 1, what: "the end of the name" }));
assert_eq!(FaceName::parse(""), Err(NameError::Expected { at: 0, what: "a face" }));
}
#[test]
fn hostile_names_never_panic() {
let long = "x".repeat(MAX_NAME_LEN + 1);
let deep = format!("{}x", "F1:inner:".repeat(64));
let deep_pieces = format!("x{}{}", "#[x".repeat(64), "]".repeat(64));
for s in [
"",
"|",
"||",
"#[",
"#[]",
"x#[",
"x#[]",
"x#[y",
"x#[y,",
"]",
"x]",
"F1:blend:",
"F1:blend:x",
"F1:blend:x|",
"@F",
"x@F1.",
"x@F",
"x~",
"x|y~",
"x|y~01",
"F01:top",
"x#0x",
"F99999999999999999999999:top",
"x#99999999999",
"\u{1F600}|\u{1F601}",
long.as_str(),
deep.as_str(),
deep_pieces.as_str(),
] {
if let Ok(f) = FaceName::parse(s) {
assert_eq!(f.to_string(), s);
}
if let Ok(e) = EdgeName::parse(s) {
assert_eq!(e.to_string(), s);
}
}
assert_eq!(FaceName::parse(&long), Err(NameError::TooLong));
assert_eq!(FaceName::parse(&deep), Err(NameError::TooDeep));
assert_eq!(FaceName::parse(&deep_pieces), Err(NameError::TooDeep));
// An open boundary's edge has one face; an edge three faces meet at, three.
assert_eq!(edge("F2:top").faces.len(), 1);
assert_eq!(edge("x|y|z").faces.len(), 3);
// A blend's edge has two faces at most; a third is the outer edge's.
let e = edge("F5:blend:a|b|c");
assert!(matches!(&e.faces[0].base, Base::Blend { rounds: Rounds::Edge(inner), .. } if inner.faces.len() == 2) && e.faces.len() == 2);
// A fillet on an open boundary's edge.
assert!(matches!(face("F1:blend:F10:start").base, Base::Blend { rounds: Rounds::Edge(ref e), .. } if e.faces.len() == 1));
}
+53
View File
@@ -0,0 +1,53 @@
//! Questions callers ask of a name kept as a `String`.
use super::lexer::number;
use super::*;
/// The feature whose `F<n>:` starts the face name `name` (a copy's is its source's). Every name a
/// feature made starts so (`F2:top`, `F5:blend:…`, `F3:inner:…`), so only that token is read:
/// cheap enough to ask of every face each frame.
pub fn made_by(name: &str) -> Option<u64> {
let rest = name.strip_prefix('F')?;
let end = rest.find(|c: char| !c.is_ascii_digit())?;
if rest.get(end..end + 1) != Some(":") {
return None;
}
number(rest.get(..end)?)
}
/// `name` without the `F<n>:` of the feature that made it, and `n`.
pub fn split_feature(name: &str) -> Option<(u64, &str)> {
let id = made_by(name)?;
Some((id, name.strip_prefix(&format!("F{id}:"))?))
}
/// Whether `name` is a pattern's or mirror's copy of a face.
pub fn is_copy(name: &str) -> bool {
FaceName::parse(name).map_or_else(|_| name.contains('@'), |f| f.is_copy())
}
/// Whether `name` is a fillet's or chamfer's face.
pub fn is_blend(name: &str) -> bool {
FaceName::parse(name).map_or_else(|_| name.contains(":blend:"), |f| f.is_blend())
}
/// Whether `name` is a piece told apart by a number (an older design's piece, or a tie).
pub fn is_numbered_piece(name: &str) -> bool {
FaceName::parse(name).map_or_else(
|_| name.match_indices('#').any(|(i, _)| name.get(i + 1..).is_some_and(|rest| rest.starts_with(|c: char| c.is_ascii_digit()))),
|f| f.is_numbered_piece(),
)
}
/// Whether `name` is a whole `side` face: a cylinder's wall, or a tool's faces swept from its
/// whole profile.
pub fn is_wall(name: &str) -> bool {
FaceName::parse(name).map_or_else(|_| name.ends_with(":side"), |f| f.suffixes.is_empty() && matches!(f.base, Base::Made { role: Role::Wall, .. }))
}
/// `name` with every feature id `ids` maps replaced, the rest kept; `None` when it doesn't read.
pub fn map_features(name: &str, ids: &std::collections::BTreeMap<u64, u64>) -> Option<String> {
let f = FaceName::parse(name).ok()?;
let mapped = f.map(&mut |id, _| Some(ids.get(&id.0).map_or(id, |n| FeatureId(*n))), &mut |_| None)?;
Some(mapped.to_string())
}
+91
View File
@@ -0,0 +1,91 @@
//! Writing names back as text: `Display`.
use std::fmt;
use super::*;
impl fmt::Display for Role {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Role::Top => f.write_str("top"),
Role::Bottom => f.write_str("bottom"),
Role::Start => f.write_str("start"),
Role::End => f.write_str("end"),
Role::Base => f.write_str("base"),
Role::Section => f.write_str("section"),
Role::Wall => f.write_str("side"),
Role::Side { profile: Some(k), curve } => write!(f, "side:s{k}.{curve}"),
Role::Side { profile: None, curve } => write!(f, "side:{curve}"),
Role::BoxFace { axis, high } => {
let a = match axis {
Axis::X => 'x',
Axis::Y => 'y',
Axis::Z => 'z',
};
write!(f, "{}{a}", if *high { '+' } else { '-' })
}
Role::Index(k) => write!(f, "face:{k}"),
Role::Other(t) => f.write_str(t),
}
}
}
impl fmt::Display for FaceName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.base {
Base::Made { feature, role } => write!(f, "F{}:{role}", feature.0)?,
Base::Blend { feature, rounds: Rounds::Edge(e) } => write!(f, "F{}:blend:{e}", feature.0)?,
Base::Blend { feature, rounds: Rounds::Index(k) } => write!(f, "F{}:blend:{k}", feature.0)?,
Base::Inner { feature, offsets } => write!(f, "F{}:inner:{offsets}", feature.0)?,
Base::BodyFace { body, index } => write!(f, "{body}:face:{index}")?,
Base::Other(t) => f.write_str(t)?,
}
for s in &self.suffixes {
match s {
Suffix::Copy { feature, instance } => write!(f, "@F{}.{instance}", feature.0)?,
Suffix::Piece(q) => {
f.write_str("#[")?;
for (i, a) in q.around.iter().enumerate() {
if i > 0 {
f.write_str(",")?;
}
write!(f, "{a}")?;
}
f.write_str("]")?;
if let Some(k) = q.tie {
write!(f, "#{k}")?;
}
}
Suffix::Numbered(k) => write!(f, "#{k}")?,
}
}
Ok(())
}
}
impl fmt::Display for EdgeName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, face) in self.faces.iter().enumerate() {
if i > 0 {
f.write_str("|")?;
}
write!(f, "{face}")?;
}
match self.tie {
Some(k) => write!(f, "~{k}"),
None => Ok(()),
}
}
}
impl fmt::Display for NameError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NameError::TooLong => write!(f, "a name is at most {MAX_NAME_LEN} bytes"),
NameError::TooDeep => write!(f, "a name nests at most {MAX_DEPTH} deep"),
NameError::Expected { at, what } => write!(f, "expected {what} at byte {at}"),
}
}
}
impl std::error::Error for NameError {}