Skip to main content

vyre_foundation/validate/
validation_error.rs

1//! Validation error type for vyre IR programs.
2
3use core::fmt;
4use std::borrow::Cow;
5use std::sync::Arc;
6
7/// A validation error in a vyre Program.
8#[non_exhaustive]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ValidationError {
11    /// What is wrong.
12    pub message: Cow<'static, str>,
13}
14
15impl ValidationError {
16    /// Build an unsupported-operation diagnostic for backend capability checks.
17    #[must_use]
18    pub fn unsupported_op(backend: &'static str, op_id: &Arc<str>, node_index: usize) -> Self {
19        Self {
20            message: Cow::Owned(format!(
21                "backend `{backend}` does not support operation `{op_id}` at node {node_index}. Fix: choose a backend whose capability set includes this operation, lower the program through a supported backend pipeline, or register an implementation for `{op_id}`."
22            )),
23        }
24    }
25
26    /// Error message.
27    #[must_use]
28    #[inline]
29    pub fn message(&self) -> &str {
30        &self.message
31    }
32}
33
34impl fmt::Display for ValidationError {
35    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36        write!(f, "vyre IR validation: {}", self.message)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn unsupported_op_contains_fix_hint() {
46        let err = ValidationError::unsupported_op("backend-a", &Arc::from("math::fma"), 3);
47        assert!(err.message().contains("backend-a"));
48        assert!(err.message().contains("math::fma"));
49        assert!(err.message().contains("3"));
50        assert!(err.message().contains("Fix:"));
51    }
52
53    #[test]
54    fn message_accessor() {
55        let err = ValidationError {
56            message: Cow::Borrowed("test"),
57        };
58        assert_eq!(err.message(), "test");
59    }
60
61    #[test]
62    fn display_formatting() {
63        let err = ValidationError {
64            message: Cow::Borrowed("bad IR"),
65        };
66        assert_eq!(err.to_string(), "vyre IR validation: bad IR");
67    }
68
69    #[test]
70    fn clone_and_eq() {
71        let a = ValidationError {
72            message: Cow::Borrowed("same"),
73        };
74        let b = a.clone();
75        assert_eq!(a, b);
76    }
77}