Skip to content

Commit bcb6ead

Browse files
committed
add helper to create metadata with duration
1 parent c59a3d2 commit bcb6ead

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

dsc_lib/src/configure/config_doc.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use chrono::{DateTime, Local};
45
use rust_i18n::t;
56
use schemars::JsonSchema;
67
use serde::{Deserialize, Serialize};
@@ -58,6 +59,26 @@ pub struct MicrosoftDscMetadata {
5859
pub security_context: Option<SecurityContextKind>,
5960
}
6061

62+
impl MicrosoftDscMetadata {
63+
/// Creates a new instance of `MicrosoftDscMetadata` with the duration
64+
///
65+
/// # Arguments
66+
///
67+
/// * `start` - The start time of the configuration operation
68+
/// * `end` - The end time of the configuration operation
69+
///
70+
/// # Returns
71+
///
72+
/// A new instance of `MicrosoftDscMetadata` with the duration calculated from the start and end times.
73+
#[must_use]
74+
pub fn new_with_duration(start: &DateTime<Local>, end: &DateTime<Local>) -> Self {
75+
Self {
76+
duration: Some(end.signed_duration_since(*start).to_string()),
77+
..Default::default()
78+
}
79+
}
80+
}
81+
6182
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
6283
pub struct Metadata {
6384
#[serde(rename = "Microsoft.DSC", skip_serializing_if = "Option::is_none")]

dsc_lib/src/configure/mod.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,7 @@ impl Configurator {
327327
let end_datetime = chrono::Local::now();
328328
let mut metadata = Metadata {
329329
microsoft: Some(
330-
MicrosoftDscMetadata {
331-
duration: Some(end_datetime.signed_duration_since(start_datetime).to_string()),
332-
..Default::default()
333-
}
330+
MicrosoftDscMetadata::new_with_duration(&start_datetime, &end_datetime)
334331
),
335332
other: Map::new(),
336333
};
@@ -484,10 +481,7 @@ impl Configurator {
484481

485482
let mut metadata = Metadata {
486483
microsoft: Some(
487-
MicrosoftDscMetadata {
488-
duration: Some(end_datetime.signed_duration_since(start_datetime).to_string()),
489-
..Default::default()
490-
}
484+
MicrosoftDscMetadata::new_with_duration(&start_datetime, &end_datetime)
491485
),
492486
other: Map::new(),
493487
};
@@ -557,10 +551,7 @@ impl Configurator {
557551
let end_datetime = chrono::Local::now();
558552
let mut metadata = Metadata {
559553
microsoft: Some(
560-
MicrosoftDscMetadata {
561-
duration: Some(end_datetime.signed_duration_since(start_datetime).to_string()),
562-
..Default::default()
563-
}
554+
MicrosoftDscMetadata::new_with_duration(&start_datetime, &end_datetime)
564555
),
565556
other: Map::new(),
566557
};

0 commit comments

Comments
 (0)