Repository: astral-sh/ruff
File: crates/ty_test/src/parser.rs (Rust)

Code before:
pub(crate) struct EmbeddedFileSourceMap {
    start_line_and_line_count: Vec<(usize, usize)>,
}

impl EmbeddedFileSourceMap {
    pub(crate) fn new(
        md_index: &LineIndex,
        dimensions: impl IntoIterator<Item = BacktickOffsets>,
    ) -> EmbeddedFileSourceMap {
        EmbeddedFileSourceMap {
            start_line_and_line_count: dimensions
                .into_iter()
                .map(|d| {
                    let start_line = md_index.line_index(d.0).get();
                    let end_line = md_index.line_index(d.1).get();
                    let code_line_count = (end_line - start_line) - 1;
                    (start_line, code_line_count)
                })
                .collect(),
        }
    }

    pub(crate) fn to_absolute_line_number(
        &self,
        relative_line_number: OneIndexed,
    ) -> anyhow::Result<OneIndexed> {
        let mut relative_line_number = relative_line_number.get();
        let original_relative_line_number = relative_line_number;

        for (start_line, line_count) in &self.start_line_and_line_count {
            if relative_line_number > *line_count {
                relative_line_number -= *line_count;
            } else {
                let absolute_line_number = start_line + relative_line_number;
                return Ok(OneIndexed::new(absolute_line_number)
                    .expect("absolute line number must be >= 1"));
            }
        }

        let total: usize = self.start_line_and_line_count.iter().map(|(_, c)| *c).sum();
        bail!(
            "Relative line {original_relative_line_number} exceeds \
             total embedded code lines ({total}). This usually means a trailing `# revealed:` without \
             a matching `reveal_type(...)` line above."
        );
    }

    pub(crate) fn last_absolute_line_number(&self) -> Option<OneIndexed> {
        self.start_line_and_line_count
            .last()
            .and_then(|(start_line, line_count)| OneIndexed::new(start_line + line_count))

Review comment (suggestion):
I think I would change this to be an `std::result::Result<OneIndexed, OneIndexed>` where
the error case returns the last line number (`last_absolute_line_number`). 

This leaves deciding on what error message to use up to the caller

Code after:
pub(crate) struct EmbeddedFileSourceMap {
    start_line_and_line_count: Vec<(usize, usize)>,
}

impl EmbeddedFileSourceMap {
    pub(crate) fn new(
        md_index: &LineIndex,
        dimensions: impl IntoIterator<Item = BacktickOffsets>,
    ) -> EmbeddedFileSourceMap {
        EmbeddedFileSourceMap {
            start_line_and_line_count: dimensions
                .into_iter()
                .map(|d| {
                    let start_line = md_index.line_index(d.0).get();
                    let end_line = md_index.line_index(d.1).get();
                    let code_line_count = (end_line - start_line) - 1;
                    (start_line, code_line_count)
                })
                .collect(),
        }
    }

    /// Returns the absolute line number in the markdown file for a given line number
    /// relative to the concatenated code blocks.
    ///
    /// Returns an `Err` if the relative line number is out of bounds where
    /// the returned value is the absolute line number of the last code block.
    ///
    /// # Panics
    ///  If called when the markdown file has no code blocks.
    pub(crate) fn to_absolute_line_number(
        &self,
        relative_line_number: OneIndexed,
    ) -> std::result::Result<OneIndexed, OneIndexed> {
        let mut relative_line_number = relative_line_number.get();

        for (start_line, line_count) in &self.start_line_and_line_count {
            if relative_line_number > *line_count {
                relative_line_number -= *line_count;
            } else {
                let absolute_line_number = start_line + relative_line_number;
                return Ok(OneIndexed::new(absolute_line_number)
                    .expect("absolute line number must be >= 1"));
            }
        }

        let last_line_number = self
            .start_line_and_line_count
            .last()
            .and_then(|(start_line, line_count)| OneIndexed::new(start_line + line_count));