use std::fmt;
use std::io;
use sbol_rdf::{Iri, Resource};
use crate::document::RawDocument;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ResolvedContent {
pub bytes: Vec<u8>,
pub media_type: Option<String>,
}
impl ResolvedContent {
pub fn new(bytes: impl Into<Vec<u8>>, media_type: Option<String>) -> Self {
Self {
bytes: bytes.into(),
media_type,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum ResolutionErrorKind {
UnsupportedScheme,
NotFound,
InvalidData,
Io,
Http,
Parse,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ResolutionError {
kind: ResolutionErrorKind,
message: String,
}
impl ResolutionError {
pub fn new(kind: ResolutionErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
pub fn kind(&self) -> ResolutionErrorKind {
self.kind
}
pub fn message(&self) -> &str {
&self.message
}
}
impl fmt::Display for ResolutionError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl std::error::Error for ResolutionError {}
impl From<io::Error> for ResolutionError {
fn from(error: io::Error) -> Self {
Self::new(ResolutionErrorKind::Io, error.to_string())
}
}
pub trait DocumentResolver {
fn resolve_document(&self, resource: &Resource) -> Result<RawDocument, ResolutionError>;
}
pub trait ContentResolver {
fn resolve_content(&self, source: &Iri) -> Result<ResolvedContent, ResolutionError>;
}