mirror of
https://github.com/martinvonz/jj.git
synced 2025-02-11 23:08:03 +00:00
conflicts: rename try_map()
to maybe_map()
I'm going to add a `Result` version and it makes more sense to call that `try_map()`.
This commit is contained in:
parent
83fefa9a3b
commit
24c0190f74
2 changed files with 14 additions and 11 deletions
|
@ -126,12 +126,15 @@ impl<T> Conflict<T> {
|
||||||
|
|
||||||
/// Creates a new conflict by applying `f` to each remove and add.
|
/// Creates a new conflict by applying `f` to each remove and add.
|
||||||
pub fn map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> U) -> Conflict<U> {
|
pub fn map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> U) -> Conflict<U> {
|
||||||
self.try_map(|term| Some(f(term))).unwrap()
|
self.maybe_map(|term| Some(f(term))).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new conflict by applying `f` to each remove and add, returning
|
/// Creates a new conflict by applying `f` to each remove and add, returning
|
||||||
/// `None if `f` returns `None` for any of them.
|
/// `None if `f` returns `None` for any of them.
|
||||||
pub fn try_map<'a, U>(&'a self, mut f: impl FnMut(&'a T) -> Option<U>) -> Option<Conflict<U>> {
|
pub fn maybe_map<'a, U>(
|
||||||
|
&'a self,
|
||||||
|
mut f: impl FnMut(&'a T) -> Option<U>,
|
||||||
|
) -> Option<Conflict<U>> {
|
||||||
let removes = self.removes.iter().map(&mut f).collect::<Option<_>>()?;
|
let removes = self.removes.iter().map(&mut f).collect::<Option<_>>()?;
|
||||||
let adds = self.adds.iter().map(&mut f).collect::<Option<_>>()?;
|
let adds = self.adds.iter().map(&mut f).collect::<Option<_>>()?;
|
||||||
Some(Conflict { removes, adds })
|
Some(Conflict { removes, adds })
|
||||||
|
@ -196,7 +199,7 @@ impl Conflict<Option<TreeValue>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn to_file_conflict(&self) -> Option<Conflict<Option<FileId>>> {
|
pub fn to_file_conflict(&self) -> Option<Conflict<Option<FileId>>> {
|
||||||
self.try_map(|term| match term {
|
self.maybe_map(|term| match term {
|
||||||
None => Some(None),
|
None => Some(None),
|
||||||
Some(TreeValue::File {
|
Some(TreeValue::File {
|
||||||
id,
|
id,
|
||||||
|
@ -766,7 +769,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_try_map() {
|
fn test_maybe_map() {
|
||||||
fn sqrt(i: &i32) -> Option<i32> {
|
fn sqrt(i: &i32) -> Option<i32> {
|
||||||
if *i >= 0 {
|
if *i >= 0 {
|
||||||
Some((*i as f64).sqrt() as i32)
|
Some((*i as f64).sqrt() as i32)
|
||||||
|
@ -778,11 +781,11 @@ mod tests {
|
||||||
Conflict::new(removes.to_vec(), adds.to_vec())
|
Conflict::new(removes.to_vec(), adds.to_vec())
|
||||||
}
|
}
|
||||||
// 1-way conflict
|
// 1-way conflict
|
||||||
assert_eq!(c(&[], &[1]).try_map(sqrt), Some(c(&[], &[1])));
|
assert_eq!(c(&[], &[1]).maybe_map(sqrt), Some(c(&[], &[1])));
|
||||||
assert_eq!(c(&[], &[-1]).try_map(sqrt), None);
|
assert_eq!(c(&[], &[-1]).maybe_map(sqrt), None);
|
||||||
// 3-way conflict
|
// 3-way conflict
|
||||||
assert_eq!(c(&[1], &[4, 9]).try_map(sqrt), Some(c(&[1], &[2, 3])));
|
assert_eq!(c(&[1], &[4, 9]).maybe_map(sqrt), Some(c(&[1], &[2, 3])));
|
||||||
assert_eq!(c(&[-1], &[4, 9]).try_map(sqrt), None);
|
assert_eq!(c(&[-1], &[4, 9]).maybe_map(sqrt), None);
|
||||||
assert_eq!(c(&[1], &[-4, 9]).try_map(sqrt), None);
|
assert_eq!(c(&[1], &[-4, 9]).maybe_map(sqrt), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -648,7 +648,7 @@ fn try_resolve_file_conflict(
|
||||||
// we can't merge them anyway. At the same time we determine whether the
|
// we can't merge them anyway. At the same time we determine whether the
|
||||||
// resulting file should be executable.
|
// resulting file should be executable.
|
||||||
// TODO: Change to let-else once our MSRV is above 1.65
|
// TODO: Change to let-else once our MSRV is above 1.65
|
||||||
let file_id_conflict = if let Some(conflict) = conflict.try_map(|term| match term {
|
let file_id_conflict = if let Some(conflict) = conflict.maybe_map(|term| match term {
|
||||||
Some(TreeValue::File { id, executable: _ }) => Some(id),
|
Some(TreeValue::File { id, executable: _ }) => Some(id),
|
||||||
_ => None,
|
_ => None,
|
||||||
}) {
|
}) {
|
||||||
|
@ -657,7 +657,7 @@ fn try_resolve_file_conflict(
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
};
|
};
|
||||||
// TODO: Change to let-else once our MSRV is above 1.65
|
// TODO: Change to let-else once our MSRV is above 1.65
|
||||||
let executable_conflict = if let Some(conflict) = conflict.try_map(|term| match term {
|
let executable_conflict = if let Some(conflict) = conflict.maybe_map(|term| match term {
|
||||||
Some(TreeValue::File { id: _, executable }) => Some(executable),
|
Some(TreeValue::File { id: _, executable }) => Some(executable),
|
||||||
_ => None,
|
_ => None,
|
||||||
}) {
|
}) {
|
||||||
|
|
Loading…
Reference in a new issue