oxirs-core 0.2.2

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Core error types for OxiRS
//!
//! This module provides the base error types that all OxiRS modules should use.
//! Module-specific errors should include this as a variant.

// Removed unused std::fmt import

/// Core error type for OxiRS operations
#[derive(Debug, Clone, thiserror::Error)]
pub enum CoreError {
    /// Invalid parameter provided
    #[error("Invalid parameter '{name}': {message}")]
    InvalidParameter { name: String, message: String },

    /// Resource not found
    #[error("Resource not found: {0}")]
    NotFound(String),

    /// Operation not supported
    #[error("Operation not supported: {0}")]
    NotSupported(String),

    /// Dimension mismatch in vector operations
    #[error("Dimension mismatch: expected {expected}, got {actual}")]
    DimensionMismatch { expected: usize, actual: usize },

    /// Platform capability not available
    #[error("Platform capability not available: {0}")]
    CapabilityNotAvailable(String),

    /// Memory allocation failure
    #[error("Memory allocation failed: {0}")]
    MemoryError(String),

    /// I/O error
    #[error("I/O error: {0}")]
    IoError(String),

    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(String),

    /// Configuration error
    #[error("Configuration error: {0}")]
    ConfigError(String),

    /// Timeout error
    #[error("Operation timed out: {0}")]
    Timeout(String),

    /// Generic internal error
    #[error("Internal error: {0}")]
    Internal(String),
}

impl From<std::io::Error> for CoreError {
    fn from(err: std::io::Error) -> Self {
        CoreError::IoError(err.to_string())
    }
}

impl From<serde_json::Error> for CoreError {
    fn from(err: serde_json::Error) -> Self {
        CoreError::SerializationError(err.to_string())
    }
}

/// Result type alias using CoreError
pub type CoreResult<T> = Result<T, CoreError>;

// Re-export the main OxiRS error types for compatibility
pub use crate::{OxirsError, Result as OxirsResult};

/// Validation functions for common parameter checks
pub mod validation {
    use super::{CoreError, CoreResult};
    use std::fmt;

