use crate::types::{ArtifactLocation, Message};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Location {
pub id: Option<i32>,
pub physical_location: Option<PhysicalLocation>,
pub logical_locations: Option<Vec<LogicalLocation>>,
pub message: Option<Message>,
pub annotations: Option<Vec<Region>>,
pub relationships: Option<Vec<LocationRelationship>>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PhysicalLocation {
pub artifact_location: Option<ArtifactLocation>,
pub region: Option<Region>,
pub context_region: Option<Region>,
pub address: Option<Address>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Region {
pub start_line: Option<i32>,
pub start_column: Option<i32>,
pub end_line: Option<i32>,
pub end_column: Option<i32>,
pub char_offset: Option<i32>,
pub char_length: Option<i32>,
pub byte_offset: Option<i32>,
pub byte_length: Option<i32>,
pub snippet: Option<ArtifactContent>,
pub message: Option<Message>,
pub source_language: Option<String>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LogicalLocation {
pub name: Option<String>,
pub index: Option<i32>,
pub fully_qualified_name: Option<String>,
pub decorated_name: Option<String>,
pub parent_index: Option<i32>,
pub kind: Option<String>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Address {
pub absolute_address: Option<i64>,
pub relative_address: Option<i32>,
pub length: Option<i32>,
pub kind: Option<String>,
pub name: Option<String>,
pub fully_qualified_name: Option<String>,
pub offset_from_parent: Option<i32>,
pub parent_index: Option<i32>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct LocationRelationship {
pub target: i32,
pub kinds: Option<Vec<String>>,
pub description: Option<Message>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArtifactContent {
pub text: Option<String>,
pub binary: Option<String>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
impl Location {
pub fn new() -> Self {
Self {
id: None,
physical_location: None,
logical_locations: None,
message: None,
annotations: None,
relationships: None,
properties: None,
}
}
pub fn with_physical_location(physical_location: PhysicalLocation) -> Self {
Self {
id: None,
physical_location: Some(physical_location),
logical_locations: None,
message: None,
annotations: None,
relationships: None,
properties: None,
}
}
pub fn with_message(mut self, message: impl Into<Message>) -> Self {
self.message = Some(message.into());
self
}
pub fn add_logical_location(mut self, logical_location: LogicalLocation) -> Self {
self.logical_locations
.get_or_insert_with(Vec::new)
.push(logical_location);
self
}
}
impl PhysicalLocation {
pub fn new() -> Self {
Self {
artifact_location: None,
region: None,
context_region: None,
address: None,
properties: None,
}
}
pub fn with_artifact_location(artifact_location: ArtifactLocation) -> Self {
Self {
artifact_location: Some(artifact_location),
region: None,
context_region: None,
address: None,
properties: None,
}
}
pub fn with_region(mut self, region: Region) -> Self {
self.region = Some(region);
self
}
pub fn with_context_region(mut self, context_region: Region) -> Self {
self.context_region = Some(context_region);
self
}
}
impl Region {
pub fn new() -> Self {
Self {
start_line: None,
start_column: None,
end_line: None,
end_column: None,
char_offset: None,
char_length: None,
byte_offset: None,
byte_length: None,
snippet: None,
message: None,
source_language: None,
properties: None,
}
}
pub fn from_coordinates(
start_line: i32,
start_column: i32,
end_line: i32,
end_column: i32,
) -> Self {
Self {
start_line: Some(start_line),
start_column: Some(start_column),
end_line: Some(end_line),
end_column: Some(end_column),
char_offset: None,
char_length: None,
byte_offset: None,
byte_length: None,
snippet: None,
message: None,
source_language: None,
properties: None,
}
}
pub fn from_line(line: i32) -> Self {
Self {
start_line: Some(line),
start_column: None,
end_line: Some(line),
end_column: None,
char_offset: None,
char_length: None,
byte_offset: None,
byte_length: None,
snippet: None,
message: None,
source_language: None,
properties: None,
}
}
pub fn from_char_offset(offset: i32, length: i32) -> Self {
Self {
start_line: None,
start_column: None,
end_line: None,
end_column: None,
char_offset: Some(offset),
char_length: Some(length),
byte_offset: None,
byte_length: None,
snippet: None,
message: None,
source_language: None,
properties: None,
}
}
pub fn with_snippet(mut self, text: impl Into<String>) -> Self {
self.snippet = Some(ArtifactContent {
text: Some(text.into()),
binary: None,
properties: None,
});
self
}
}
impl LogicalLocation {
pub fn new() -> Self {
Self {
name: None,
index: None,
fully_qualified_name: None,
decorated_name: None,
parent_index: None,
kind: None,
properties: None,
}
}
pub fn with_name(name: impl Into<String>) -> Self {
Self {
name: Some(name.into()),
index: None,
fully_qualified_name: None,
decorated_name: None,
parent_index: None,
kind: None,
properties: None,
}
}
pub fn with_fully_qualified_name(mut self, name: impl Into<String>) -> Self {
self.fully_qualified_name = Some(name.into());
self
}
pub fn with_kind(mut self, kind: impl Into<String>) -> Self {
self.kind = Some(kind.into());
self
}
}
impl Default for Location {
fn default() -> Self {
Self::new()
}
}
impl Default for PhysicalLocation {
fn default() -> Self {
Self::new()
}
}
impl Default for Region {
fn default() -> Self {
Self::new()
}
}
impl Default for LogicalLocation {
fn default() -> Self {
Self::new()
}
}