diff --git a/capi/src/run_editor.rs b/capi/src/run_editor.rs index f5e5a0bb1..f267deeac 100644 --- a/capi/src/run_editor.rs +++ b/capi/src/run_editor.rs @@ -3,14 +3,14 @@ //! are being applied to the Run. It provides the current state of the editor as //! state objects that can be visualized by any kind of User Interface. -use super::{Json, output_vec, str}; +use super::{output_vec, str, Json}; use crate::{ linked_layout::OwnedLinkedLayout, run::OwnedRun, slice, sum_of_best_cleaner::OwnedSumOfBestCleaner, }; use livesplit_core::{ - Run, RunEditor, TimingMethod, settings::{Image, ImageCache}, + Run, RunEditor, TimingMethod, }; use std::os::raw::c_char; @@ -401,6 +401,23 @@ pub unsafe extern "C" fn RunEditor_import_comparison( .is_ok() } +/// Imports a named comparison from the provided run as a comparison. The +/// comparison can't be added if its name starts with `[Race]` or it already +/// exists. +#[unsafe(no_mangle)] +pub unsafe extern "C" fn RunEditor_import_comparison_as_comparison( + this: &mut RunEditor, + run: &Run, + comparison: *const c_char, + run_comparison: *const c_char, +) -> bool { + // SAFETY: The caller guarantees that `comparison` is valid. + this.import_comparison_as_comparison(run, unsafe { str(comparison) }, unsafe { + str(run_comparison) + }) + .is_ok() +} + /// Removes the chosen custom comparison. You can't remove a Comparison /// Generator's Comparison or the Personal Best. #[unsafe(no_mangle)] diff --git a/src/run/editor/mod.rs b/src/run/editor/mod.rs index 10214c841..051c53755 100644 --- a/src/run/editor/mod.rs +++ b/src/run/editor/mod.rs @@ -6,11 +6,13 @@ use super::{AddComparisonError, CopyComparisonError, LinkedLayout}; use crate::{ - Run, Segment, Time, TimeSpan, TimingMethod, comparison, + comparison, + comparison::personal_best, platform::prelude::*, settings::Image, timing::ParseError as ParseTimeSpanError, - util::{PopulateString, caseless}, + util::{caseless, PopulateString}, + Run, Segment, Time, TimeSpan, TimingMethod, }; use core::{mem::swap, num::ParseIntError}; use snafu::{OptionExt, ResultExt}; @@ -758,6 +760,18 @@ impl Editor { &mut self, run: &Run, comparison: &str, + ) -> Result<(), AddComparisonError> { + self.import_comparison_as_comparison(run, comparison, personal_best::NAME) + } + + /// Imports a named comparison from the provided run as a comparison. The + /// comparison can't be added if its name starts with `[Race]` or it already + /// exists. + pub fn import_comparison_as_comparison( + &mut self, + run: &Run, + comparison: &str, + run_comparison: &str, ) -> Result<(), AddComparisonError> { self.run.add_custom_comparison(comparison)?; @@ -769,7 +783,7 @@ impl Editor { .enumerate() .find(|(_, s)| caseless::eq(segment.name(), s.name())) { - *my_segment.comparison_mut(comparison) = segment.personal_best_split_time(); + *my_segment.comparison_mut(comparison) = segment.comparison(run_comparison); remaining_segments = &mut remaining_segments[segment_index + 1..]; } } @@ -777,7 +791,7 @@ impl Editor { if let [.., my_segment] = &mut self.run.segments_mut()[..] && let [.., segment] = run.segments() { - *my_segment.comparison_mut(comparison) = segment.personal_best_split_time(); + *my_segment.comparison_mut(comparison) = segment.comparison(run_comparison); } self.fix(); diff --git a/src/run/editor/tests/mark_as_modified.rs b/src/run/editor/tests/mark_as_modified.rs index f7f8f8500..817aeb654 100644 --- a/src/run/editor/tests/mark_as_modified.rs +++ b/src/run/editor/tests/mark_as_modified.rs @@ -271,6 +271,17 @@ fn when_importing_comparison() { assert!(editor.run().has_been_modified()); } +#[test] +fn when_importing_comparison_as_comparison() { + let mut editor = base(); + let mut run = Run::new(); + run.push_segment(Segment::new("")); + editor + .import_comparison_as_comparison(&run, "New Comparison", "Personal Best") + .unwrap(); + assert!(editor.run().has_been_modified()); +} + #[test] fn when_removing_comparison() { let mut editor = base();