    /// Check that a value is positive
    pub fn check_positive<T>(value: T, name: &str) -> CoreResult<T>
    where
        T: PartialOrd + Default + fmt::Display + Copy,
    {
        if value <= T::default() {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: format!("Value must be positive, got {value}"),
            })
        } else {
            Ok(value)
        }
    }

    /// Check that a value is finite (for floating point)
    pub fn check_finite_f32(value: f32, name: &str) -> CoreResult<f32> {
        if !value.is_finite() {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: format!("Value must be finite, got {value}"),
            })
        } else {
            Ok(value)
        }
    }

    /// Check that a value is finite (for floating point)
    pub fn check_finite_f64(value: f64, name: &str) -> CoreResult<f64> {
        if !value.is_finite() {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: format!("Value must be finite, got {value}"),
            })
        } else {
            Ok(value)
        }
    }

    /// Check that an array contains only finite values
    pub fn check_finite_array(values: &[f32]) -> CoreResult<()> {
        for (i, &value) in values.iter().enumerate() {
            if !value.is_finite() {
                return Err(CoreError::InvalidParameter {
                    name: format!("array[{i}]"),
                    message: format!("Value must be finite, got {value}"),
                });
            }
        }
        Ok(())
    }

    /// Check that dimensions match
    pub fn check_dimensions(expected: usize, actual: usize, _context: &str) -> CoreResult<()> {
        if expected != actual {
            Err(CoreError::DimensionMismatch { expected, actual })
        } else {
            Ok(())
        }
    }

    /// Check that a slice is not empty
    pub fn check_non_empty<T>(slice: &[T], name: &str) -> CoreResult<()> {
        if slice.is_empty() {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: "Value must not be empty".to_string(),
            })
        } else {
            Ok(())
        }
    }

    /// Check that a string is not empty
    pub fn check_non_empty_str(value: &str, name: &str) -> CoreResult<()> {
        if value.is_empty() {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: "Value must not be empty".to_string(),
            })
        } else {
            Ok(())
        }
    }

    /// Check that a value is within a range
    pub fn check_range<T>(value: T, min: T, max: T, name: &str) -> CoreResult<T>
    where
        T: PartialOrd + fmt::Display + Copy,
    {
        if value < min || value > max {
            Err(CoreError::InvalidParameter {
                name: name.to_string(),
                message: format!("Value must be between {min} and {max}, got {value}"),
            })
        } else {
            Ok(value)
        }
    }
}

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

    #[test]
    fn test_core_error_display() {
        let error = CoreError::InvalidParameter {
            name: "test".to_string(),
            message: "test message".to_string(),
        };
        assert_eq!(error.to_string(), "Invalid parameter 'test': test message");

        let error = CoreError::NotFound("resource".to_string());
        assert_eq!(error.to_string(), "Resource not found: resource");

        let error = CoreError::DimensionMismatch {
            expected: 3,
            actual: 5,
        };
        assert_eq!(error.to_string(), "Dimension mismatch: expected 3, got 5");
    }

    #[test]
    fn test_from_io_error() {
        let io_error = std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
        let core_error = CoreError::from(io_error);
        match core_error {
            CoreError::IoError(msg) => assert!(msg.contains("file not found")),
            _ => panic!("Expected IoError"),
        }
    }

    #[test]
    fn test_from_serde_error() {
        let serde_error = serde_json::from_str::<i32>("invalid").unwrap_err();
        let core_error = CoreError::from(serde_error);
        match core_error {
            CoreError::SerializationError(_) => {} // Expected
            _ => panic!("Expected SerializationError"),
        }
    }

    #[test]
    fn test_check_positive() {
        // Test positive values
        assert_eq!(
            check_positive(5, "test").expect("value should be positive"),
            5
        );
        assert_eq!(
            check_positive(1.5f32, "test").expect("value should be positive"),
            1.5f32
        );

        // Test zero and negative values
        assert!(check_positive(0, "test").is_err());
        assert!(check_positive(-1, "test").is_err());
        assert!(check_positive(-1.5f32, "test").is_err());

        // Check error message
        let err = check_positive(0, "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("must be positive"));
            }
            _ => panic!("Expected InvalidParameter"),
        }
    }

    #[test]
    fn test_check_finite_f32() {
        // Test finite values
        assert_eq!(
            check_finite_f32(1.5, "test").expect("value should be finite f32"),
            1.5
        );
        assert_eq!(
            check_finite_f32(0.0, "test").expect("value should be finite f32"),
            0.0
        );
        assert_eq!(
            check_finite_f32(-1.5, "test").expect("value should be finite f32"),
            -1.5
        );

        // Test non-finite values
        assert!(check_finite_f32(f32::INFINITY, "test").is_err());
        assert!(check_finite_f32(f32::NEG_INFINITY, "test").is_err());
        assert!(check_finite_f32(f32::NAN, "test").is_err());

        // Check error message
        let err = check_finite_f32(f32::INFINITY, "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("must be finite"));
            }
            _ => panic!("Expected InvalidParameter"),
        }
    }

    #[test]
    fn test_check_finite_f64() {
        // Test finite values
        assert_eq!(
            check_finite_f64(1.5, "test").expect("value should be finite f64"),
            1.5
        );
        assert_eq!(
            check_finite_f64(0.0, "test").expect("value should be finite f64"),
            0.0
        );
        assert_eq!(
            check_finite_f64(-1.5, "test").expect("value should be finite f64"),
            -1.5
        );

        // Test non-finite values
        assert!(check_finite_f64(f64::INFINITY, "test").is_err());
        assert!(check_finite_f64(f64::NEG_INFINITY, "test").is_err());
        assert!(check_finite_f64(f64::NAN, "test").is_err());
    }

    #[test]
    fn test_check_finite_array() {
        // Test array with all finite values
        let finite_array = [1.0, 2.5, -3.0, 0.0];
        assert!(check_finite_array(&finite_array).is_ok());

        // Test empty array
        assert!(check_finite_array(&[]).is_ok());

        // Test array with infinity
        let inf_array = [1.0, f32::INFINITY, 3.0];
        let err = check_finite_array(&inf_array).unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "array[1]");
                assert!(message.contains("must be finite"));
            }
            _ => panic!("Expected InvalidParameter"),
        }

        // Test array with NaN
        let nan_array = [1.0, f32::NAN, 3.0];
        assert!(check_finite_array(&nan_array).is_err());
    }

    #[test]
    fn test_check_dimensions() {
        // Test matching dimensions
        assert!(check_dimensions(5, 5, "test").is_ok());
        assert!(check_dimensions(0, 0, "test").is_ok());

        // Test mismatched dimensions
        let err = check_dimensions(5, 3, "test").unwrap_err();
        match err {
            CoreError::DimensionMismatch { expected, actual } => {
                assert_eq!(expected, 5);
                assert_eq!(actual, 3);
            }
            _ => panic!("Expected DimensionMismatch"),
        }
    }

    #[test]
    fn test_check_non_empty() {
        // Test non-empty slice
        let data = [1, 2, 3];
        assert!(check_non_empty(&data, "test").is_ok());

        // Test empty slice
        let empty: &[i32] = &[];
        let err = check_non_empty(empty, "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("must not be empty"));
            }
            _ => panic!("Expected InvalidParameter"),
        }
    }

    #[test]
    fn test_check_non_empty_str() {
        // Test non-empty string
        assert!(check_non_empty_str("hello", "test").is_ok());

        // Test empty string
        let err = check_non_empty_str("", "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("must not be empty"));
            }
            _ => panic!("Expected InvalidParameter"),
        }
    }

    #[test]
    fn test_check_range() {
        // Test value within range
        assert_eq!(
            check_range(5, 1, 10, "test").expect("value should be in range"),
            5
        );
        assert_eq!(
            check_range(1, 1, 10, "test").expect("value should be in range"),
            1
        );
        assert_eq!(
            check_range(10, 1, 10, "test").expect("value should be in range"),
            10
        );
        assert_eq!(
            check_range(2.5f32, 1.0, 5.0, "test").expect("value should be in range"),
            2.5f32
        );

        // Test value below range
        let err = check_range(0, 1, 10, "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("between 1 and 10"));
                assert!(message.contains("got 0"));
            }
            _ => panic!("Expected InvalidParameter"),
        }

        // Test value above range
        let err = check_range(11, 1, 10, "test_param").unwrap_err();
        match err {
            CoreError::InvalidParameter { name, message } => {
                assert_eq!(name, "test_param");
                assert!(message.contains("between 1 and 10"));
                assert!(message.contains("got 11"));
            }
            _ => panic!("Expected InvalidParameter"),
        }
    }

    #[test]
    fn test_error_variants() {
        let error = CoreError::NotSupported("test operation".to_string());
        assert_eq!(error.to_string(), "Operation not supported: test operation");

        let error = CoreError::CapabilityNotAvailable("SIMD".to_string());
        assert_eq!(error.to_string(), "Platform capability not available: SIMD");

        let error = CoreError::MemoryError("allocation failed".to_string());
        assert_eq!(
            error.to_string(),
            "Memory allocation failed: allocation failed"
        );

        let error = CoreError::ConfigError("invalid setting".to_string());
        assert_eq!(error.to_string(), "Configuration error: invalid setting");

        let error = CoreError::Timeout("5 seconds".to_string());
        assert_eq!(error.to_string(), "Operation timed out: 5 seconds");

        let error = CoreError::Internal("unexpected state".to_string());
        assert_eq!(error.to_string(), "Internal error: unexpected state");
    }
}