lib3mf 0.1.6

Pure Rust implementation for 3MF (3D Manufacturing Format) parsing and writing
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Error types for 3MF parsing
//!
//! This module provides comprehensive error handling for 3MF file operations.
//! All errors include error codes for categorization and detailed context to help
//! with debugging.
//!
//! # Error Codes
//!
//! Error codes follow the pattern: `E<category><number>`
//!
//! Categories:
//! - **E1xxx**: I/O and archive errors
//! - **E2xxx**: XML parsing and structure errors
//! - **E3xxx**: Model validation errors
//! - **E4xxx**: Unsupported features
//!
//! ## Common Error Codes
//!
//! - `E1001`: I/O error reading file
//! - `E1002`: ZIP archive format error
//! - `E1003`: Missing required file in archive
//! - `E2001`: XML parsing error
//! - `E2002`: XML attribute error
//! - `E2003`: Invalid XML structure
//! - `E2004`: Invalid 3MF format
//! - `E3001`: Invalid model structure
//! - `E3002`: Numeric parse error
//! - `E4001`: Unsupported feature
//! - `E4002`: Required extension not supported

use std::io;
use thiserror::Error;

/// Result type for 3MF operations
pub type Result<T> = std::result::Result<T, Error>;

/// Additional context for errors
///
/// Provides optional supplementary information to help with debugging:
/// - File location information (when available from XML parsing)
/// - Line and column numbers (when available from XML parsing)
/// - Helpful hints for resolving common issues
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ErrorContext {
    /// The file where the error occurred (typically a path within the 3MF archive)
    pub file: Option<String>,

    /// Line number where the error occurred (when available from XML parsing)
    pub line: Option<usize>,

    /// Column number where the error occurred (when available from XML parsing)
    pub column: Option<usize>,

    /// A helpful hint for resolving the error
    pub hint: Option<String>,
}

impl ErrorContext {
    /// Create a new empty error context
    pub fn new() -> Self {
        Self {
            file: None,
            line: None,
            column: None,
            hint: None,
        }
    }

    /// Create an error context with just a hint
    pub fn with_hint(hint: impl Into<String>) -> Self {
        Self {
            file: None,
            line: None,
            column: None,
            hint: Some(hint.into()),
        }
    }

    /// Set the file location
    pub fn file(mut self, file: impl Into<String>) -> Self {
        self.file = Some(file.into());
        self
    }

    /// Set the line number
    pub fn line(mut self, line: usize) -> Self {
        self.line = Some(line);
        self
    }

    /// Set the column number
    pub fn column(mut self, column: usize) -> Self {
        self.column = Some(column);
        self
    }

    /// Set the hint
    pub fn hint(mut self, hint: impl Into<String>) -> Self {
        self.hint = Some(hint.into());
        self
    }
}

impl Default for ErrorContext {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for ErrorContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut parts = Vec::new();

        if let Some(ref file) = self.file {
            parts.push(format!("File: {}", file));
        }

        if let (Some(line), Some(column)) = (self.line, self.column) {
            parts.push(format!("Location: line {}, column {}", line, column));
        } else if let Some(line) = self.line {
            parts.push(format!("Line: {}", line));
        }

        if let Some(ref hint) = self.hint {
            parts.push(format!("Hint: {}", hint));
        }

        if !parts.is_empty() {
            write!(f, "\n{}", parts.join("\n"))
        } else {
            Ok(())
        }
    }
}

