cairo_lang_sierra_generator/
statements_code_locations.rs

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
use std::collections::HashMap;

use cairo_lang_sierra::debug_info::Annotations;
use cairo_lang_sierra::program::StatementIdx;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use serde::{Deserialize, Serialize};

/// A full path to a cairo source file.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct SourceFileFullPath(pub String);

/// A location in a cairo source file.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct SourceCodeLocation {
    /// Line index, 0 based.
    pub line: usize,
    /// Character index inside the line, 0 based.
    pub col: usize,
}

/// A location in a cairo source file.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct SourceCodeSpan {
    /// Beginning of the text span in the cairo source file.
    pub start: SourceCodeLocation,
    /// End of the text span in the cairo source file, not included.
    pub end: SourceCodeLocation,
}

/// The mapping between sierra statement indexes and locations in cairo code
/// (if obtainable) which caused the statement to be generated.
///
/// Should be created using
/// [`crate::statements_locations::StatementsLocations::extract_statements_source_code_locations`].
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct StatementsSourceCodeLocations {
    pub statements_to_code_location_map:
        HashMap<StatementIdx, Vec<(SourceFileFullPath, SourceCodeSpan)>>,
}

impl From<StatementsSourceCodeLocations> for Annotations {
    fn from(value: StatementsSourceCodeLocations) -> Self {
        let mapping = serde_json::to_value(value.statements_to_code_location_map).unwrap();
        OrderedHashMap::from([(
            "github.com/software-mansion/cairo-coverage".to_string(),
            serde_json::Value::from_iter([("statements_code_locations", mapping)]),
        )])
    }
}