Skip to content

Cannot create a Snippet without know whether annotation or patch #300

@epage

Description

@epage

I have code like the following

fn typo_to_group<'t>(msg: &'t Typo<'t>) -> Group<'t> {
    let title = match &msg.corrections {
        typos::Status::Valid => unimplemented!("never valid words to report"),
        typos::Status::Invalid => {
            format!("`{}` is disallowed", msg.typo,)
        }
        typos::Status::Corrections(corrections) => {
            format!(
                "`{}` should be {}",
                msg.typo,
                itertools::join(corrections.iter().map(|s| format!("`{s}`")), ", ")
            )
        }
    };
    let group = Group::with_title(Level::ERROR.primary_title(Cow::Owned(title)));
    let group = match &msg.context {
        Some(Context::File(context)) => {
            let path = context.path.as_os_str().to_string_lossy();
            let line = String::from_utf8_lossy(msg.buffer.as_ref());
            let snippet = Snippet::source(line)
                .path(path)
                .line_start(context.line_num);
            append_corrections(msg, 0, snippet, group)
        }
        Some(Context::Path(context)) => {
            let path = context
                .path
                .parent()
                .unwrap_or(std::path::Path::new("."))
                .join("");
            let path = path.as_os_str().to_string_lossy();
            let line = context.path.as_os_str().to_string_lossy();
            let snippet = Snippet::source(line);
            append_corrections(msg, path.len(), snippet, group)
        }
        Some(_) | None => group,
    };
    group
}

fn append_corrections<'t>(
    msg: &'t Typo<'t>,
    offset: usize,
    snippet: Snippet<'t, Annotation<'t>>,
    group: Group<'t>,
) -> Group<'t> {
    let span_start = msg.byte_offset + offset;
    let span_end = span_start + msg.typo.len();
    let span = span_start..span_end;

    match &msg.corrections {
        typos::Status::Valid => unimplemented!("never valid words to report"),
        typos::Status::Invalid => {
            let snippet = snippet.annotation(AnnotationKind::Primary.span(span));
            group.element(snippet)
        }
        typos::Status::Corrections(corrections) => {
            let snippet = snippet.annotation(AnnotationKind::Primary.span(span));
            group.element(snippet)
        }
    }
}

The problem is that I want to make the Corrections variant in append_corrections use Patch but can't because the passed in snippet has to have one specific type.

One option is for us to create Snippets with T = () and then annotation can convert it to T= Annotation and patch to T= Patch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: enhancementM-breaking-changeMeta: Implementing or merging this will introduce a breaking change

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions