mod cursor;
mod engine;
use super::{LiteralError, LiteralReader, OccurrencePath, ReasoningError};
pub use cursor::{LiteralCursorError, LiteralTermAccess, Rdf12LiteralOccurrences};
pub use engine::Rdf12LiteralTyper;
use serde::{Deserialize, Serialize};
use std::{error::Error, fmt, future::Future, num::NonZeroU64};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum LiteralRole {
Source,
TargetContext,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LiteralOccurrence<T> {
pub literal: T,
pub root: [T; 3],
pub role: LiteralRole,
pub path: OccurrencePath,
}
pub type LiteralOccurrenceRead<T, R, E> =
Result<Option<(LiteralOccurrence<T>, R)>, LiteralCursorError<T, E>>;
pub trait LiteralOccurrenceCursor: Send {
type Term: Clone + Eq + Send + Sync;
type Error: Error + Send + Sync + 'static;
type Reader: LiteralReader<Error = Self::Error>;
fn next(
&mut self,
) -> impl Future<Output = LiteralOccurrenceRead<Self::Term, Self::Reader, Self::Error>> + Send;
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LiteralTypingLimits {
pub(super) occurrences: NonZeroU64,
pub(super) inferred: NonZeroU64,
pub(super) bytes: u64,
}
impl LiteralTypingLimits {
pub const fn new(occurrences: NonZeroU64, inferred: NonZeroU64, bytes: u64) -> Self {
Self {
occurrences,
inferred,
bytes,
}
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LiteralTypingReport {
pub occurrences: u64,
pub bytes: u64,
pub opaque: u64,
pub inferred: u64,
}
#[derive(Debug)]
pub enum LiteralTypingError<T, E> {
Cursor(LiteralCursorError<T, E>),
Reasoning(ReasoningError<E>),
OccurrenceLimit,
RoleMismatch {
expected: LiteralRole,
occurrence: LiteralOccurrence<T>,
},
Literal {
occurrence: LiteralOccurrence<T>,
error: LiteralError<E>,
},
}
impl<T, E: fmt::Display> fmt::Display for LiteralTypingError<T, E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Cursor(error) => error.fmt(f),
Self::Reasoning(error) => error.fmt(f),
Self::OccurrenceLimit => f.write_str("literal occurrence limit exceeded"),
Self::RoleMismatch {
expected,
occurrence,
} => write!(
f,
"expected {expected:?} literal role, received {:?}",
occurrence.role
),
Self::Literal { occurrence, error } => write!(
f,
"{:?} literal inspection failed: {error}",
occurrence.role
),
}
}
}
impl<T: fmt::Debug, E: Error + 'static> Error for LiteralTypingError<T, E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Cursor(error) => error.source(),
Self::Reasoning(error) => Some(error),
Self::Literal { error, .. } => Some(error),
_ => None,
}
}
}