allure_rust_core/
attachment.rs1use serde_json::Value as JsonValue;
2
3#[derive(Debug, Clone, Copy)]
4pub enum AttachmentType {
5 Text,
6 Html,
7 Xml,
8 Json,
9 Yaml,
10 Csv,
11 Tsv,
12 UriList,
13 Png,
14 Jpeg,
15 Gif,
16 Bmp,
17 Tiff,
18 Svg,
19 ImageDiff,
20 Mp4,
21 Ogg,
22 Webm,
23}
24
25impl AttachmentType {
26 pub fn mime_type(&self) -> &'static str {
27 match self {
28 AttachmentType::Text => "text/plain",
29 AttachmentType::Html => "text/html",
30 AttachmentType::Xml => "application/xml",
31 AttachmentType::Json => "application/json",
32 AttachmentType::Yaml => "application/yaml",
33 AttachmentType::Csv => "text/csv",
34 AttachmentType::Tsv => "text/tab-separated-values",
35 AttachmentType::UriList => "text/uri-list",
36 AttachmentType::Png => "image/png",
37 AttachmentType::Jpeg => "image/jpeg",
38 AttachmentType::Gif => "image/gif",
39 AttachmentType::Bmp => "image/bmp",
40 AttachmentType::Tiff => "image/tiff",
41 AttachmentType::Svg => "image/svg+xml",
42 AttachmentType::ImageDiff => "application/vnd.allure.image.diff",
43 AttachmentType::Mp4 => "video/mp4",
44 AttachmentType::Ogg => "video/ogg",
45 AttachmentType::Webm => "video/webm",
46 }
47 }
48
49 pub fn extension(&self) -> &'static str {
50 match self {
51 AttachmentType::Text => "txt",
52 AttachmentType::Html => "html",
53 AttachmentType::Xml => "xml",
54 AttachmentType::Json => "json",
55 AttachmentType::Yaml => "yaml",
56 AttachmentType::Csv => "csv",
57 AttachmentType::Tsv => "tsv",
58 AttachmentType::UriList => "uri",
59 AttachmentType::Png => "png",
60 AttachmentType::Jpeg => "jpg",
61 AttachmentType::Gif => "gif",
62 AttachmentType::Bmp => "bmp",
63 AttachmentType::Tiff => "tiff",
64 AttachmentType::Svg => "svg",
65 AttachmentType::ImageDiff => "diff.png",
66 AttachmentType::Mp4 => "mp4",
67 AttachmentType::Ogg => "ogg",
68 AttachmentType::Webm => "webm",
69 }
70 }
71}
72
73pub trait IntoAttachment {
74 fn into_bytes(self) -> Vec<u8>;
75 fn attachment_type(&self) -> AttachmentType;
76}
77
78impl IntoAttachment for &str {
79 fn into_bytes(self) -> Vec<u8> {
80 self.as_bytes().to_vec()
81 }
82
83 fn attachment_type(&self) -> AttachmentType {
84 AttachmentType::Text
85 }
86}
87
88impl IntoAttachment for String {
89 fn into_bytes(self) -> Vec<u8> {
90 self.into_bytes()
91 }
92
93 fn attachment_type(&self) -> AttachmentType {
94 AttachmentType::Text
95 }
96}
97
98impl IntoAttachment for &[u8] {
99 fn into_bytes(self) -> Vec<u8> {
100 self.to_vec()
101 }
102
103 fn attachment_type(&self) -> AttachmentType {
104 AttachmentType::Text
105 }
106}
107
108impl IntoAttachment for Vec<u8> {
109 fn into_bytes(self) -> Vec<u8> {
110 self
111 }
112
113 fn attachment_type(&self) -> AttachmentType {
114 AttachmentType::Text
115 }
116}
117
118impl IntoAttachment for JsonValue {
119 fn into_bytes(self) -> Vec<u8> {
120 serde_json::to_string_pretty(&self)
121 .unwrap_or_else(|_| "{}".to_string())
122 .into_bytes()
123 }
124
125 fn attachment_type(&self) -> AttachmentType {
126 AttachmentType::Json
127 }
128}
129
130pub struct TypedAttachment<T> {
131 pub content: T,
132 pub attachment_type: AttachmentType,
133}
134
135impl<T: IntoAttachment> IntoAttachment for TypedAttachment<T> {
136 fn into_bytes(self) -> Vec<u8> {
137 self.content.into_bytes()
138 }
139
140 fn attachment_type(&self) -> AttachmentType {
141 self.attachment_type
142 }
143}