1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
 * This file is part of CycloneDX Rust Cargo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

use std::convert::TryFrom;

use thiserror::Error;
use time::{format_description::well_known::Iso8601, OffsetDateTime};

use crate::validation::{
    FailureReason, Validate, ValidationContext, ValidationError, ValidationResult,
};

/// For the purposes of CycloneDX SBOM documents, `DateTime` is a ISO8601 formatted timestamp
///
/// The corresponding CycloneDX XML schema definition is the [`xs` namespace](https://cyclonedx.org/docs/1.3/xml/#ns_xs), which defines the [`dateTime`](https://www.w3.org/TR/xmlschema11-2/#dateTime)) format.
///
/// A valid timestamp can be created from a [`String`](std::string::String) using the [`TryFrom`](std::convert::TryFrom) / [`TryInto`](std::convert::TryInto) traits.
///
/// ```
/// use cyclonedx_bom::external_models::date_time::DateTime;
/// use std::convert::TryInto;
///
/// let timestamp = String::from("1970-01-01T00:00:00Z");
/// let date_time: DateTime = timestamp.clone().try_into().expect("Failed to parse as DateTime");
///
/// assert_eq!(date_time.to_string(), timestamp);
/// ```
#[derive(Debug, PartialEq, Eq)]
pub struct DateTime(pub(crate) String);

impl DateTime {
    pub fn now() -> Result<Self, DateTimeError> {
        let now = OffsetDateTime::now_utc()
            .format(&Iso8601::DEFAULT)
            .map_err(|_| DateTimeError::FailedCurrentTime)?;
        Ok(Self(now))
    }
}

impl TryFrom<String> for DateTime {
    type Error = DateTimeError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        match OffsetDateTime::parse(&value, &Iso8601::DEFAULT) {
            Ok(_) => Ok(Self(value)),
            Err(e) => Err(DateTimeError::InvalidDateTime(format!(
                "DateTime does not conform to ISO 8601: {}",
                e
            ))),
        }
    }
}

impl Validate for DateTime {
    fn validate_with_context(
        &self,
        context: ValidationContext,
    ) -> Result<ValidationResult, ValidationError> {
        match OffsetDateTime::parse(&self.0.to_string(), &Iso8601::DEFAULT) {
            Ok(_) => Ok(ValidationResult::Passed),
            Err(_) => Ok(ValidationResult::Failed {
                reasons: vec![FailureReason {
                    message: "DateTime does not conform to ISO 8601".to_string(),
                    context,
                }],
            }),
        }
    }
}

impl ToString for DateTime {
    fn to_string(&self) -> String {
        self.0.clone()
    }
}

#[derive(Debug, Error, PartialEq, Eq)]
pub enum DateTimeError {
    #[error("Invalid DateTime: {}", .0)]
    InvalidDateTime(String),

    #[error("Failed to get current time")]
    FailedCurrentTime,
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::validation::FailureReason;
    use pretty_assertions::assert_eq;

    #[test]
    fn valid_datetimes_should_pass_validation() {
        let validation_result = DateTime("1969-06-28T01:20:00.00-04:00".to_string())
            .validate_with_context(ValidationContext::default())
            .expect("Error while validating");

        assert_eq!(validation_result, ValidationResult::Passed)
    }

    #[test]
    fn invalid_datetimes_should_fail_validation() {
        let validation_result = DateTime("invalid date".to_string())
            .validate_with_context(ValidationContext::default())
            .expect("Error while validating");

        assert_eq!(
            validation_result,
            ValidationResult::Failed {
                reasons: vec![FailureReason {
                    message: "DateTime does not conform to ISO 8601".to_string(),
                    context: ValidationContext::default()
                }]
            }
        )
    }
}