Skip to main content

sbol_core/validation/
resolver.rs

1//! Version-neutral external-resolution interfaces: the traits a caller
2//! implements to dereference document and content references during
3//! resolver-aware validation, plus the resolved value and error types they
4//! exchange.
5
6use std::fmt;
7use std::io;
8
9use sbol_rdf::{Iri, Resource};
10
11use crate::document::RawDocument;
12
13/// Resolved byte content for an Attachment or Model source.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub struct ResolvedContent {
17    pub bytes: Vec<u8>,
18    pub media_type: Option<String>,
19}
20
21impl ResolvedContent {
22    pub fn new(bytes: impl Into<Vec<u8>>, media_type: Option<String>) -> Self {
23        Self {
24            bytes: bytes.into(),
25            media_type,
26        }
27    }
28}
29
30/// Coarse class of a resolution failure.
31#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32#[non_exhaustive]
33pub enum ResolutionErrorKind {
34    UnsupportedScheme,
35    NotFound,
36    InvalidData,
37    Io,
38    Http,
39    Parse,
40}
41
42/// A resolver failure with a stable kind and human-readable context.
43#[derive(Clone, Debug, PartialEq, Eq)]
44#[non_exhaustive]
45pub struct ResolutionError {
46    kind: ResolutionErrorKind,
47    message: String,
48}
49
50impl ResolutionError {
51    pub fn new(kind: ResolutionErrorKind, message: impl Into<String>) -> Self {
52        Self {
53            kind,
54            message: message.into(),
55        }
56    }
57
58    pub fn kind(&self) -> ResolutionErrorKind {
59        self.kind
60    }
61
62    pub fn message(&self) -> &str {
63        &self.message
64    }
65}
66
67impl fmt::Display for ResolutionError {
68    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
69        formatter.write_str(&self.message)
70    }
71}
72
73impl std::error::Error for ResolutionError {}
74
75impl From<io::Error> for ResolutionError {
76    fn from(error: io::Error) -> Self {
77        Self::new(ResolutionErrorKind::Io, error.to_string())
78    }
79}
80
81/// Resolves an external resource into an SBOL document.
82pub trait DocumentResolver {
83    fn resolve_document(&self, resource: &Resource) -> Result<RawDocument, ResolutionError>;
84}
85
86/// Resolves an Attachment or Model source into bytes.
87pub trait ContentResolver {
88    fn resolve_content(&self, source: &Iri) -> Result<ResolvedContent, ResolutionError>;
89}