indicatrix-editor 0.7.2

GUI-free faceting-design editor logic shared by the desktop editor and the wasm web app: the EditorSession (design, undo/redo with caller-supplied coalescing timestamps, selection, dirty tracking), tier/preform/material form parsing, plain-struct view models, the cutting sheet, Retarget, the guided walkthrough and the solve policy. No GUI toolkit, no threads, no clock, no filesystem.
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! The tier table's "Generate steps" form: parsing and validating its numeric fields.

use super::number_expr::{NumberExprError, eval_number};
use indicatrix::geometry::meet_solver::MeetConstraint;
use indicatrix_cut_core::ConstraintTier;
use std::fmt;

/// The steepest angle magnitude a tier can have: angles are measured from the girdle
/// plane, so no crown or pavilion facet is steeper than a right angle.
const MAX_ANGLE_DEG: f64 = 90.0;

/// Why the "Generate steps" form was refused.
///
/// Its [`fmt::Display`] text is the message to show the cutter, and it converts
/// into a `String` of that text for callers that only display it.
#[derive(Debug, Clone, PartialEq)]
pub enum StepSeriesError {
    /// The start angle field is not a number.
    StartNotANumber(String),
    /// The angle step field is not a number.
    StepNotANumber(String),
    /// The start angle or the angle step is infinite or NaN.
    NotFinite,
    /// The tier count is below 1 or above [`ConstraintTier::MAX_STEP_SERIES`].
    CountOutOfRange {
        /// The count that was asked for.
        count: i32,
    },
    /// A generated tier's angle is 0 degrees or steeper than 90 degrees (either side).
    AngleOutOfRange {
        /// The 1-based position of the offending tier in the ladder.
        tier: usize,
        /// Its angle, in degrees.
        angle_deg: f64,
    },
    /// A generated tier's angle falls on the other side of the girdle from the start
    /// angle, which would put the ladder across the crown/pavilion boundary.
    CrossesBlock {
        /// The 1-based position of the offending tier in the ladder.
        tier: usize,
        /// Its angle, in degrees.
        angle_deg: f64,
    },
    /// The anchor field is not a number.
    AnchorNotANumber(String),
    /// The anchor is infinite or NaN.
    AnchorNotFinite,
    /// A field holds a calculation (`40 + 2`) that cannot be read or done.
    CannotCalculate {
        /// The field's name as the message shows it ("Start angle", "Angle step",
        /// "Anchor").
        field: &'static str,
        /// What was typed, trimmed.
        text: String,
        /// Why it cannot be done, as a phrase without a final full stop.
        reason: String,
    },
}

impl fmt::Display for StepSeriesError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::StartNotANumber(text) => write!(f, "Start angle '{text}' is not a number."),
            Self::StepNotANumber(text) => write!(f, "Angle step '{text}' is not a number."),
            Self::NotFinite => f.write_str("Start angle and angle step must be finite numbers."),
            Self::CountOutOfRange { count } => write!(
                f,
                "Tier count {count} is out of range -- enter a whole number from 1 to {}.",
                ConstraintTier::MAX_STEP_SERIES
            ),
            // Angles in a message are magnitudes: the side of the girdle is the block's, never
            // a sign (the stored `angle_deg` stays signed).
            Self::AngleOutOfRange { tier, angle_deg } => write!(
                f,
                "Tier {tier} of the ladder would sit at {:.2}\u{b0} -- every generated \
                 angle must be more than 0\u{b0} and at most {MAX_ANGLE_DEG}\u{b0} from the \
                 girdle plane.",
                angle_deg.abs()
            ),
            Self::CrossesBlock { tier, angle_deg } => write!(
                f,
                "Tier {tier} of the ladder would cross the girdle (it would sit at {:.2}\u{b0} \
                 on the other side of the start angle's block) -- a ladder stays within one \
                 block (crown or pavilion).",
                angle_deg.abs()
            ),
            Self::AnchorNotANumber(text) => write!(f, "Anchor '{text}' is not a number."),
            Self::AnchorNotFinite => f.write_str("Anchor must be a finite number."),
            Self::CannotCalculate {
                field,
                text,
                reason,
            } => write!(f, "{field} '{text}' cannot be calculated: {reason}."),
        }
    }
}

