|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | +use serde::de::{Error, MapAccess, Visitor}; |
| 5 | +use serde::{Deserialize, Deserializer, Serialize}; |
| 6 | +use serde_with::skip_serializing_none; |
| 7 | +use std::fmt::{self, Formatter}; |
| 8 | + |
| 9 | +/// Attributes of an ELF symbol file. |
| 10 | +#[non_exhaustive] |
| 11 | +#[skip_serializing_none] |
| 12 | +#[derive(Clone, Debug, PartialEq, Serialize)] |
| 13 | +pub struct ELFSourcemapAttributes { |
| 14 | + /// The target CPU architecture. |
| 15 | + #[serde(rename = "arch")] |
| 16 | + pub arch: Option<String>, |
| 17 | + /// The timestamp when the symbol file was created. |
| 18 | + #[serde(rename = "created_at")] |
| 19 | + pub created_at: chrono::DateTime<chrono::Utc>, |
| 20 | + /// The SHA256 hash of the ELF file. |
| 21 | + #[serde(rename = "file_hash")] |
| 22 | + pub file_hash: Option<String>, |
| 23 | + /// The ELF file name. |
| 24 | + #[serde(rename = "file_name")] |
| 25 | + pub file_name: Option<String>, |
| 26 | + /// The GNU build ID (UUID format). |
| 27 | + #[serde(rename = "gnu_build_id")] |
| 28 | + pub gnu_build_id: Option<String>, |
| 29 | + /// The Go build ID (UUID format). |
| 30 | + #[serde(rename = "go_build_id")] |
| 31 | + pub go_build_id: Option<String>, |
| 32 | + /// The type of source map. |
| 33 | + #[serde(rename = "mapkind")] |
| 34 | + pub mapkind: String, |
| 35 | + /// The origin of the ELF file. |
| 36 | + #[serde(rename = "origin")] |
| 37 | + pub origin: Option<String>, |
| 38 | + /// The version of the origin package. |
| 39 | + #[serde(rename = "origin_version")] |
| 40 | + pub origin_version: Option<String>, |
| 41 | + /// The size of the ELF file in bytes. |
| 42 | + #[serde(rename = "size")] |
| 43 | + pub size: i64, |
| 44 | + /// The source of the debug symbols. |
| 45 | + #[serde(rename = "symbol_source")] |
| 46 | + pub symbol_source: Option<String>, |
| 47 | + #[serde(flatten)] |
| 48 | + pub additional_properties: std::collections::BTreeMap<String, serde_json::Value>, |
| 49 | + #[serde(skip)] |
| 50 | + #[serde(default)] |
| 51 | + pub(crate) _unparsed: bool, |
| 52 | +} |
| 53 | + |
| 54 | +impl ELFSourcemapAttributes { |
| 55 | + pub fn new( |
| 56 | + created_at: chrono::DateTime<chrono::Utc>, |
| 57 | + mapkind: String, |
| 58 | + size: i64, |
| 59 | + ) -> ELFSourcemapAttributes { |
| 60 | + ELFSourcemapAttributes { |
| 61 | + arch: None, |
| 62 | + created_at, |
| 63 | + file_hash: None, |
| 64 | + file_name: None, |
| 65 | + gnu_build_id: None, |
| 66 | + go_build_id: None, |
| 67 | + mapkind, |
| 68 | + origin: None, |
| 69 | + origin_version: None, |
| 70 | + size, |
| 71 | + symbol_source: None, |
| 72 | + additional_properties: std::collections::BTreeMap::new(), |
| 73 | + _unparsed: false, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn arch(mut self, value: String) -> Self { |
| 78 | + self.arch = Some(value); |
| 79 | + self |
| 80 | + } |
| 81 | + |
| 82 | + pub fn file_hash(mut self, value: String) -> Self { |
| 83 | + self.file_hash = Some(value); |
| 84 | + self |
| 85 | + } |
| 86 | + |
| 87 | + pub fn file_name(mut self, value: String) -> Self { |
| 88 | + self.file_name = Some(value); |
| 89 | + self |
| 90 | + } |
| 91 | + |
| 92 | + pub fn gnu_build_id(mut self, value: String) -> Self { |
| 93 | + self.gnu_build_id = Some(value); |
| 94 | + self |
| 95 | + } |
| 96 | + |
| 97 | + pub fn go_build_id(mut self, value: String) -> Self { |
| 98 | + self.go_build_id = Some(value); |
| 99 | + self |
| 100 | + } |
| 101 | + |
| 102 | + pub fn origin(mut self, value: String) -> Self { |
| 103 | + self.origin = Some(value); |
| 104 | + self |
| 105 | + } |
| 106 | + |
| 107 | + pub fn origin_version(mut self, value: String) -> Self { |
| 108 | + self.origin_version = Some(value); |
| 109 | + self |
| 110 | + } |
| 111 | + |
| 112 | + pub fn symbol_source(mut self, value: String) -> Self { |
| 113 | + self.symbol_source = Some(value); |
| 114 | + self |
| 115 | + } |
| 116 | + |
| 117 | + pub fn additional_properties( |
| 118 | + mut self, |
| 119 | + value: std::collections::BTreeMap<String, serde_json::Value>, |
| 120 | + ) -> Self { |
| 121 | + self.additional_properties = value; |
| 122 | + self |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl<'de> Deserialize<'de> for ELFSourcemapAttributes { |
| 127 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 128 | + where |
| 129 | + D: Deserializer<'de>, |
| 130 | + { |
| 131 | + struct ELFSourcemapAttributesVisitor; |
| 132 | + impl<'a> Visitor<'a> for ELFSourcemapAttributesVisitor { |
| 133 | + type Value = ELFSourcemapAttributes; |
| 134 | + |
| 135 | + fn expecting(&self, f: &mut Formatter<'_>) -> fmt::Result { |
| 136 | + f.write_str("a mapping") |
| 137 | + } |
| 138 | + |
| 139 | + fn visit_map<M>(self, mut map: M) -> Result<Self::Value, M::Error> |
| 140 | + where |
| 141 | + M: MapAccess<'a>, |
| 142 | + { |
| 143 | + let mut arch: Option<String> = None; |
| 144 | + let mut created_at: Option<chrono::DateTime<chrono::Utc>> = None; |
| 145 | + let mut file_hash: Option<String> = None; |
| 146 | + let mut file_name: Option<String> = None; |
| 147 | + let mut gnu_build_id: Option<String> = None; |
| 148 | + let mut go_build_id: Option<String> = None; |
| 149 | + let mut mapkind: Option<String> = None; |
| 150 | + let mut origin: Option<String> = None; |
| 151 | + let mut origin_version: Option<String> = None; |
| 152 | + let mut size: Option<i64> = None; |
| 153 | + let mut symbol_source: Option<String> = None; |
| 154 | + let mut additional_properties: std::collections::BTreeMap< |
| 155 | + String, |
| 156 | + serde_json::Value, |
| 157 | + > = std::collections::BTreeMap::new(); |
| 158 | + let mut _unparsed = false; |
| 159 | + |
| 160 | + while let Some((k, v)) = map.next_entry::<String, serde_json::Value>()? { |
| 161 | + match k.as_str() { |
| 162 | + "arch" => { |
| 163 | + if v.is_null() { |
| 164 | + continue; |
| 165 | + } |
| 166 | + arch = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 167 | + } |
| 168 | + "created_at" => { |
| 169 | + created_at = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 170 | + } |
| 171 | + "file_hash" => { |
| 172 | + if v.is_null() { |
| 173 | + continue; |
| 174 | + } |
| 175 | + file_hash = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 176 | + } |
| 177 | + "file_name" => { |
| 178 | + if v.is_null() { |
| 179 | + continue; |
| 180 | + } |
| 181 | + file_name = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 182 | + } |
| 183 | + "gnu_build_id" => { |
| 184 | + if v.is_null() { |
| 185 | + continue; |
| 186 | + } |
| 187 | + gnu_build_id = |
| 188 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 189 | + } |
| 190 | + "go_build_id" => { |
| 191 | + if v.is_null() { |
| 192 | + continue; |
| 193 | + } |
| 194 | + go_build_id = |
| 195 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 196 | + } |
| 197 | + "mapkind" => { |
| 198 | + mapkind = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 199 | + } |
| 200 | + "origin" => { |
| 201 | + if v.is_null() { |
| 202 | + continue; |
| 203 | + } |
| 204 | + origin = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 205 | + } |
| 206 | + "origin_version" => { |
| 207 | + if v.is_null() { |
| 208 | + continue; |
| 209 | + } |
| 210 | + origin_version = |
| 211 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 212 | + } |
| 213 | + "size" => { |
| 214 | + size = Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 215 | + } |
| 216 | + "symbol_source" => { |
| 217 | + if v.is_null() { |
| 218 | + continue; |
| 219 | + } |
| 220 | + symbol_source = |
| 221 | + Some(serde_json::from_value(v).map_err(M::Error::custom)?); |
| 222 | + } |
| 223 | + &_ => { |
| 224 | + if let Ok(value) = serde_json::from_value(v.clone()) { |
| 225 | + additional_properties.insert(k, value); |
| 226 | + } |
| 227 | + } |
| 228 | + } |
| 229 | + } |
| 230 | + let created_at = created_at.ok_or_else(|| M::Error::missing_field("created_at"))?; |
| 231 | + let mapkind = mapkind.ok_or_else(|| M::Error::missing_field("mapkind"))?; |
| 232 | + let size = size.ok_or_else(|| M::Error::missing_field("size"))?; |
| 233 | + |
| 234 | + let content = ELFSourcemapAttributes { |
| 235 | + arch, |
| 236 | + created_at, |
| 237 | + file_hash, |
| 238 | + file_name, |
| 239 | + gnu_build_id, |
| 240 | + go_build_id, |
| 241 | + mapkind, |
| 242 | + origin, |
| 243 | + origin_version, |
| 244 | + size, |
| 245 | + symbol_source, |
| 246 | + additional_properties, |
| 247 | + _unparsed, |
| 248 | + }; |
| 249 | + |
| 250 | + Ok(content) |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + deserializer.deserialize_any(ELFSourcemapAttributesVisitor) |
| 255 | + } |
| 256 | +} |
0 commit comments