rename to smartpack n smartunpack

This commit is contained in:
2026-05-18 19:30:58 +07:00
parent cc95bc97d3
commit 396e0848da
21 changed files with 323 additions and 314 deletions

View File

@@ -1,6 +1,6 @@
// msghandler Rust Module
// Cross-platform bi-directional data bridge
// Implements smartsend and smartreceive for message transport
// Implements smartpack and smartunpack for message transport
// with support for both direct payload transport and URL-based transport
// for larger payloads using the Claim-Check pattern.
//
@@ -325,8 +325,8 @@ impl MsgEnvelopeV1 {
// Options Structures
// ============================================================================
/// Options for the `smartsend` function
pub struct SmartsendOptions {
/// Options for the `smartpack` function
pub struct smartpackOptions {
/// Broker URL
pub broker_url: String,
/// HTTP file server URL for large payloads
@@ -355,9 +355,9 @@ pub struct SmartsendOptions {
pub sender_id: String,
}
impl Default for SmartsendOptions {
impl Default for smartpackOptions {
fn default() -> Self {
SmartsendOptions {
smartpackOptions {
broker_url: DEFAULT_BROKER_URL.to_string(),
fileserver_url: DEFAULT_FILESERVER_URL.to_string(),
fileserver_upload_handler: None,
@@ -375,8 +375,8 @@ impl Default for SmartsendOptions {
}
}
/// Options for the `smartreceive` function
pub struct SmartreceiveOptions {
/// Options for the `smartunpack` function
pub struct smartunpackOptions {
/// Custom file server download handler (optional, uses exponential backoff by default)
pub fileserver_download_handler: Option<Arc<dyn FileDownloadHandler>>,
/// Maximum retry attempts for fetching a URL
@@ -387,9 +387,9 @@ pub struct SmartreceiveOptions {
pub max_delay: u64,
}
impl Default for SmartreceiveOptions {
impl Default for smartunpackOptions {
fn default() -> Self {
SmartreceiveOptions {
smartunpackOptions {
fileserver_download_handler: None,
max_retries: DEFAULT_MAX_RETRIES,
base_delay: DEFAULT_BASE_DELAY,
@@ -689,7 +689,7 @@ pub fn log_trace(correlation_id: &str, message: &str) {
}
// ============================================================================
// Public API: smartsend
// Public API: smartpack
// ============================================================================
/// Send data with automatic transport selection.
@@ -715,23 +715,23 @@ pub fn log_trace(correlation_id: &str, message: &str) {
///
/// # Example
/// ```no_run
/// use msghandler::{smartsend, Payload, SmartsendOptions};
/// use msghandler::{smartpack, Payload, smartpackOptions};
///
/// let (envelope, json_str) = smartsend(
/// let (envelope, json_str) = smartpack(
/// "/agent/wine/api/v1/prompt",
/// &[
/// ("msg".to_string(), Payload::Text("Hello!".to_string()), "text".to_string()),
/// ("data".to_string(), Payload::Binary(vec![1, 2, 3]), "binary".to_string()),
/// ],
/// &SmartsendOptions::default(),
/// &smartpackOptions::default(),
/// ).unwrap();
///
/// // Caller publishes via their preferred transport
/// ```
pub fn smartsend(
pub fn smartpack(
subject: &str,
data: &[(String, Payload, String)],
options: &SmartsendOptions,
options: &smartpackOptions,
) -> Result<(MsgEnvelopeV1, String), MsgHandlerError> {
let correlation_id = if options.correlation_id.is_empty() {
Uuid::new_v4().to_string()
@@ -752,7 +752,7 @@ pub fn smartsend(
};
log_trace(&correlation_id, &format!(
"Starting smartsend for subject: {}", subject
"Starting smartpack for subject: {}", subject
));
let mut payloads: Vec<MsgPayloadV1> = Vec::new();
@@ -844,7 +844,7 @@ pub fn smartsend(
// ============================================================================
/// Store deserialized Payload data back into a MsgPayloadV1's data field.
/// After smartreceive(), payload.data contains the deserialized content as a string
/// After smartunpack(), payload.data contains the deserialized content as a string
/// (decoded text, JSON string, or base64 for binary types).
fn store_deserialized_data(payload: &MsgPayloadV1, deserialized: &Payload) -> MsgPayloadV1 {
let mut p = payload.clone();
@@ -861,7 +861,7 @@ fn store_deserialized_data(payload: &MsgPayloadV1, deserialized: &Payload) -> Ms
}
// ============================================================================
// Public API: smartreceive
// Public API: smartunpack
// ============================================================================
/// Receive and process messages.
@@ -880,7 +880,7 @@ fn store_deserialized_data(payload: &MsgPayloadV1, deserialized: &Payload) -> Ms
///
/// # Example
/// ```no_run
/// use msghandler::{smartreceive, SmartreceiveOptions};
/// use msghandler::{smartunpack, smartunpackOptions};
/// use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
///
/// let msg_json_str = r#"{"correlation_id":"abc123","msg_id":"msg-uuid",
@@ -893,7 +893,7 @@ fn store_deserialized_data(payload: &MsgPayloadV1, deserialized: &Payload) -> Ms
/// "data":"SGVsbG8=","metadata":{"payload_bytes":5}
/// }]}"#;
///
/// let envelope = smartreceive(msg_json_str, &SmartreceiveOptions::default()).unwrap();
/// let envelope = smartunpack(msg_json_str, &smartunpackOptions::default()).unwrap();
///
/// for payload in &envelope.payloads {
/// if payload.transport == "direct" {
@@ -904,9 +904,9 @@ fn store_deserialized_data(payload: &MsgPayloadV1, deserialized: &Payload) -> Ms
/// }
/// }
/// ```
pub fn smartreceive(
pub fn smartunpack(
msg_json_str: &str,
options: &SmartreceiveOptions,
options: &smartunpackOptions,
) -> Result<MsgEnvelopeV1, MsgHandlerError> {
// Parse the JSON envelope
let mut env: MsgEnvelopeV1 = serde_json::from_str(msg_json_str)
@@ -998,9 +998,9 @@ pub fn smartreceive(
pub fn send_text(
subject: &str,
text: &str,
options: &SmartsendOptions,
options: &smartpackOptions,
) -> Result<(MsgEnvelopeV1, String), MsgHandlerError> {
smartsend(
smartpack(
subject,
&[(
"text".to_string(),
@@ -1015,9 +1015,9 @@ pub fn send_text(
pub fn send_dictionary(
subject: &str,
data: &JsonValue,
options: &SmartsendOptions,
options: &smartpackOptions,
) -> Result<(MsgEnvelopeV1, String), MsgHandlerError> {
smartsend(
smartpack(
subject,
&[(
"dictionary".to_string(),
@@ -1032,9 +1032,9 @@ pub fn send_dictionary(
pub fn send_binary(
subject: &str,
data: &[u8],
options: &SmartsendOptions,
options: &smartpackOptions,
) -> Result<(MsgEnvelopeV1, String), MsgHandlerError> {
smartsend(
smartpack(
subject,
&[(
"binary".to_string(),
@@ -1094,10 +1094,10 @@ pub fn plik_upload_file(
// All public types are already exported via `pub` on their definitions.
// Key types:
// - `smartsend`, `smartreceive` - main API functions
// - `smartpack`, `smartunpack` - main API functions
// - `Payload` - type-safe payload enum
// - `MsgEnvelopeV1`, `MsgPayloadV1` - wire format structs
// - `SmartsendOptions`, `SmartreceiveOptions` - configuration
// - `smartpackOptions`, `smartunpackOptions` - configuration
// - `FileUploadHandler`, `FileDownloadHandler` - trait abstractions
// - `PlikOneshotUploadHandler`, `BackoffDownloadHandler` - default implementations
// - `MsgHandlerError` - error type
@@ -1193,12 +1193,12 @@ mod tests {
#[test]
fn test_default_options() {
let opts = SmartsendOptions::default();
let opts = smartpackOptions::default();
assert_eq!(opts.size_threshold, DEFAULT_SIZE_THRESHOLD);
assert_eq!(opts.broker_url, DEFAULT_BROKER_URL);
assert_eq!(opts.fileserver_url, DEFAULT_FILESERVER_URL);
let opts = SmartreceiveOptions::default();
let opts = smartunpackOptions::default();
assert_eq!(opts.max_retries, DEFAULT_MAX_RETRIES);
assert_eq!(opts.base_delay, DEFAULT_BASE_DELAY);
assert_eq!(opts.max_delay, DEFAULT_MAX_DELAY);