use std::sync::Arc;
use crate::format::FormatKind;
use crate::io::{InputProvider, OutputTarget};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FileExistsPolicy {
Overwrite,
Append,
#[default]
Error,
}
impl std::str::FromStr for FileExistsPolicy {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let policy = match s.to_ascii_lowercase().as_str() {
"overwrite" => FileExistsPolicy::Overwrite,
"append" => FileExistsPolicy::Append,
"error" => FileExistsPolicy::Error,
_ => return Err(()),
};
Ok(policy)
}
}
#[derive(Debug, Clone)]
pub struct InputSpec {
pub raw: String,
pub provider: Arc<dyn InputProvider>,
pub explicit_format: Option<FormatKind>,
pub format_candidates: Vec<FormatKind>,
}
impl InputSpec {
pub fn new(raw: impl Into<String>, provider: Arc<dyn InputProvider>) -> Self {
Self {
raw: raw.into(),
provider,
explicit_format: None,
format_candidates: Vec::new(),
}
}
pub fn with_format(mut self, format: FormatKind) -> Self {
self.explicit_format = Some(format);
self
}
pub fn with_candidates(mut self, candidates: Vec<FormatKind>) -> Self {
self.format_candidates = candidates;
self
}
}
#[derive(Debug, Clone)]
pub struct OutputSpec {
pub raw: String,
pub target: Arc<dyn OutputTarget>,
pub explicit_format: Option<FormatKind>,
pub format_candidates: Vec<FormatKind>,
pub file_exists_policy: FileExistsPolicy,
}
impl OutputSpec {
pub fn new(raw: impl Into<String>, target: Arc<dyn OutputTarget>) -> Self {
Self {
raw: raw.into(),
target,
explicit_format: None,
format_candidates: Vec::new(),
file_exists_policy: FileExistsPolicy::default(),
}
}
pub fn with_format(mut self, format: FormatKind) -> Self {
self.explicit_format = Some(format);
self
}
pub fn with_candidates(mut self, candidates: Vec<FormatKind>) -> Self {
self.format_candidates = candidates;
self
}
pub fn with_file_exists_policy(mut self, policy: FileExistsPolicy) -> Self {
self.file_exists_policy = policy;
self
}
}