refactor(core): Centralize timestamp parsing in core::time

Duplicate ISO 8601 timestamp parsing functions existed in both
discussion.rs and merge_request.rs transformers. This extracts
iso_to_ms_strict() and iso_to_ms_opt_strict() into core::time
as the single source of truth, and updates both transformer
modules to use the shared implementations.

Also removes the private now_ms() from merge_request.rs in
favor of the existing core::time::now_ms(), and replaces the
local parse_timestamp_opt() in discussion.rs with the public
iso_to_ms() from core::time.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Taylor Eernisse
2026-01-29 08:41:34 -05:00
parent 55b895a2eb
commit 390f8a9288
3 changed files with 28 additions and 53 deletions

View File

@@ -1,33 +1,8 @@
//! Merge request transformer: converts GitLabMergeRequest to local schema.
use chrono::DateTime;
use std::time::{SystemTime, UNIX_EPOCH};
use crate::core::time::{iso_to_ms_opt_strict, iso_to_ms_strict, now_ms};
use crate::gitlab::types::GitLabMergeRequest;
/// Get current time in milliseconds since Unix epoch.
fn now_ms() -> i64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_millis() as i64
}
/// Parse ISO 8601 timestamp to milliseconds since Unix epoch.
fn iso_to_ms(ts: &str) -> Result<i64, String> {
DateTime::parse_from_rfc3339(ts)
.map(|dt| dt.timestamp_millis())
.map_err(|e| format!("Failed to parse timestamp '{}': {}", ts, e))
}
/// Parse optional ISO 8601 timestamp to optional milliseconds since Unix epoch.
fn iso_to_ms_opt(ts: &Option<String>) -> Result<Option<i64>, String> {
match ts {
Some(s) => iso_to_ms(s).map(Some),
None => Ok(None),
}
}
/// Local schema representation of a merge request row.
#[derive(Debug, Clone)]
pub struct NormalizedMergeRequest {
@@ -77,12 +52,12 @@ pub fn transform_merge_request(
local_project_id: i64,
) -> Result<MergeRequestWithMetadata, String> {
// Parse required timestamps
let created_at = iso_to_ms(&gitlab_mr.created_at)?;
let updated_at = iso_to_ms(&gitlab_mr.updated_at)?;
let created_at = iso_to_ms_strict(&gitlab_mr.created_at)?;
let updated_at = iso_to_ms_strict(&gitlab_mr.updated_at)?;
// Parse optional timestamps
let merged_at = iso_to_ms_opt(&gitlab_mr.merged_at)?;
let closed_at = iso_to_ms_opt(&gitlab_mr.closed_at)?;
let merged_at = iso_to_ms_opt_strict(&gitlab_mr.merged_at)?;
let closed_at = iso_to_ms_opt_strict(&gitlab_mr.closed_at)?;
// Draft: prefer draft, fallback to work_in_progress
let is_draft = gitlab_mr.draft || gitlab_mr.work_in_progress;