Capture receipt time only when the source omits its timestamp. Call this before adapter-specific normalization so user/tool data cannot supply it.
(
self,
agent: &str,
hook_type: HookType,
raw: &Value,
)
| 21 | /// Capture receipt time only when the source omits its timestamp. Call this |
| 22 | /// before adapter-specific normalization so user/tool data cannot supply it. |
| 23 | pub(super) fn parse( |
| 24 | self, |
| 25 | agent: &str, |
| 26 | hook_type: HookType, |
| 27 | raw: &Value, |
| 28 | ) -> AgentResult<DateTime<Utc>> { |
| 29 | let Some(value) = raw.get("timestamp").filter(|value| !value.is_null()) else { |
| 30 | return Ok(Utc::now()); |
| 31 | }; |
| 32 | |
| 33 | let timestamp = match self { |
| 34 | Self::Rfc3339 => parse_rfc3339(value), |
| 35 | Self::UnixMillisString => value |
| 36 | .as_str() |
| 37 | .and_then(|value| value.parse::<i64>().ok()) |
| 38 | .and_then(DateTime::from_timestamp_millis), |
| 39 | Self::Rfc3339OrUnixMillis => match value { |
| 40 | Value::String(_) => parse_rfc3339(value), |
| 41 | Value::Number(_) => value.as_i64().and_then(DateTime::from_timestamp_millis), |
| 42 | _ => None, |
| 43 | }, |
| 44 | }; |
| 45 | |
| 46 | timestamp.ok_or_else(|| AgentError::HookParseFailed { |
| 47 | agent: agent.to_string(), |
| 48 | hook_type: hook_type.as_str().to_string(), |
| 49 | reason: match self { |
| 50 | Self::Rfc3339 => "timestamp must be an RFC-3339 string", |
| 51 | Self::UnixMillisString => { |
| 52 | "timestamp must be a decimal epoch-millisecond string within the supported UTC range" |
| 53 | } |
| 54 | Self::Rfc3339OrUnixMillis => { |
| 55 | "timestamp must be an RFC-3339 string or integer epoch milliseconds within the supported UTC range" |
| 56 | } |
| 57 | }.to_string(), |
| 58 | }) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | fn parse_rfc3339(value: &Value) -> Option<DateTime<Utc>> { |
no test coverage detected