dsh_api/
display.rs

1//! # `Display` implementations for selected types
2//!
3//! This module provides implementations of the [`Display`] trait for selected types.
4//!
5//! * [`ActualCertificate`]
6//! * [`AllocationStatus`]
7//! * [`AppCatalogApp`]
8//! * [`AppCatalogAppConfiguration`]
9//! * [`AppCatalogAppResourcesValue`]
10//! * [`AppCatalogManifest`]
11//! * [`Application`]
12//! * [`ApplicationSecret`]
13//! * [`ApplicationVolumes`]
14//! * [`Bucket`]
15//! * [`BucketStatus`]
16//! * [`Certificate`]
17//! * [`CertificateStatus`]
18//! * [`Empty`]
19//! * [`HealthCheck`]
20//! * [`LimitValue`]
21//! * [`LimitValueCertificateCount`]
22//! * [`LimitValueConsumerRate`]
23//! * [`LimitValueCpu`]
24//! * [`LimitValueKafkaAclGroupCount`]
25//! * [`LimitValueMem`]
26//! * [`LimitValuePartitionCount`]
27//! * [`LimitValueProducerRate`]
28//! * [`LimitValueRequestRate`]
29//! * [`LimitValueSecretCount`]
30//! * [`LimitValueTopicCount`]
31//! * [`ManagedStream`]
32//! * [`ManagedStreamId`]
33//! * [`ManagedTenant`]
34//! * [`Metrics`]
35//! * [`Notification`]
36//! * [`PathSpec`]
37//! * [`PortMapping`]
38//! * [`PublicManagedStream`]
39//! * [`Secret`]
40//! * [`Task`]
41//! * [`TaskStatus`]
42//! * [`Topic`]
43//! * [`TopicStatus`]
44//! * [`Vhost`]
45//! * [`Volume`]
46//! * [`VolumeStatus`]
47
48use crate::types::{
49  ActualCertificate, AllocationStatus, AppCatalogApp, AppCatalogAppConfiguration, AppCatalogAppResourcesValue, AppCatalogManifest, Application, ApplicationSecret,
50  ApplicationVolumes, Bucket, BucketStatus, Certificate, CertificateStatus, Empty, HealthCheck, LimitValue, LimitValueCertificateCount, LimitValueConsumerRate, LimitValueCpu,
51  LimitValueKafkaAclGroupCount, LimitValueMem, LimitValuePartitionCount, LimitValueProducerRate, LimitValueRequestRate, LimitValueSecretCount, LimitValueTopicCount, ManagedStream,
52  ManagedStreamId, ManagedTenant, Metrics, Notification, PathSpec, PortMapping, PublicManagedStream, Secret, Task, TaskStatus, Topic, TopicStatus, Vhost, Volume, VolumeStatus,
53};
54use std::collections::HashMap;
55use std::fmt::{Display, Formatter};
56use std::ops::Deref;
57
58impl Display for ActualCertificate {
59  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
60    write!(f, "key: {}", self.key_secret)?;
61    if let Some(ref passphrase_secret) = self.passphrase_secret {
62      write!(f, ", passphrase: {}", passphrase_secret)?;
63    }
64    write!(f, ", cert chain: {}", self.cert_chain_secret)
65  }
66}
67
68impl Display for AllocationStatus {
69  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70    if self.provisioned {
71      write!(f, "provisioned")?;
72    } else {
73      write!(f, "not-provisioned")?;
74    }
75    if let Some(ref derived_from) = self.derived_from {
76      write!(f, ", derived from: {}", derived_from)?;
77    }
78    if !self.notifications.is_empty() {
79      write!(
80        f,
81        ", notifications: [{}]",
82        self
83          .notifications
84          .iter()
85          .map(|notification| notification.to_string())
86          .collect::<Vec<_>>()
87          .join(", ")
88      )?;
89    };
90    Ok(())
91  }
92}
93
94impl Display for AppCatalogApp {
95  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
96    write!(f, "name: {}", self.name)?;
97    if let Some(ref configuraton) = self.configuration {
98      write!(f, ", configuration: {}", configuraton)?;
99    }
100    write!(f, ", manifest urn: {}", self.manifest_urn)
101  }
102}
103
104impl Display for AppCatalogAppConfiguration {
105  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
106    write!(f, "name: {}", self.name)?;
107    write!(f, ", manifest urn: {}", self.manifest_urn)?;
108    if self.stopped {
109      write!(f, ", stopped")?;
110    }
111    for (key, value) in &self.configuration {
112      write!(f, ", {}: {}", key, value)?;
113    }
114    Ok(())
115  }
116}
117
118impl Display for AppCatalogAppResourcesValue {
119  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
120    match self {
121      Self::Application(application) => write!(f, "application({})", application),
122      Self::Bucket(bucket) => write!(f, "bucket({})", bucket),
123      Self::Certificate(certificate) => write!(f, "certificate({})", certificate),
124      Self::Secret(secret) => write!(f, "secret({})", secret),
125      Self::Topic(topic) => write!(f, "topic({})", topic),
126      Self::Vhost(vhost) => write!(f, "vhost({})", vhost),
127      Self::Volume(volume) => write!(f, "volume({})", volume),
128    }
129  }
130}
131
132impl Display for AppCatalogManifest {
133  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
134    if self.draft {
135      write!(f, "draft, {}", self.last_modified)?
136    } else {
137      write!(f, "{}", self.last_modified)?
138    }
139    write!(f, "payload: {} bytes", self.payload.len())
140  }
141}
142
143impl Display for Application {
144  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
145    write!(
146      f,
147      "instances: {}, cpus: {}, mem: {}, token: {}, single: {}, image: {}",
148      self.instances, self.cpus, self.mem, self.needs_token, self.single_instance, self.image
149    )
150  }
151}
152
153impl Display for ApplicationSecret {
154  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
155    write!(
156      f,
157      "name: {}, injections: {}",
158      self.name,
159      self
160        .injections
161        .iter()
162        .map(|injection| { format!("[{}]", injection.iter().map(|kv| { format!("{}->{}", kv.0, kv.1) }).collect::<Vec<_>>().join(", ")) })
163        .collect::<Vec<_>>()
164        .join("")
165    )
166  }
167}
168
169impl Display for ApplicationVolumes {
170  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
171    write!(f, "{}", self.name)
172  }
173}
174
175impl Display for Bucket {
176  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
177    match (self.encrypted, self.versioned) {
178      (false, false) => Ok(()),
179      (false, true) => write!(f, "versioned"),
180      (true, false) => write!(f, "encrypted"),
181      (true, true) => write!(f, "encrypted, versioned"),
182    }
183  }
184}
185
186impl Display for BucketStatus {
187  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
188    write!(f, "status: {}", self.status)?;
189    if let Some(ref actual) = self.actual {
190      write!(f, ", actual: {}", actual)?;
191    }
192    if let Some(ref configuration) = self.configuration {
193      write!(f, ", configuration: {}", configuration)?;
194    }
195    Ok(())
196  }
197}
198
199impl Display for Certificate {
200  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
201    write!(f, "key: {}", self.key_secret)?;
202    if let Some(ref passphrase_secret) = self.passphrase_secret {
203      write!(f, ", passphrase: {}", passphrase_secret)?;
204    }
205    write!(f, ", cert chain: {}", self.cert_chain_secret)
206  }
207}
208
209impl Display for CertificateStatus {
210  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
211    write!(f, "status: {}", self.status)?;
212    if let Some(ref actual) = self.actual {
213      write!(f, ", actual: {}", actual)?;
214    }
215    if let Some(ref configuration) = self.configuration {
216      write!(f, ", configuration: {}", configuration)?;
217    }
218    Ok(())
219  }
220}
221
222impl Display for Empty {
223  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
224    write!(f, "empty")
225  }
226}
227
228impl Display for HealthCheck {
229  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
230    if let Some(protocol) = self.protocol {
231      write!(f, "{}::", protocol.to_string())?
232    }
233    write!(f, "{}:{}", self.path, self.port)
234  }
235}
236
237impl Display for LimitValue {
238  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
239    match self {
240      LimitValue::CertificateCount(count) => write!(f, "{}", count),
241      LimitValue::ConsumerRate(rate) => write!(f, "{}", rate),
242      LimitValue::Cpu(cpu) => write!(f, "{}", cpu),
243      LimitValue::KafkaAclGroupCount(count) => write!(f, "{}", count),
244      LimitValue::Mem(mem) => write!(f, "{}", mem),
245      LimitValue::PartitionCount(count) => write!(f, "{}", count),
246      LimitValue::ProducerRate(rate) => write!(f, "{}", rate),
247      LimitValue::RequestRate(rate) => write!(f, "{}", rate),
248      LimitValue::SecretCount(count) => write!(f, "{}", count),
249      LimitValue::TopicCount(count) => write!(f, "{}", count),
250    }
251  }
252}
253
254impl Display for LimitValueCertificateCount {
255  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
256    write!(f, "certificates: {}", self.value)
257  }
258}
259
260impl Display for LimitValueConsumerRate {
261  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
262    write!(f, "consumer rate: {}", self.value)
263  }
264}
265
266impl Display for LimitValueCpu {
267  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
268    write!(f, "cpu: {}", self.value)
269  }
270}
271
272impl Display for LimitValueKafkaAclGroupCount {
273  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
274    write!(f, "kafka acl groups: {}", self.value)
275  }
276}
277
278impl Display for LimitValueMem {
279  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
280    write!(f, "mem: {}", self.value)
281  }
282}
283
284impl Display for LimitValuePartitionCount {
285  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
286    write!(f, "partitions: {}", self.value)
287  }
288}
289
290impl Display for LimitValueProducerRate {
291  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
292    write!(f, "producer rate: {}", self.value)
293  }
294}
295
296impl Display for LimitValueRequestRate {
297  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
298    write!(f, "request rate: {}", self.value)
299  }
300}
301
302impl Display for LimitValueSecretCount {
303  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
304    write!(f, "secrets: {}", self.value)
305  }
306}
307
308impl Display for LimitValueTopicCount {
309  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
310    write!(f, "topics: {}", self.value)
311  }
312}
313
314impl Display for ManagedStream {
315  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
316    write_topic(f, Some("internal"), self.partitions, self.replication_factor, &self.kafka_properties)?;
317    Ok(())
318  }
319}
320
321impl Display for ManagedStreamId {
322  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
323    write!(f, "{}", self.deref())
324  }
325}
326
327impl Display for ManagedTenant {
328  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
329    write!(f, "name: {}", self.name)?;
330    let enabled_services = self
331      .services
332      .iter()
333      .filter_map(|service| if service.enabled { Some(service.name.to_string()) } else { None })
334      .collect::<Vec<_>>();
335    if enabled_services.is_empty() {
336      Ok(())
337    } else {
338      write!(f, " ({})", enabled_services.join(", "))
339    }
340  }
341}
342
343impl Display for Metrics {
344  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
345    write!(f, "{}:{}", self.path, self.port)
346  }
347}
348
349impl Display for Notification {
350  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
351    write!(f, "remove: {}", self.remove)?;
352    if !self.args.is_empty() {
353      write!(
354        f,
355        ", args: {}",
356        self.args.iter().map(|(key, value)| format!("{}->{}", key, value)).collect::<Vec<_>>().join(", ")
357      )?;
358    }
359    write!(f, ", message: {}", self.message)
360  }
361}
362
363impl Display for PathSpec {
364  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
365    write!(f, "{}", self.prefix)
366  }
367}
368
369impl Display for PortMapping {
370  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
371    let mut fields = vec![];
372    if let Some(ref auth) = self.auth {
373      fields.push(format!("auth: {}", auth))
374    }
375    if let Some(ref mode) = self.mode {
376      fields.push(format!("mode: {}", mode))
377    }
378    if let Some(ref service_group) = self.service_group {
379      fields.push(format!("service group: {}", service_group))
380    }
381    if let Some(ref tls) = self.tls {
382      fields.push(format!("port mapping: {}", tls.to_string()))
383    }
384    if let Some(ref vhost) = self.vhost {
385      fields.push(format!("vhost: {}", vhost))
386    }
387    if let Some(ref whitelist) = self.whitelist {
388      fields.push(format!("whitelist: {}", whitelist))
389    }
390    if !self.paths.is_empty() {
391      fields.push(format!("paths: {}", self.paths.iter().map(|p| p.prefix.to_string()).collect::<Vec<_>>().join(", ")))
392    }
393    write!(f, "{}", fields.join(", "))
394  }
395}
396
397impl Display for PublicManagedStream {
398  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
399    write_topic(f, Some("public"), self.partitions, self.replication_factor, &self.kafka_properties)?;
400    if self.contract.can_be_retained {
401      write!(f, ", retained")?;
402    }
403    Ok(())
404  }
405}
406
407impl Display for Secret {
408  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
409    write!(f, "{}", self.name)
410  }
411}
412
413impl Display for Task {
414  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
415    write!(f, "host: {}, state: {}", self.host, self.state.to_string())
416  }
417}
418
419impl Display for TaskStatus {
420  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
421    write!(f, "status: ({})", self.status)?;
422    if let Some(ref task) = self.actual {
423      write!(f, ", actual: ({})", task)?;
424    }
425    if let Some(ref configuration) = self.configuration {
426      write!(f, ", configuration: ({})", configuration)?;
427    }
428    Ok(())
429  }
430}
431
432impl Display for Topic {
433  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
434    write_topic(f, None, self.partitions, self.replication_factor, &self.kafka_properties)
435  }
436}
437
438impl Display for TopicStatus {
439  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
440    write!(f, "status: ({})", self.status)?;
441    if let Some(ref actual) = self.actual {
442      write!(f, ", actual: ({})", actual)?;
443    }
444    if let Some(ref configuration) = self.configuration {
445      write!(f, ", configuration: ({})", configuration)?;
446    }
447    Ok(())
448  }
449}
450
451impl Display for Vhost {
452  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
453    write!(f, "{}", self.value)
454  }
455}
456
457impl Display for Volume {
458  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
459    write!(f, "size: {} GB", self.size_gi_b)
460  }
461}
462
463impl Display for VolumeStatus {
464  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
465    if let Some(ref actual) = self.actual {
466      write!(f, "actual({}), ", actual)?
467    }
468    if let Some(ref configuration) = self.configuration {
469      write!(f, "configuration({}), ", configuration)?
470    }
471    write!(f, "{}", self.status)
472  }
473}
474
475fn write_topic(f: &mut Formatter<'_>, kind: Option<&str>, partitions: i64, replication_factor: i64, kafka_properties: &HashMap<String, String>) -> std::fmt::Result {
476  if let Some(kind) = kind {
477    write!(f, "managed: {}", kind)?;
478  }
479  write!(f, "partitions: {}", partitions)?;
480  write!(f, ", replication factor: {}", replication_factor)?;
481  for (key, value) in kafka_properties {
482    write!(f, ", {}: {}", key, value)?
483  }
484  Ok(())
485}