/// Errors that can occur when parsing 3MF files
#[derive(Error, Debug)]
pub enum Error {
    /// IO error occurred while reading the file
    ///
    /// **Error Code**: E1001
    ///
    /// **Common Causes**:
    /// - File not found
    /// - Insufficient permissions
    /// - Disk read error
    #[error("[E1001] I/O error: {0}")]
    Io(#[from] io::Error),

    /// ZIP archive error
    ///
    /// **Error Code**: E1002
    ///
    /// **Common Causes**:
    /// - Corrupted ZIP file
    /// - Unsupported compression method
    /// - Truncated archive
    ///
    /// **Suggestions**:
    /// - Verify the file is a valid 3MF (ZIP) archive
    /// - Try re-downloading or re-exporting the file
    #[error("[E1002] ZIP error: {0}")]
    Zip(#[from] zip::result::ZipError),

    /// XML parsing error
    ///
    /// **Error Code**: E2001
    ///
    /// **Common Causes**:
    /// - Malformed XML syntax
    /// - Invalid character encoding
    /// - Unclosed tags
    #[error("[E2001] XML parsing error: {0}")]
    Xml(#[from] quick_xml::Error),

    /// XML attribute error
    ///
    /// **Error Code**: E2002
    ///
    /// **Common Causes**:
    /// - Missing required attribute
    /// - Invalid attribute value
    /// - Duplicate attribute
    ///
    /// **Suggestions**:
    /// - Check the 3MF specification for required attributes
    /// - Verify attribute values are properly formatted
    #[error("[E2002] XML attribute error: {0}")]
    XmlAttr(String),

    /// Missing required file in the 3MF archive
    ///
    /// **Error Code**: E1003
    ///
    /// **Common Causes**:
    /// - Incomplete 3MF package
    /// - Missing 3D model file
    /// - Missing content types file
    ///
    /// **Suggestions**:
    /// - Ensure the 3MF archive contains all required files
    /// - Check for `[Content_Types].xml` and `3D/3dmodel.model` files
    #[error("[E1003] Missing required file: {0}")]
    MissingFile(String),

    /// Invalid 3MF format
    ///
    /// **Error Code**: E2004
    ///
    /// **Common Causes**:
    /// - Non-compliant OPC structure
    /// - Invalid content types
    /// - Missing required OPC relationships
    ///
    /// **Suggestions**:
    /// - Verify the file was exported correctly
    /// - Check the 3MF specification for OPC requirements
    #[error("[E2004] Invalid 3MF format: {0}")]
    InvalidFormat(String),

    /// Invalid XML structure
    ///
    /// **Error Code**: E2003
    ///
    /// **Common Causes**:
    /// - Missing required XML elements
    /// - Elements in wrong order
    /// - Invalid element nesting
    ///
    /// **Suggestions**:
    /// - Check element hierarchy matches 3MF specification
    /// - Verify required child elements are present
    #[error("[E2003] Invalid XML structure: {0}")]
    InvalidXml(String),

    /// Invalid model structure or validation failure
    ///
    /// **Error Code**: E3001
    ///
    /// **Common Causes**:
    /// - Mesh topology errors (non-manifold, degenerate triangles)
    /// - Invalid object references
    /// - Out-of-bounds vertex indices
    /// - Duplicate object IDs
    ///
    /// **Suggestions**:
    /// - Check mesh for manifold edges (each edge shared by at most 2 triangles)
    /// - Verify all vertex indices are within bounds
    /// - Ensure all object IDs are unique and positive
    /// - Validate all references point to existing objects
    #[error("[E3001] Invalid model: {0}")]
    InvalidModel(String),

    /// Geometry placed outside the positive octant
    ///
    /// **Error Code**: E3003
    ///
    /// **Common Causes**:
    /// - Build item transforms place mesh vertices with negative coordinates
    /// - Object positioned below build plate (negative Z)
    /// - Object positioned with negative X or Y coordinates
    ///
    /// **Suggestions**:
    /// - Per 3MF spec, all coordinates must be non-negative (>= 0)
    /// - Adjust build item transforms to ensure all geometry is in positive octant
    /// - Check that final transformed coordinates (after applying transforms) are >= 0
    ///
    /// **Details**:
    /// Object ID: {0}, Min coordinates: ({1:.2}, {2:.2}, {3:.2})
    #[error(
        "[E3003] Geometry outside positive octant - Object {0}: Min coordinates ({1:.2}, {2:.2}, {3:.2}), all coordinates must be >= 0"
    )]
    OutsidePositiveOctant(usize, f64, f64, f64),

    /// Parse error for numeric values
    ///
    /// **Error Code**: E3002
    ///
    /// **Common Causes**:
    /// - Invalid number format
    /// - Out-of-range values
    /// - Non-numeric characters in numeric fields
    ///
    /// **Suggestions**:
    /// - Verify numeric values use proper format (e.g., "1.5" not "1,5")
    /// - Check for special characters or extra whitespace
    #[error("[E3002] Parse error: {0}")]
    ParseError(String),

    /// Unsupported feature or extension
    ///
    /// **Error Code**: E4001
    ///
    /// **Common Causes**:
    /// - Using 3MF extensions not implemented by this parser
    /// - Future 3MF specification features
    ///
    /// **Suggestions**:
    /// - Check if the feature is part of a 3MF extension
    /// - Verify this parser supports the required extensions
    #[error("[E4001] Unsupported feature: {0}")]
    Unsupported(String),

    /// Required extension not supported
    ///
    /// **Error Code**: E4002
    ///
    /// **Common Causes**:
    /// - 3MF file requires an extension not implemented by this parser
    /// - Extension marked as required in the XML namespace
    ///
    /// **Suggestions**:
    /// - Check the file's required extensions in the model XML
    /// - Use a different parser that supports the required extension
    /// - If possible, re-export without the extension
    #[error("[E4002] Required extension not supported: {0}")]
    UnsupportedExtension(String),

    /// Invalid SecureContent keystore
    ///
    /// **Error Code**: E4003
    ///
    /// **Common Causes**:
    /// - Invalid consumer index reference (EPX-2601)
    /// - Missing consumer element when accessright is defined (EPX-2602)
    /// - Invalid encryption algorithm (EPX-2603)
    /// - Duplicate consumer IDs (EPX-2604)
    /// - Invalid encrypted file path (EPX-2605)
    /// - Missing required keystore elements (EPX-2606)
    /// - Referenced file doesn't exist in package (EPX-2607)
    ///
    /// **Suggestions**:
    /// - Verify the keystore.xml follows the 3MF SecureContent specification
    /// - Check consumer definitions and accessright references
    /// - Ensure all referenced files exist in the package
    #[error("[E4003] Invalid SecureContent: {0}")]
    InvalidSecureContent(String),

    /// XML writing error
    ///
    /// **Error Code**: E2005
    ///
    /// **Common Causes**:
    /// - Failed to serialize XML
    /// - Invalid data for XML writing
    /// - I/O error during writing
    ///
    /// **Suggestions**:
    /// - Check that data structures are valid
    /// - Ensure output stream is writable
    #[error("[E2005] XML writing error: {0}")]
    XmlWrite(String),
}

impl From<std::num::ParseFloatError> for Error {
    fn from(err: std::num::ParseFloatError) -> Self {
        Error::ParseError(format!("Failed to parse floating-point number: {}", err))
    }
}

impl From<std::num::ParseIntError> for Error {
    fn from(err: std::num::ParseIntError) -> Self {
        Error::ParseError(format!("Failed to parse integer: {}", err))
    }
}

impl From<quick_xml::events::attributes::AttrError> for Error {
    fn from(err: quick_xml::events::attributes::AttrError) -> Self {
        Error::XmlAttr(format!("Attribute parsing failed: {}", err))
    }
}

impl Error {
    /// Get the error type as a string for matching against expected failures
    ///
    /// This returns a stable identifier for the error variant, useful for
    /// testing and validation purposes.
    pub fn error_type(&self) -> &'static str {
        match self {
            Error::Io(_) => "Io",
            Error::Zip(_) => "Zip",
            Error::Xml(_) => "Xml",
            Error::XmlAttr(_) => "XmlAttr",
            Error::MissingFile(_) => "MissingFile",
            Error::InvalidFormat(_) => "InvalidFormat",
            Error::InvalidXml(_) => "InvalidXml",
            Error::InvalidModel(_) => "InvalidModel",
            Error::OutsidePositiveOctant(_, _, _, _) => "OutsidePositiveOctant",
            Error::ParseError(_) => "ParseError",
            Error::Unsupported(_) => "Unsupported",
            Error::UnsupportedExtension(_) => "UnsupportedExtension",
            Error::InvalidSecureContent(_) => "InvalidSecureContent",
            Error::XmlWrite(_) => "XmlWrite",
        }
    }

    /// Create an InvalidXml error with element context
    ///
    /// # Arguments
    /// * `element` - The XML element name where the error occurred
    /// * `message` - Description of the error
    ///
    /// # Example
    /// ```ignore
    /// Error::invalid_xml_element("vertex", "Missing required 'x' attribute")
    /// ```
    pub fn invalid_xml_element(element: &str, message: &str) -> Self {
        Error::InvalidXml(format!("Element '<{}>': {}", element, message))
    }

    /// Create an InvalidXml error for a missing required attribute
    ///
    /// # Arguments
    /// * `element` - The XML element name
    /// * `attribute` - The missing attribute name
    ///
    /// # Example
    /// ```ignore
    /// Error::missing_attribute("object", "id")
    /// ```
    pub fn missing_attribute(element: &str, attribute: &str) -> Self {
        Error::InvalidXml(format!(
            "Element '<{}>' is missing required attribute '{}'. \
             Check the 3MF specification for required attributes.",
            element, attribute
        ))
    }

    /// Create an InvalidFormat error with context about what file/structure is invalid
    ///
    /// # Arguments
    /// * `context` - What part of the format is invalid (e.g., "OPC structure", "Content types")
    /// * `message` - Description of the error
    pub fn invalid_format_context(context: &str, message: &str) -> Self {
        Error::InvalidFormat(format!("{}: {}", context, message))
    }

    /// Create a ParseError with context about what was being parsed
    ///
    /// # Arguments
    /// * `field_name` - The name of the field being parsed (e.g., "vertex x coordinate")
    /// * `value` - The value that failed to parse
    /// * `expected_type` - The expected type (e.g., "floating-point number")
    pub fn parse_error_with_context(field_name: &str, value: &str, expected_type: &str) -> Self {
        Error::ParseError(format!(
            "Failed to parse '{}': expected {}, got '{}'. \
             Verify the value is properly formatted.",
            field_name, expected_type, value
        ))
    }

