weavatrix-rust 2.16.4

Protocol-independent Rust repository intelligence: typed evidence graphs for impact, architecture, APIs, Git, n8n, Dify, Agent catalogs, Mermaid, and Web3 ABI
Documentation
use std::fmt::{Display, Formatter};
use std::path::PathBuf;

#[derive(Debug)]
pub enum Error {
    Io {
        path: PathBuf,
        source: std::io::Error,
    },
    InvalidRepository(PathBuf),
    Parse {
        language: &'static str,
        path: String,
        message: String,
    },
    Graph(weavatrix_graph::GraphError),
    Json(blazingly_json::Error),
    Scan(weavatrix_scan::Error),
    Analysis(String),
}

impl Error {
    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
        Self::Io {
            path: path.into(),
            source,
        }
    }
}

impl Display for Error {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Io { path, source } => {
                write!(formatter, "I/O error at {}: {source}", path.display())
            }
            Self::InvalidRepository(path) => {
                write!(
                    formatter,
                    "repository root is not a readable directory: {}",
                    path.display()
                )
            }
            Self::Parse {
                language,
                path,
                message,
            } => write!(formatter, "{language} parse failed for {path}: {message}"),
            Self::Graph(source) => write!(formatter, "invalid graph: {source}"),
            Self::Json(source) => write!(formatter, "JSON serialization failed: {source}"),
            Self::Scan(source) => write!(formatter, "repository scan failed: {source}"),
            Self::Analysis(message) => write!(formatter, "repository analysis failed: {message}"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io { source, .. } => Some(source),
            Self::Graph(source) => Some(source),
            Self::Json(source) => Some(source),
            Self::Scan(source) => Some(source),
            _ => None,
        }
    }
}

impl From<blazingly_json::Error> for Error {
    fn from(value: blazingly_json::Error) -> Self {
        Self::Json(value)
    }
}

impl From<weavatrix_graph::GraphError> for Error {
    fn from(value: weavatrix_graph::GraphError) -> Self {
        Self::Graph(value)
    }
}

impl From<weavatrix_scan::Error> for Error {
    fn from(value: weavatrix_scan::Error) -> Self {
        Self::Scan(value)
    }
}

pub type Result<T> = std::result::Result<T, Error>;

#[cfg(test)]
mod tests {
    use super::Error;
    use blazingly_json::Value;

    #[test]
    fn display_and_source_cover_each_variant() {
        let io = Error::io("gone.rs", std::io::Error::other("denied"));
        assert!(io.to_string().contains("gone.rs"));
        assert!(std::error::Error::source(&io).is_some());

        let invalid = Error::InvalidRepository("nope".into());
        assert!(invalid.to_string().contains("nope"));
        assert!(std::error::Error::source(&invalid).is_none());

        let parse = Error::Parse {
            language: "rust",
            path: "x.rs".into(),
            message: "boom".into(),
        };
        assert!(parse.to_string().contains("rust parse failed"));
        assert!(std::error::Error::source(&parse).is_none());

        let json: Error = blazingly_json::from_str::<Value>("{").unwrap_err().into();
        assert!(json.to_string().contains("JSON"));
        assert!(std::error::Error::source(&json).is_some());

        let analysis = Error::Analysis("blocked".into());
        assert!(analysis.to_string().contains("blocked"));
        assert!(std::error::Error::source(&analysis).is_none());
    }
}