use super::config::InputConfig;
use crate::cook::command::CookCommand;
use anyhow::Result;
pub struct LegacyInputAdapter;
impl LegacyInputAdapter {
pub fn from_cook_command(command: &CookCommand) -> Result<InputConfig> {
let mut config = InputConfig::default();
if !command.args.is_empty() {
let args_str = command.args.join(",");
config = InputConfig::from_command_args(&args_str);
}
if !command.map.is_empty() {
if !command.args.is_empty() {
let args_source = super::config::InputSource::Arguments {
value: command.args.join(","),
separator: Some(",".to_string()),
validation: None,
};
let file_source = super::config::InputSource::FilePattern {
patterns: command.map.clone(),
recursive: false,
filters: None,
};
config.sources = vec![super::config::InputSource::Composite {
sources: vec![args_source, file_source],
merge_strategy: super::config::MergeStrategy::Sequential,
}];
} else {
config = InputConfig::from_file_patterns(command.map.clone());
}
}
if command.args.is_empty() && command.map.is_empty() {
config.sources = vec![super::config::InputSource::Empty];
}
Ok(config)
}
pub fn supports_mapreduce(config: &InputConfig) -> bool {
config.sources.iter().any(|source| {
matches!(
source,
super::config::InputSource::FilePattern { .. }
| super::config::InputSource::StructuredData { .. }
| super::config::InputSource::Composite { .. }
)
})
}
pub fn to_legacy_format(inputs: &[super::ExecutionInput]) -> Vec<String> {
inputs
.iter()
.map(|input| {
if let Some(file_path) = input.variables.get("file_path") {
file_path.to_string()
} else if let Some(arg) = input.variables.get("arg") {
arg.to_string()
} else if let Some(item) = input.variables.get("item") {
item.to_string()
} else {
input.id.clone()
}
})
.collect()
}
}