use relay_protocol::{Annotated, Array, Empty, FromValue, IntoValue, Object, Value};
use crate::processor::ProcessValue;
#[derive(Clone, Debug, Default, PartialEq, Empty, FromValue, IntoValue, ProcessValue)]
#[metastructure(process_func = "process_template_info")]
pub struct TemplateInfo {
#[metastructure(pii = "true", max_chars = 128, max_chars_allowance = 20)]
pub filename: Annotated<String>,
#[metastructure(pii = "true", max_chars = 256, max_chars_allowance = 40)]
pub abs_path: Annotated<String>,
pub lineno: Annotated<u64>,
pub colno: Annotated<u64>,
pub pre_context: Annotated<Array<String>>,
pub context_line: Annotated<String>,
pub post_context: Annotated<Array<String>>,
#[metastructure(additional_properties)]
pub other: Object<Value>,
}
#[cfg(test)]
mod tests {
use relay_protocol::Map;
use similar_asserts::assert_eq;
use super::*;
#[test]
fn test_template_roundtrip() {
let json = r#"{
"filename": "myfile.rs",
"abs_path": "/path/to",
"lineno": 2,
"colno": 42,
"pre_context": [
"fn main() {"
],
"context_line": "unimplemented!()",
"post_context": [
"}"
],
"other": "value"
}"#;
let template_info = Annotated::new(TemplateInfo {
filename: Annotated::new("myfile.rs".to_string()),
abs_path: Annotated::new("/path/to".to_string()),
lineno: Annotated::new(2),
colno: Annotated::new(42),
pre_context: Annotated::new(vec![Annotated::new("fn main() {".to_string())]),
context_line: Annotated::new("unimplemented!()".to_string()),
post_context: Annotated::new(vec![Annotated::new("}".to_string())]),
other: {
let mut map = Map::new();
map.insert(
"other".to_string(),
Annotated::new(Value::String("value".to_string())),
);
map
},
});
assert_eq!(template_info, Annotated::from_json(json).unwrap());
assert_eq!(json, template_info.to_json_pretty().unwrap());
}
#[test]
fn test_template_default_values() {
let json = "{}";
let template_info = Annotated::new(TemplateInfo {
filename: Annotated::empty(),
abs_path: Annotated::empty(),
lineno: Annotated::empty(),
colno: Annotated::empty(),
pre_context: Annotated::empty(),
context_line: Annotated::empty(),
post_context: Annotated::empty(),
other: Object::default(),
});
assert_eq!(template_info, Annotated::from_json(json).unwrap());
assert_eq!(json, template_info.to_json().unwrap());
}
}