1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! Owned variants of the `annotate_snippets` library structs
use std::borrow::Cow;

use annotate_snippets::{
    display_list::FormatOptions,
    snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation},
};

use debris_common::{InputFiles, Span};

/// An owned counterpart to the `annotate_snippets::Snippet` struct
#[derive(Debug)]
pub struct SnippetOwned<'a> {
    pub title: Cow<'a, str>,
    pub id: Option<Cow<'a, str>>,
    pub annotation_type: AnnotationType,
    pub slices: Vec<SliceOwned<'a>>,
    pub footer: Vec<AnnotationOwned<'a>>,
}

/// An owned counterpart to the `annotate_snippets::Slice` struct
#[derive(Debug)]
pub struct SliceOwned<'a> {
    pub source: &'a str,
    pub origin: Option<&'a str>,
    pub annotations: Vec<SourceAnnotationOwned>,
}

/// An owned counterpart to the `annotate_snippets::SourceAnnotation` struct
#[derive(Debug)]
pub struct AnnotationOwned<'a> {
    pub id: Option<Cow<'a, str>>,
    pub label: Option<Cow<'a, str>>,
    pub annotation_type: AnnotationType,
}

/// An owned counterpart to the `annotate_snippets::SourceAnnotation` struct
#[derive(Debug)]
pub struct SourceAnnotationOwned {
    pub annotation_type: AnnotationType,
    pub range: Span,
    pub label: String,
}

impl SnippetOwned<'_> {
    pub fn as_snippet(&self, input_files: &InputFiles) -> Snippet {
        Snippet {
            title: Some(Annotation {
                annotation_type: self.annotation_type,
                id: self.id.as_deref(),
                label: Some(&self.title),
            }),
            slices: self
                .slices
                .iter()
                .map(|slice| slice.as_slice(input_files))
                .collect(),
            footer: self
                .footer
                .iter()
                .map(AnnotationOwned::as_annotation)
                .collect(),
            opt: FormatOptions {
                color: super::COLORED,
                ..Default::default()
            },
        }
    }
}

impl SliceOwned<'_> {
    pub fn as_slice(&self, input_files: &InputFiles) -> Slice {
        Slice {
            source: self.source,
            line_start: 1,
            annotations: self
                .annotations
                .iter()
                .map(|ann| ann.as_source_annotation(input_files))
                .collect(),
            origin: self.origin,
            fold: true,
        }
    }
}

impl AnnotationOwned<'_> {
    pub fn as_annotation(&self) -> Annotation {
        Annotation {
            annotation_type: self.annotation_type,
            id: self.id.as_deref(),
            label: self.label.as_deref(),
        }
    }
}

impl SourceAnnotationOwned {
    pub fn as_source_annotation(&self, input_files: &InputFiles) -> SourceAnnotation {
        let text = &input_files.get_span_code(self.range).get_code().source;
        let range = self.range.char_bounds(text);
        SourceAnnotation {
            range,
            label: &self.label,
            annotation_type: self.annotation_type,
        }
    }
}