    /// Create an XmlWrite error
    ///
    /// # Arguments
    /// * `message` - Description of the writing error
    pub fn xml_write(message: String) -> Self {
        Error::XmlWrite(message)
    }
}

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

    #[test]
    fn test_error_codes_in_messages() {
        // Verify error codes are present in error messages
        let io_err = Error::Io(io::Error::new(io::ErrorKind::NotFound, "test"));
        assert!(io_err.to_string().contains("[E1001]"));

        let missing_file = Error::MissingFile("test.model".to_string());
        assert!(missing_file.to_string().contains("[E1003]"));

        let invalid_model = Error::InvalidModel("test error".to_string());
        assert!(invalid_model.to_string().contains("[E3001]"));

        let parse_err = Error::ParseError("test".to_string());
        assert!(parse_err.to_string().contains("[E3002]"));

        let unsupported = Error::Unsupported("test feature".to_string());
        assert!(unsupported.to_string().contains("[E4001]"));
    }

    #[test]
    fn test_invalid_xml_element_helper() {
        let err = Error::invalid_xml_element("vertex", "Missing required 'x' attribute");
        assert!(err.to_string().contains("Element '<vertex>'"));
        assert!(err.to_string().contains("Missing required 'x' attribute"));
        assert!(err.to_string().contains("[E2003]"));
    }

    #[test]
    fn test_missing_attribute_helper() {
        let err = Error::missing_attribute("object", "id");
        assert!(err.to_string().contains("Element '<object>'"));
        assert!(err.to_string().contains("missing required attribute 'id'"));
        assert!(err.to_string().contains("3MF specification"));
        assert!(err.to_string().contains("[E2003]"));
    }

    #[test]
    fn test_invalid_format_context_helper() {
        let err = Error::invalid_format_context("OPC structure", "Missing relationship");
        assert!(err.to_string().contains("OPC structure"));
        assert!(err.to_string().contains("Missing relationship"));
        assert!(err.to_string().contains("[E2004]"));
    }

    #[test]
    fn test_parse_error_with_context_helper() {
        let err =
            Error::parse_error_with_context("vertex x coordinate", "abc", "floating-point number");
        assert!(err.to_string().contains("vertex x coordinate"));
        assert!(err.to_string().contains("floating-point number"));
        assert!(err.to_string().contains("'abc'"));
        assert!(err.to_string().contains("properly formatted"));
        assert!(err.to_string().contains("[E3002]"));
    }

    #[test]
    fn test_parse_float_error_conversion() {
        let parse_err: std::num::ParseFloatError = "not_a_number".parse::<f64>().unwrap_err();
        let err = Error::from(parse_err);
        assert!(
            err.to_string()
                .contains("Failed to parse floating-point number")
        );
        assert!(err.to_string().contains("[E3002]"));
    }

    #[test]
    fn test_parse_int_error_conversion() {
        let parse_err: std::num::ParseIntError = "not_a_number".parse::<i32>().unwrap_err();
        let err = Error::from(parse_err);
        assert!(err.to_string().contains("Failed to parse integer"));
        assert!(err.to_string().contains("[E3002]"));
    }

    #[test]
    fn test_error_context_with_hint() {
        let ctx = ErrorContext::with_hint("Check the 3MF specification");
        assert_eq!(ctx.hint, Some("Check the 3MF specification".to_string()));
        assert_eq!(ctx.file, None);
        assert_eq!(ctx.line, None);
        assert_eq!(ctx.column, None);
    }

    #[test]
    fn test_error_context_builder() {
        let ctx = ErrorContext::new()
            .file("3D/3dmodel.model")
            .line(42)
            .column(15)
            .hint("Check attribute syntax");

        assert_eq!(ctx.file, Some("3D/3dmodel.model".to_string()));
        assert_eq!(ctx.line, Some(42));
        assert_eq!(ctx.column, Some(15));
        assert_eq!(ctx.hint, Some("Check attribute syntax".to_string()));
    }

    #[test]
    fn test_error_context_display() {
        let ctx = ErrorContext::new()
            .file("3D/3dmodel.model")
            .line(42)
            .column(15)
            .hint("Check attribute syntax");

        let display = ctx.to_string();
        assert!(display.contains("File: 3D/3dmodel.model"));
        assert!(display.contains("Location: line 42, column 15"));
        assert!(display.contains("Hint: Check attribute syntax"));
    }

    #[test]
    fn test_error_context_display_partial() {
        let ctx = ErrorContext::new()
            .file("3D/3dmodel.model")
            .hint("Check the specification");

        let display = ctx.to_string();
        assert!(display.contains("File: 3D/3dmodel.model"));
        assert!(display.contains("Hint: Check the specification"));
        assert!(!display.contains("Location:"));
    }

    #[test]
    fn test_error_context_display_empty() {
        let ctx = ErrorContext::new();
        let display = ctx.to_string();
        assert_eq!(display, "");
    }
}