Skip to content

Commit

Permalink
✨ Add 'components: all'
Browse files Browse the repository at this point in the history
  • Loading branch information
mleduque committed Dec 23, 2023
1 parent 4aeff21 commit 3851e64
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/log_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fn check_installed_components(module: &WeiduMod) -> Result<Vec<u32>> {
match &module.components {
Components::None => Ok(vec![]),
Components::Ask => Ok(vec![]),
Components::All => Ok(vec![]),
Components::List(components) => {
let log_rows = match parse_weidu_log(Some(&module.name)) {
Ok(log_rows) => log_rows,
Expand Down
20 changes: 20 additions & 0 deletions src/module/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use serde::de::{self, Visitor, SeqAccess};
pub enum Components {
Ask,
None,
All,
List(Vec<Component>),
}

Expand All @@ -35,6 +36,7 @@ impl Serialize for Components {
match self {
Components::Ask => serializer.serialize_str("ask"),
Components::None => serializer.serialize_str("none"),
Components::All => serializer.serialize_str("all"),
Components::List(list) => serializer.collect_seq(list.iter()),
}
}
Expand Down Expand Up @@ -67,6 +69,7 @@ impl FromStr for Components {
return match s {
"ask" => Ok(Components::Ask),
"none" => Ok(Components::None),
"all" => Ok(Components::All),
_ => Err(ParseComponentError(s.to_string())),
}
}
Expand Down Expand Up @@ -171,6 +174,23 @@ mod test_deserialize {
);
}

#[test]
fn deserialize_all() {
let yaml = r#"
name: mod_name
components: all
"#;
let module: WeiduMod = serde_yaml::from_str(yaml).unwrap();
assert_eq!(
module,
WeiduMod {
name: lwc!("mod_name"),
components: Components::All,
..Default::default()
}
);
}

#[test]
fn deserialize_list() {
let yaml = r#"
Expand Down
18 changes: 18 additions & 0 deletions src/run_weidu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn run_weidu_install(tp2: &str, module: &WeiduMod, opts: &Install, global: &
match &module.components {
Components::None => Ok(RunResult::Dry("Explicitly requested no components to be installed".to_string())),
Components::Ask => run_weidu_install_interactive(tp2, module, opts, &global.game_language, config),
Components::All => run_weidu_install_all(tp2, module, opts, &global.game_language, language_id, config),
Components::List(comp) if comp.is_empty() => run_weidu_install_interactive(tp2, module, opts, &global.game_language, config),
Components::List(components) => run_weidu_install_auto(tp2, module, components, opts, &global.game_language, language_id, config)
}
Expand Down Expand Up @@ -101,6 +102,17 @@ fn run_weidu_install_interactive(tp2: &str, module: &WeiduMod, opts: &Install,
}
}

fn run_weidu_install_all(tp2: &str, module: &WeiduMod, opts: &Install,
game_lang: &str, language_id: u32, config: &Config) -> Result<RunResult> {
let list = match run_weidu_list_components(tp2, language_id, config) {
Err(error) => bail!("Could not get component list for 'All' mod\n{error}"),
Ok(list) => list,
};
let components = list.iter()
.map(|weidu_comp| Component::Simple(weidu_comp.number))
.collect::<Vec<_>>();
run_weidu_install_auto(tp2, module, &components, opts, game_lang, language_id, config)
}

pub fn format_install_result(result: &RunResult, module: &WeiduMod) -> Vec<u8> {
return match result {
Expand All @@ -121,11 +133,17 @@ pub fn format_install_result(result: &RunResult, module: &WeiduMod) -> Vec<u8> {

#[derive(Debug, Clone, serde::Deserialize)]
pub struct WeiduComponent {
// apparition order in component list
pub index: u32,
// component number (as defined by DESIGNATED)
pub number: u32,
// Dunno... FORCED_SUBCOMPONENT maybe?
pub forced: bool,
// Component name (which appears in weidu.log)
pub name: String,
// when the component is one of multiple options GROUP+SUBCOMPONENT - name of the parent group
pub subgroup: Option<String>,
// Component grouping by category for ease of installation
pub group: Vec<String>,
}

Expand Down
4 changes: 3 additions & 1 deletion src/sub/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn check_safely_installable(module: &Module) -> Result<SafetyResult> {
let installed = extract_unique_components()?;
match module.get_components() {
Components::None => Ok(SafetyResult::Safe),
Components::Ask => {
Components::Ask | Components::All => {
let existing = installed.iter().filter(|comp| comp.mod_key == *module.get_name()).collect_vec();
if !existing.is_empty() {
let prompt = format!(r#"
Expand Down Expand Up @@ -231,6 +231,7 @@ fn record_selection(index: usize, module: &WeiduMod, output_file: &str, original
let previous_mod = record_manifest.modules[..index].iter().rev().find(|item| match item.get_components() {
Components::List(_) => true,
Components::Ask => true,
Components::All => true,
Components::None => false,
});
debug!("record_selection- previous_mod={:?}", previous_mod);
Expand All @@ -242,6 +243,7 @@ fn record_selection(index: usize, module: &WeiduMod, output_file: &str, original
let previous_components = match previous_components {
Components::List(ref list) => list,
Components::Ask => bail!("components for previous mod fragment were not recorded"),
Components::All => bail!("components for previous mod fragment were not recorded"),
Components::None => bail!("search incorrectly returned a 'none' component list"),
};
let previous_name = previous.get_name();
Expand Down
1 change: 1 addition & 0 deletions src/sub/reset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub fn reset(args: &Reset, game_dir: &CanonPath, config: &Config) -> Result<()>
let components = match weidu_mod.components {
Components::None => bail!("Can't reset to a module fragment which doesn't install components (`components: none`)"),
Components::Ask => bail!("Can't reset to a module fragment which doesn't specify components explicitly (`components: ask`)"),
Components::All => bail!("Can't reset to a module fragment which doesn't specify components explicitly (`components: all`)"),
Components::List(list) if list.is_empty() => bail!("Can't reset to a module fragment which doesn't install components (`components list is empty`)"),
Components::List(ref list) => list,
};
Expand Down

0 comments on commit 3851e64

Please sign in to comment.