Skip to main content

HunkFinder

Trait HunkFinder 

Source
pub trait HunkFinder {
    // Required method
    fn find_location<T: AsRef<str> + Sync>(
        &self,
        hunk: &Hunk,
        target_lines: &[T],
    ) -> Result<(HunkLocation, MatchType), HunkApplyError>;
}
Expand description

A trait for strategies that find the location to apply a hunk.

This allows the core matching algorithm to be pluggable, enabling different search strategies to be used if needed. The library provides a robust DefaultHunkFinder that should be sufficient for most use cases.

§Arguments

  • hunk - The hunk to locate.
  • target_lines - The content to search within.

§Returns

A tuple containing the HunkLocation and the MatchType on success.

§Errors

Returns Err(HunkApplyError) if no suitable location could be found.

§Examples

This example shows a hypothetical, simplified implementation of a HunkFinder that only performs an exact match search.

use mpatch::{Hunk, HunkFinder, HunkLocation, MatchType, HunkApplyError};

struct ExactOnlyFinder;

impl HunkFinder for ExactOnlyFinder {
    fn find_location<T: AsRef<str> + Sync>(
        &self,
        hunk: &Hunk,
        target_lines: &[T],
    ) -> Result<(HunkLocation, MatchType), HunkApplyError> {
        let match_block = hunk.get_match_block();
        if match_block.is_empty() {
            return Err(HunkApplyError::ContextNotFound);
        }

        target_lines
            .windows(match_block.len())
            .enumerate()
            .find(|(_, window)| {
                window.iter().map(|s| s.as_ref()).eq(match_block.iter().copied())
            })
            .map(|(i, _)| (
                HunkLocation { start_index: i, length: match_block.len() },
                MatchType::Exact
            ))
            .ok_or(HunkApplyError::ContextNotFound)
    }
}

Required Methods§

Source

fn find_location<T: AsRef<str> + Sync>( &self, hunk: &Hunk, target_lines: &[T], ) -> Result<(HunkLocation, MatchType), HunkApplyError>

Finds the location to apply a hunk to a slice of lines.

This is the core method for any HunkFinder implementation. It encapsulates the search logic used to determine where a hunk’s changes should be applied within the target content. Implementations should return the location and the type of match found, or an error if no suitable location can be determined.

§Arguments
  • hunk - The Hunk to locate.
  • target_lines - A slice of strings representing the content to search within.
§Returns

A tuple containing the HunkLocation and the MatchType on success.

§Errors

Returns Err(HunkApplyError) if no suitable location could be found.

§Examples
// 1. Create a hunk to search for.
let diff = "```diff\n--- a/f\n+++ b/f\n@@ -1,2 +1,2\n line 1\n-line 2\n+line two\n```";
let hunk = parse_single_patch(diff)?.hunks.remove(0);

// 2. Define the content to search within.
let target_lines = vec!["line 1", "line 2"];

// 3. Instantiate a finder and call the method.
let options = ApplyOptions::new();
let finder = DefaultHunkFinder::new(&options);
let (location, match_type) = finder.find_location(&hunk, &target_lines)?;

// 4. Check the result.
assert_eq!(location, HunkLocation { start_index: 0, length: 2 });
assert!(matches!(match_type, MatchType::Exact));

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§