|
| 1 | +//! Error handling for SBML validation and consistency checking. |
| 2 | +//! |
| 3 | +//! This module provides types for representing and working with errors that occur |
| 4 | +//! during SBML document validation. It includes structures for individual errors |
| 5 | +//! and collections of errors, as well as severity levels to indicate the importance |
| 6 | +//! of each error. |
| 7 | +//! |
| 8 | +//! The main types in this module are: |
| 9 | +//! - `SBMLErrorLog`: A collection of validation errors from an SBML document |
| 10 | +//! - `SBMLError`: An individual validation error with detailed information |
| 11 | +//! - `SBMLErrorSeverity`: The severity level of an error (Error, Warning, etc.) |
| 12 | +
|
| 13 | +use std::pin::Pin; |
| 14 | + |
| 15 | +use crate::{pin_ptr, sbmlcxx, SBMLDocument}; |
| 16 | + |
| 17 | +/// Represents a collection of SBML validation errors from a document. |
| 18 | +/// |
| 19 | +/// This struct contains the validation status of an SBML document and |
| 20 | +/// a list of all errors encountered during validation. |
| 21 | +#[derive(Debug)] |
| 22 | +pub struct SBMLErrorLog { |
| 23 | + /// Indicates whether the document is valid (true) or has errors (false) |
| 24 | + pub valid: bool, |
| 25 | + /// Collection of all errors found during validation |
| 26 | + pub errors: Vec<SBMLError>, |
| 27 | +} |
| 28 | + |
| 29 | +impl SBMLErrorLog { |
| 30 | + /// Creates a new error log from an SBML document. |
| 31 | + /// |
| 32 | + /// Extracts all errors from the document's internal error log and |
| 33 | + /// determines if the document is valid based on the presence of |
| 34 | + /// errors with severity level Error or Fatal. |
| 35 | + /// |
| 36 | + /// # Arguments |
| 37 | + /// * `document` - Reference to the SBML document to extract errors from |
| 38 | + /// |
| 39 | + /// # Returns |
| 40 | + /// A new `SBMLErrorLog` containing all errors and validation status |
| 41 | + pub fn new(document: &SBMLDocument) -> Self { |
| 42 | + // Get the amount of errors for extraction |
| 43 | + let n_errors = document.inner().borrow().getNumErrors().0; |
| 44 | + |
| 45 | + // Pin the error log to extract all errors |
| 46 | + let errorlog_ptr = document.inner().borrow_mut().pin_mut().getErrorLog(); |
| 47 | + let errorlog = pin_ptr!(errorlog_ptr, sbmlcxx::SBMLErrorLog); |
| 48 | + |
| 49 | + // Convert the errors to a Vec with pre-allocated capacity for efficiency |
| 50 | + let mut errors = Vec::with_capacity(n_errors as usize); |
| 51 | + for i in 0..n_errors { |
| 52 | + errors.push(SBMLError::new(errorlog.as_ref().getError(i.into()))); |
| 53 | + } |
| 54 | + |
| 55 | + // Document is invalid if it contains any Error or Fatal severity errors |
| 56 | + let has_errors = errors.iter().any(|error| { |
| 57 | + error.severity == SBMLErrorSeverity::Error || error.severity == SBMLErrorSeverity::Fatal |
| 58 | + }); |
| 59 | + |
| 60 | + Self { |
| 61 | + valid: !has_errors, |
| 62 | + errors, |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Represents a single SBML validation error. |
| 68 | +/// |
| 69 | +/// Contains detailed information about an error encountered during |
| 70 | +/// SBML document validation, including its message, severity level, |
| 71 | +/// location in the document, and category. |
| 72 | +#[derive(Debug)] |
| 73 | +pub struct SBMLError { |
| 74 | + /// The error message describing the issue |
| 75 | + pub message: String, |
| 76 | + /// The severity level of the error |
| 77 | + pub severity: SBMLErrorSeverity, |
| 78 | + /// The line number where the error occurred |
| 79 | + pub line: u32, |
| 80 | + /// The column number where the error occurred |
| 81 | + pub column: u32, |
| 82 | + /// The category of the error (e.g., "SBML", "XML", etc.) |
| 83 | + pub category: String, |
| 84 | +} |
| 85 | + |
| 86 | +impl SBMLError { |
| 87 | + /// Creates a new SBML error from a raw error pointer. |
| 88 | + /// |
| 89 | + /// # Arguments |
| 90 | + /// * `error` - Pointer to the native SBMLError object |
| 91 | + /// |
| 92 | + /// # Returns |
| 93 | + /// A new `SBMLError` with information extracted from the native error |
| 94 | + pub fn new(error: *const sbmlcxx::SBMLError) -> Self { |
| 95 | + let xml_error = error as *const sbmlcxx::XMLError; |
| 96 | + let xml_error = unsafe { Pin::new_unchecked(&*xml_error) }; |
| 97 | + |
| 98 | + let message = xml_error.as_ref().getMessage().to_string(); |
| 99 | + let line = xml_error.as_ref().getLine().0; |
| 100 | + let column = xml_error.as_ref().getColumn().0; |
| 101 | + let category = xml_error.as_ref().getCategoryAsString().to_string(); |
| 102 | + let severity = SBMLErrorSeverity::from(&*xml_error); |
| 103 | + |
| 104 | + Self { |
| 105 | + message, |
| 106 | + severity, |
| 107 | + line, |
| 108 | + column, |
| 109 | + category, |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +/// Represents the severity level of an SBML error. |
| 115 | +/// |
| 116 | +/// SBML errors can have different severity levels, ranging from |
| 117 | +/// informational messages to fatal errors that prevent document processing. |
| 118 | +#[derive(Debug, PartialEq, Eq)] |
| 119 | +pub enum SBMLErrorSeverity { |
| 120 | + /// Standard error that indicates a problem with the SBML document |
| 121 | + Error, |
| 122 | + /// Severe error that prevents further processing |
| 123 | + Fatal, |
| 124 | + /// Warning that doesn't invalidate the document but should be addressed |
| 125 | + Warning, |
| 126 | + /// Informational message that doesn't indicate a problem |
| 127 | + Info, |
| 128 | + /// Internal error in the SBML library |
| 129 | + Internal, |
| 130 | + /// System-level error (e.g., file I/O problems) |
| 131 | + System, |
| 132 | + /// Unknown error type |
| 133 | + Unknown, |
| 134 | +} |
| 135 | + |
| 136 | +impl From<&sbmlcxx::XMLError> for SBMLErrorSeverity { |
| 137 | + /// Converts a native XMLError to an SBMLErrorSeverity. |
| 138 | + /// |
| 139 | + /// # Arguments |
| 140 | + /// * `xml_error` - Reference to the native XMLError |
| 141 | + /// |
| 142 | + /// # Returns |
| 143 | + /// The corresponding SBMLErrorSeverity variant |
| 144 | + fn from(xml_error: &sbmlcxx::XMLError) -> Self { |
| 145 | + if xml_error.isError() { |
| 146 | + SBMLErrorSeverity::Error |
| 147 | + } else if xml_error.isWarning() { |
| 148 | + SBMLErrorSeverity::Warning |
| 149 | + } else if xml_error.isInternal() { |
| 150 | + SBMLErrorSeverity::Internal |
| 151 | + } else if xml_error.isFatal() { |
| 152 | + SBMLErrorSeverity::Fatal |
| 153 | + } else if xml_error.isSystem() { |
| 154 | + SBMLErrorSeverity::System |
| 155 | + } else if xml_error.isInfo() { |
| 156 | + SBMLErrorSeverity::Info |
| 157 | + } else { |
| 158 | + SBMLErrorSeverity::Unknown |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments