use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ArtifactLocation {
pub uri: Option<String>,
pub uri_base_id: Option<String>,
pub index: Option<i32>,
pub description: Option<crate::types::Message>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Artifact {
pub location: Option<ArtifactLocation>,
pub parent_index: Option<i32>,
pub offset: Option<i32>,
pub length: Option<i32>,
pub roles: Option<Vec<ArtifactRole>>,
pub mime_type: Option<String>,
pub contents: Option<ArtifactContent>,
pub encoding: Option<String>,
pub source_language: Option<String>,
pub hashes: Option<Vec<Hash>>,
pub last_modified_time_utc: Option<String>,
pub description: Option<crate::types::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>,
pub rendered: Option<MultiformatMessageString>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MultiformatMessageString {
pub text: Option<String>,
pub markdown: Option<String>,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ArtifactRole {
AnalysisTarget,
Attachment,
ResponseFile,
ResultFile,
StandardStream,
TracedFile,
Unmodified,
Modified,
Added,
Deleted,
Renamed,
Translated,
ConfigurationFile,
PolicyFile,
DebugOutputFile,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Hash {
pub value: String,
pub algorithm: String,
#[serde(flatten)]
pub properties: Option<HashMap<String, serde_json::Value>>,
}
impl ArtifactLocation {
pub fn new(uri: impl Into<String>) -> Self {
Self {
uri: Some(uri.into()),
uri_base_id: None,
index: None,
description: None,
properties: None,
}
}
pub fn from_index(index: i32) -> Self {
Self {
uri: None,
uri_base_id: None,
index: Some(index),
description: None,
properties: None,
}
}
pub fn with_uri_base_id(mut self, uri_base_id: impl Into<String>) -> Self {
self.uri_base_id = Some(uri_base_id.into());
self
}
pub fn with_description(mut self, description: impl Into<crate::types::Message>) -> Self {
self.description = Some(description.into());
self
}
pub fn parse_uri(&self) -> Result<Option<Url>, url::ParseError> {
match &self.uri {
Some(uri) => Ok(Some(Url::parse(uri)?)),
None => Ok(None),
}
}
}
impl Artifact {
pub fn new(location: ArtifactLocation) -> Self {
Self {
location: Some(location),
parent_index: None,
offset: None,
length: None,
roles: None,
mime_type: None,
contents: None,
encoding: None,
source_language: None,
hashes: None,
last_modified_time_utc: None,
description: None,
properties: None,
}
}
pub fn add_role(mut self, role: ArtifactRole) -> Self {
self.roles.get_or_insert_with(Vec::new).push(role);
self
}
pub fn with_mime_type(mut self, mime_type: impl Into<String>) -> Self {
self.mime_type = Some(mime_type.into());
self
}
pub fn with_text_content(mut self, text: impl Into<String>) -> Self {
let content = ArtifactContent {
text: Some(text.into()),
binary: None,
rendered: None,
properties: None,
};
self.contents = Some(content);
self
}
}
impl Hash {
pub fn new(algorithm: impl Into<String>, value: impl Into<String>) -> Self {
Self {
algorithm: algorithm.into(),
value: value.into(),
properties: None,
}
}
pub fn md5(value: impl Into<String>) -> Self {
Self::new("md5", value)
}
pub fn sha1(value: impl Into<String>) -> Self {
Self::new("sha1", value)
}
pub fn sha256(value: impl Into<String>) -> Self {
Self::new("sha256", value)
}
}