impl std::error::Error for StepSeriesError {}

impl From<StepSeriesError> for String {
    fn from(error: StepSeriesError) -> Self {
        error.to_string()
    }
}

/// The "Generate steps" form's parsing half.
///
/// Everything the form needs turned into real
/// values BEFORE `ConstraintTier::step_series` can be called, except the indices list,
/// which the caller parses separately via [`super::parse_index_list`] (that parse also
/// needs the design's own `gear_teeth_abs`, not available here). An empty anchor field
/// means [`MeetConstraint::MeetExisting`] (the same "blank means use whatever's already
/// anchored" convention -- the caller is relying on an anchor elsewhere in the design); a
/// non-empty one is parsed and pinned via [`MeetConstraint::ScaleReference`].
///
/// The ladder it describes must be one a cutter can actually cut: `count` is from 1 to
/// [`ConstraintTier::MAX_STEP_SERIES`], and every generated angle (`start_angle` plus
/// `n * angle_step`, the way `ConstraintTier::step_series` computes it) has a magnitude
/// above 0 and at most 90 degrees and stays on the start angle's side of the girdle --
/// crown (positive) or pavilion (negative), never across.
///
/// Moved here from the desktop's `tier_actions/tier_generation.rs` so the web
/// app's step generator parses identically.
///
/// # Errors
///
/// A [`StepSeriesError`] naming the offending field or tier, ready to show the cutter.
pub fn parse_step_series_form(
    start_angle_text: &str,
    angle_step_text: &str,
    count: i32,
    anchor_text: &str,
) -> Result<(f64, f64, usize, MeetConstraint), StepSeriesError> {
    let start_angle = read_field(
        "Start angle",
        start_angle_text,
        StepSeriesError::StartNotANumber,
    )?;
    let angle_step = read_field(
        "Angle step",
        angle_step_text,
        StepSeriesError::StepNotANumber,
    )?;
    if !start_angle.is_finite() || !angle_step.is_finite() {
        return Err(StepSeriesError::NotFinite);
    }
    let ladder_len = usize::try_from(count)
        .ok()
        .filter(|&len| (1..=ConstraintTier::MAX_STEP_SERIES).contains(&len))
        .ok_or(StepSeriesError::CountOutOfRange { count })?;
    let anchor_text = anchor_text.trim();
    let first_constraint = if anchor_text.is_empty() {
        MeetConstraint::MeetExisting
    } else {
        let anchor = read_field("Anchor", anchor_text, StepSeriesError::AnchorNotANumber)?;
        if !anchor.is_finite() {
            return Err(StepSeriesError::AnchorNotFinite);
        }
        MeetConstraint::ScaleReference(anchor)
    };
    check_ladder_angles(start_angle, angle_step, ladder_len)?;
    Ok((start_angle, angle_step, ladder_len, first_constraint))
}

/// Reads one numeric field of the form, which may be arithmetic (`40 + 2`). Text that
/// is not a number and holds no calculation becomes the field's historical
/// "not a number" error (`not_a_number`); a calculation that cannot be done becomes
/// [`StepSeriesError::CannotCalculate`].
fn read_field(
    label: &'static str,
    text: &str,
    not_a_number: fn(String) -> StepSeriesError,
) -> Result<f64, StepSeriesError> {
    eval_number(text, None).map_err(|error| match error {
        NumberExprError::NotANumber => not_a_number(text.to_string()),
        NumberExprError::Invalid(reason) => StepSeriesError::CannotCalculate {
            field: label,
            text: text.trim().to_string(),
            reason,
        },
    })
}

