Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions capi/src/run_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)]
Expand Down
22 changes: 18 additions & 4 deletions src/run/editor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)?;

Expand All @@ -769,15 +783,15 @@ 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..];
}
}

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();
Expand Down
11 changes: 11 additions & 0 deletions src/run/editor/tests/mark_as_modified.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down