use std::path::PathBuf;
use crate::collator::AnyCollator;
use crate::error::{HanziSortError, Result};
use crate::format::FormatConfig;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputSource {
Files(Vec<PathBuf>),
Text(Vec<String>),
Stdin,
}
impl InputSource {
pub fn is_empty(&self) -> bool {
match self {
Self::Files(paths) => paths.is_empty(),
Self::Text(items) => items.is_empty(),
Self::Stdin => false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct HeaderSpec {
pub lines: usize,
pub keep: bool,
}
#[derive(Debug, Clone)]
pub struct RuntimeConfig {
pub input: InputSource,
pub format: FormatConfig,
pub collator: AnyCollator,
pub unique: bool,
pub reverse: bool,
pub header: HeaderSpec,
}
impl RuntimeConfig {
pub fn new(input: InputSource, format: FormatConfig, collator: AnyCollator) -> Result<Self> {
if input.is_empty() {
return Err(HanziSortError::InvalidArgument(
"at least one input file or text item is required".to_string(),
));
}
Ok(Self {
input,
format: format.validate()?,
collator,
unique: false,
reverse: false,
header: HeaderSpec::default(),
})
}
pub fn with_unique(mut self, unique: bool) -> Self {
self.unique = unique;
self
}
pub fn with_reverse(mut self, reverse: bool) -> Self {
self.reverse = reverse;
self
}
pub fn with_header(mut self, header: HeaderSpec) -> Self {
self.header = header;
self
}
}