/// Checks every angle of a `len`-tier ladder from `start_angle` by `angle_step`: each
/// within (0, 90] degrees of magnitude and on the start angle's side of the girdle.
fn check_ladder_angles(
    start_angle: f64,
    angle_step: f64,
    len: usize,
) -> Result<(), StepSeriesError> {
    let start_is_pavilion = start_angle.is_sign_negative();
    for n in 0..len {
        let angle_deg = angle_step.mul_add(n as f64, start_angle);
        let magnitude = angle_deg.abs();
        let within_range = magnitude > 0.0 && magnitude <= MAX_ANGLE_DEG;
        if !within_range {
            return Err(StepSeriesError::AngleOutOfRange {
                tier: n + 1,
                angle_deg,
            });
        }
        if angle_deg.is_sign_negative() != start_is_pavilion {
            return Err(StepSeriesError::CrossesBlock {
                tier: n + 1,
                angle_deg,
            });
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_blank_anchor_means_meet_existing() {
        let (start, step, count, constraint) =
            parse_step_series_form("40", " 0.5 ", 3, "  ").expect("parses");
        assert_eq!(start.to_bits(), 40.0_f64.to_bits());
        assert_eq!(step.to_bits(), 0.5_f64.to_bits());
        assert_eq!(count, 3);
        assert_eq!(constraint, MeetConstraint::MeetExisting);
    }

    #[test]
    fn an_anchor_pins_the_first_tier() {
        let (_, _, _, constraint) = parse_step_series_form("30", "1", 1, "0.32").expect("parses");
        assert_eq!(constraint, MeetConstraint::ScaleReference(0.32));
    }

    #[test]
    fn each_bad_field_is_named() {
        assert_eq!(
            parse_step_series_form("x", "1", 1, "").unwrap_err(),
            StepSeriesError::StartNotANumber("x".to_string())
        );
        assert!(
            parse_step_series_form("x", "1", 1, "")
                .unwrap_err()
                .to_string()
                .starts_with("Start angle 'x'")
        );
        assert_eq!(
            parse_step_series_form("30", "y", 1, "").unwrap_err(),
            StepSeriesError::StepNotANumber("y".to_string())
        );
        assert_eq!(
            parse_step_series_form("30", "1", 1, "z").unwrap_err(),
            StepSeriesError::AnchorNotANumber("z".to_string())
        );
        assert_eq!(
            parse_step_series_form("inf", "1", 1, "").unwrap_err(),
            StepSeriesError::NotFinite
        );
        assert_eq!(
            parse_step_series_form("30", "1", 1, "inf").unwrap_err(),
            StepSeriesError::AnchorNotFinite
        );
    }

    #[test]
    fn the_count_is_held_to_one_through_the_cap() {
        let cap = i32::try_from(ConstraintTier::MAX_STEP_SERIES).expect("the cap fits an i32");
        assert!(parse_step_series_form("10", "0.5", cap, "").is_ok());
        for bad in [0, -1, cap + 1, i32::MAX, i32::MIN] {
            assert_eq!(
                parse_step_series_form("10", "0.5", bad, "").unwrap_err(),
                StepSeriesError::CountOutOfRange { count: bad },
                "count {bad}"
            );
        }
        assert!(
            parse_step_series_form("10", "0.5", 0, "")
                .unwrap_err()
                .to_string()
                .contains("1 to 64")
        );
    }

    #[test]
    fn a_crown_ladder_must_stay_above_zero_and_within_ninety() {
        // 40, 30, 20, 10, 0: the fifth rung is the table, not a crown facet.
        assert_eq!(
            parse_step_series_form("40", "-10", 5, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange {
                tier: 5,
                angle_deg: 0.0
            }
        );
        // 80, 85, 90 fits; one more rung (95) does not.
        assert!(parse_step_series_form("80", "5", 3, "").is_ok());
        assert_eq!(
            parse_step_series_form("80", "5", 4, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange {
                tier: 4,
                angle_deg: 95.0
            }
        );
        // A start of 0 is already outside the range.
        assert!(matches!(
            parse_step_series_form("0", "1", 1, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange { tier: 1, .. }
        ));
        assert!(matches!(
            parse_step_series_form("91", "0", 1, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange { tier: 1, .. }
        ));
    }

    #[test]
    fn a_pavilion_ladder_keeps_its_sign_and_the_same_limits() {
        assert!(parse_step_series_form("-40", "-5", 10, "").is_ok());
        // -40, -60, -80, -100: past the girdle on the fourth rung.
        assert_eq!(
            parse_step_series_form("-40", "-20", 4, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange {
                tier: 4,
                angle_deg: -100.0
            }
        );
        // -90 is the girdle itself and is allowed.
        assert!(parse_step_series_form("-70", "-10", 3, "").is_ok());
        // -0.0 is the culet, not a pavilion facet.
        assert!(matches!(
            parse_step_series_form("-0", "0", 1, "").unwrap_err(),
            StepSeriesError::AngleOutOfRange { tier: 1, .. }
        ));
    }

    #[test]
    fn a_ladder_may_not_cross_into_the_other_block() {
        // -40, -10, +20: the third rung is a crown facet.
        assert_eq!(
            parse_step_series_form("-40", "30", 3, "").unwrap_err(),
            StepSeriesError::CrossesBlock {
                tier: 3,
                angle_deg: 20.0
            }
        );
        // 10, -5: the second rung is a pavilion facet.
        assert_eq!(
            parse_step_series_form("10", "-15", 2, "").unwrap_err(),
            StepSeriesError::CrossesBlock {
                tier: 2,
                angle_deg: -5.0
            }
        );
    }

    #[test]
    fn the_refusal_messages_show_angles_as_positive_numbers() {
        // The error keeps the stored angle (-100); the message a person reads does not.
        let past_the_girdle = parse_step_series_form("-40", "-20", 4, "").unwrap_err();
        assert_eq!(
            past_the_girdle.to_string(),
            "Tier 4 of the ladder would sit at 100.00\u{b0} -- every generated angle must be \
             more than 0\u{b0} and at most 90\u{b0} from the girdle plane."
        );
        let across = parse_step_series_form("10", "-15", 2, "").unwrap_err();
        assert_eq!(
            across.to_string(),
            "Tier 2 of the ladder would cross the girdle (it would sit at 5.00\u{b0} on the \
             other side of the start angle's block) -- a ladder stays within one block (crown \
             or pavilion)."
        );
    }

    #[test]
    fn the_numeric_fields_take_arithmetic() {
        let (start, step, count, constraint) =
            parse_step_series_form("40 + 2", " -(1 / 2) ", 3, "0.3 + 0.02").expect("parses");
        assert_eq!(start.to_bits(), 42.0_f64.to_bits());
        assert_eq!(step.to_bits(), (-0.5_f64).to_bits());
        assert_eq!(count, 3);
        assert_eq!(constraint, MeetConstraint::ScaleReference(0.32));
    }

    #[test]
    fn a_calculation_that_fails_names_its_field() {
        let error = parse_step_series_form("40", "1 / 0", 3, "").unwrap_err();
        assert_eq!(
            error.to_string(),
            "Angle step '1 / 0' cannot be calculated: it divides by zero."
        );
        let error = parse_step_series_form("40", "1", 3, "(2").unwrap_err();
        assert!(
            error
                .to_string()
                .starts_with("Anchor '(2' cannot be calculated:"),
            "{error}"
        );
        let error = parse_step_series_form("4 0 +", "1", 3, "").unwrap_err();
        assert!(
            error.to_string().starts_with("Start angle '4 0 +'"),
            "{error}"
        );
    }

    #[test]
    fn the_error_text_converts_to_a_string() {
        let message: String = StepSeriesError::NotFinite.into();
        assert_eq!(
            message,
            "Start angle and angle step must be finite numbers."
        );
    }
}