use crate::Format;
use std::fmt;
use std::path::Path;
use std::path::PathBuf;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct ReadCandidate {
format: Format,
resolved_path: PathBuf,
}
impl ReadCandidate {
pub(crate) fn new(format: Format, resolved_path: PathBuf) -> Self {
Self {
format,
resolved_path,
}
}
pub fn format(&self) -> Format {
self.format
}
pub fn resolved_path(&self) -> &Path {
&self.resolved_path
}
}
#[non_exhaustive]
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum InputSource {
Path(PathBuf),
#[non_exhaustive]
Memory {
role: String,
},
}
impl InputSource {
pub fn memory(role: impl Into<String>) -> Self {
Self::Memory { role: role.into() }
}
pub fn path(&self) -> Option<&Path> {
match self {
Self::Path(path) => Some(path),
Self::Memory { .. } => None,
}
}
pub fn role(&self) -> Option<&str> {
match self {
Self::Path(_) => None,
Self::Memory { role } => Some(role),
}
}
}
impl From<&Path> for InputSource {
fn from(path: &Path) -> Self {
Self::Path(path.to_path_buf())
}
}
impl From<PathBuf> for InputSource {
fn from(path: PathBuf) -> Self {
Self::Path(path)
}
}
impl fmt::Display for InputSource {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Path(path) => write!(formatter, "{}", path.display()),
Self::Memory { role } => write!(formatter, "in-memory {role}"),
}
}
}