Repository: astral-sh/ruff
File: crates/ruff_db/src/parsed.rs (Rust)

Code before:
pub fn parsed_module_impl(db: &dyn Db, file: File) -> Parsed<ModModule> {
    let source = source_text(db, file);
    let ty = file.source_type(db);

    let target_version = db.python_version();
    let options = ParseOptions::from(ty).with_target_version(target_version);
    parse_unchecked(&source, options)
        .try_into_module()
        .expect("PySourceType always parses into a module")
}

pub fn parsed_string_annotation(
    source: &str,
    string: &StringLiteral,
) -> Result<Parsed<ModExpression>, ParseError> {
    let expr = parse_string_annotation(source, string)?;

    // We need the sub-ast of the string annotation to be indexed
    indexed::ensure_indexed(&expr, string.node_index().load()).map_err(|err| {
        let message = match err {
            NodeIndexError::NoParent => {
                "internal error: string annotation's parent had no NodeIndex".to_owned()
            }
            NodeIndexError::TooNested => "too many levels of nested string annotations, remove the redundant nested quotes".to_owned(),
            NodeIndexError::OverflowedIndices => {
                "file too long for string annotations, either break up the file or don't use string annotations".to_owned()
            }
            NodeIndexError::OverflowedSubIndices => {
                "file too long for nested string annotations, remove the redundant nested quotes".to_owned()
            }
            NodeIndexError::ExhaustedSubIndices => {
                "string annotation is too long, consider introducing type aliases to simplify".to_owned()
            }
            NodeIndexError::ExhaustedSubSubIndices => {
                "nested string annotation is too long, remove the redundant nested quotes".to_owned()
            }
        };

        ParseError {
            error: ParseErrorType::OtherError(message),
            location: string.range,
        }
    })?;

    Ok(expr)
}

/// A wrapper around a parsed module.
///
/// This type manages instances of the module AST. A particular instance of the AST
/// is represented with the [`ParsedModuleRef`] type.

Review comment (bug):
nit

```suggestion
            NodeIndexError::TooNested => "too many levels of nested string annotations; remove the redundant nested quotes".to_owned(),
            NodeIndexError::OverflowedIndices => {
                "file too long for string annotations: either break up the file or don't use string annotations".to_owned()
```

Code after:
pub fn parsed_module_impl(db: &dyn Db, file: File) -> Parsed<ModModule> {
    let source = source_text(db, file);
    let ty = file.source_type(db);

    let target_version = db.python_version();
    let options = ParseOptions::from(ty).with_target_version(target_version);
    parse_unchecked(&source, options)
        .try_into_module()
        .expect("PySourceType always parses into a module")
}

pub fn parsed_string_annotation(
    source: &str,
    string: &StringLiteral,
) -> Result<Parsed<ModExpression>, ParseError> {
    let expr = parse_string_annotation(source, string)?;

    // We need the sub-ast of the string annotation to be indexed
    indexed::ensure_indexed(&expr, string.node_index().load()).map_err(|err| {
        let message = match err {
            NodeIndexError::NoParent => {
                "internal error: string annotation's parent had no NodeIndex".to_owned()
            }
            NodeIndexError::TooNested => "too many levels of nested string annotations; remove the redundant nested quotes".to_owned(),
            NodeIndexError::OverflowedIndices => {
                "file too long for string annotations; either break up the file or don't use string annotations".to_owned()
            }
            NodeIndexError::OverflowedSubIndices => {
                "file too long for nested string annotations; remove the redundant nested quotes".to_owned()
            }
            NodeIndexError::ExhaustedSubIndices => {
                "string annotation is too long; consider introducing type aliases to simplify".to_owned()
            }
            NodeIndexError::ExhaustedSubSubIndices => {
                "nested string annotation is too long; remove the redundant nested quotes".to_owned()
            }
        };

        ParseError {
            error: ParseErrorType::OtherError(message),
            location: string.range,
        }
    })?;

    Ok(expr)
}

/// A wrapper around a parsed module.
///
/// This type manages instances of the module AST. A particular instance of the AST
/// is represented with the [`ParsedModuleRef`] type.