tap_msg/utils.rs
1//! Utility functions for TAP core
2//!
3//! This module provides utility functions used throughout the TAP core library.
4
5use crate::error::Error;
6use crate::message::types::Attachment as TapAttachment;
7use crate::Result;
8use didcomm::{
9 Attachment as DidcommAttachment, AttachmentData as DidcommAttachmentData, Base64AttachmentData,
10 JsonAttachmentData,
11};
12use std::time::SystemTime;
13
14/// Gets the current time as a unix timestamp (seconds since the epoch)
15///
16/// # Returns
17///
18/// * `Result<u64>` - The current time as a unix timestamp, or an error if
19/// the system time cannot be determined.
20pub fn get_current_time() -> Result<u64> {
21 SystemTime::now()
22 .duration_since(SystemTime::UNIX_EPOCH)
23 .map_err(|e| Error::ParseError(format!("Failed to get current time: {}", e)))
24 .map(|duration| duration.as_secs())
25}
26
27/// Converts a TAP attachment into a DIDComm attachment.
28///
29/// # Arguments
30///
31/// * `tap_attachment` - A reference to the TAP Attachment.
32///
33/// # Returns
34///
35/// * `Option<DidcommAttachment>` - The converted DIDComm attachment, or None if input data is missing.
36pub fn convert_tap_attachment_to_didcomm(
37 tap_attachment: &TapAttachment,
38) -> Option<DidcommAttachment> {
39 // Convert TAP AttachmentData (struct with optional fields)
40 // to DIDComm AttachmentData (enum Base64 | Json | Links | ...)
41 let didcomm_data_result = tap_attachment.data.as_ref().and_then(|data| {
42 if let Some(b64) = &data.base64 {
43 // Assuming didcomm::AttachmentData::Base64 exists and takes (value, Option<hash>)
44 // We don't have hash info from TAP attachment, so pass None.
45 // Need to confirm the exact signature of didcomm::AttachmentData::Base64
46 // For now, let's assume it takes { value: String }
47 Some(DidcommAttachmentData::Base64 {
48 value: Base64AttachmentData {
49 base64: b64.clone(),
50 jws: None,
51 },
52 })
53 } else {
54 // Refactor using Option::map as suggested by clippy
55 data.json
56 .as_ref()
57 .map(|json_val| DidcommAttachmentData::Json {
58 value: JsonAttachmentData {
59 json: json_val.clone(),
60 jws: None,
61 },
62 })
63 }
64 });
65
66 // Only create attachment if data is present (as per DIDComm spec)
67 didcomm_data_result.map(|data| DidcommAttachment {
68 id: Some(tap_attachment.id.clone()),
69 media_type: Some(tap_attachment.media_type.clone()),
70 data,
71 description: None, // TAP attachment doesn't have description
72 filename: None, // TAP attachment doesn't have filename
73 format: None, // TAP attachment doesn't have format
74 lastmod_time: None, // TAP attachment doesn't have lastmod_time
75 byte_count: None, // TAP attachment doesn't have byte_count
76 })
77}