sbol_core/validation/
resolver.rs1use std::fmt;
7use std::io;
8
9use sbol_rdf::{Iri, Resource};
10
11use crate::document::RawDocument;
12
13#[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#[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#[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
81pub trait DocumentResolver {
83 fn resolve_document(&self, resource: &Resource) -> Result<RawDocument, ResolutionError>;
84}
85
86pub trait ContentResolver {
88 fn resolve_content(&self, source: &Iri) -> Result<ResolvedContent, ResolutionError>;
89}