use std::io::Read;
pub enum InputSource {
Text(String),
AnchoredText { text: String, anchor: String },
Reader(Box<dyn Read + 'static>),
}
impl std::fmt::Debug for InputSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(text) => f.debug_tuple("Text").field(text).finish(),
Self::AnchoredText { text, anchor } => f
.debug_struct("AnchoredText")
.field("anchor", anchor)
.field("text", text)
.finish(),
Self::Reader(_) => f.write_str("Reader(..)"),
}
}
}
#[derive(Debug)]
pub struct ResolvedInclude {
pub id: String,
pub name: String,
pub source: InputSource,
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ResolveProblem {
ResolveFailed {
spec: String,
base_dir: String,
err: std::io::Error,
},
TargetNotRegularFile { target: String },
TargetIsRootFile { spec: String },
ParentIdNotAbsoluteCanonical { parent_id: String },
ParentResolveFailed {
parent_id: String,
from_name: String,
err: std::io::Error,
},
ParentNotRegularFile { parent: String },
ParentHasNoDirectory { parent: String },
ResolvesOutsideRoot { spec: String, root: String },
TraversesSymlink { spec: String },
AbsolutePathNotAllowed { spec: String },
EmptyPath,
InvalidExtension { spec: String },
HiddenFile { spec: String },
EmptyFragment,
FragmentContainsHash { spec: String },
}
#[derive(Debug)]
#[non_exhaustive]
pub enum IncludeResolveError {
Io(std::io::Error),
Message(String),
SizeLimitExceeded(usize, usize),
FileInclude(Box<ResolveProblem>),
}
impl From<std::io::Error> for IncludeResolveError {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}
pub struct IncludeRequest<'a> {
pub spec: &'a str,
pub from_name: &'a str,
pub from_id: Option<&'a str>,
pub stack: Vec<String>,
pub size_remaining: Option<usize>,
pub location: crate::Location,
}
pub type IncludeResolver<'a> =
dyn FnMut(IncludeRequest<'_>) -> Result<ResolvedInclude, IncludeResolveError> + 'a;
impl InputSource {
#[inline]
pub fn from_string(s: String) -> Self {
Self::Text(s)
}
#[inline]
pub fn from_reader<R>(r: R) -> Self
where
R: Read + 'static,
{
Self::Reader(Box::new(r))
}
}