openleadr_client/report.rs
1use std::sync::Arc;
2
3use openleadr_wire::{report::ReportContent, Report};
4
5use crate::{error::Result, ClientRef};
6
7/// Client to manage the data of a specific report
8///
9/// Can be created by a [`EventClient`](crate::EventClient)
10/// ```no_run
11/// # use openleadr_client::{Client, Filter};
12/// # use openleadr_wire::event::Priority;
13/// let client = Client::with_url("https://your-vtn.com".try_into().unwrap(), None);
14/// # tokio_test::block_on(async {
15/// let event = client.get_event_by_id(&"event-1".parse().unwrap()).await.unwrap();
16///
17/// // retrieve all reports in that specific event, optionally filtered by the client name
18/// let mut reports = event.get_report_list(Some("client-name")).await.unwrap();
19/// let mut report = reports.remove(0);
20///
21/// // change report name
22/// report.content_mut().report_name = Some("new-report-name".to_string());
23/// report.update().await.unwrap()
24/// # })
25/// ```
26#[derive(Debug, Clone)]
27pub struct ReportClient {
28 client: Arc<ClientRef>,
29 data: Report,
30}
31
32impl ReportClient {
33 pub(super) fn from_report(client: Arc<ClientRef>, report: Report) -> Self {
34 Self {
35 client,
36 data: report,
37 }
38 }
39
40 /// Get the id of the report
41 pub fn id(&self) -> &openleadr_wire::report::ReportId {
42 &self.data.id
43 }
44
45 /// Get the time the report was created on the VTN
46 pub fn created_date_time(&self) -> &chrono::DateTime<chrono::Utc> {
47 &self.data.created_date_time
48 }
49
50 /// Get the time the report was last modified on the VTN
51 pub fn modification_date_time(&self) -> &chrono::DateTime<chrono::Utc> {
52 &self.data.modification_date_time
53 }
54
55 /// Read the data of the report
56 pub fn content(&self) -> &ReportContent {
57 &self.data.content
58 }
59
60 /// Modify the data of the report.
61 /// Make sure to call [`update`](Self::update)
62 /// after your modifications to store them on the VTN
63 pub fn content_mut(&mut self) -> &mut ReportContent {
64 &mut self.data.content
65 }
66
67 /// Stores any modifications made to the report content at the server
68 /// and refreshes the locally stored data with the returned VTN data
69 pub async fn update(&mut self) -> Result<()> {
70 let res = self
71 .client
72 .put(&format!("reports/{}", self.id()), &self.data.content)
73 .await?;
74 self.data = res;
75 Ok(())
76 }
77
78 /// Delete the report from the VTN
79 pub async fn delete(self) -> Result<Report> {
80 self.client.delete(&format!("reports/{}", self.id())).await
81 }
82}