Skip to main content

polyoxide_core/
lib.rs

1//! # polyoxide-core
2//!
3//! Core utilities and shared types for Polyoxide Polymarket API clients.
4//!
5//! This crate provides common functionality used across `polyoxide-clob`, `polyoxide-gamma`, and `polyoxide-data`:
6//! - Shared error types and error handling
7//! - HTTP client configuration
8//! - Request builder utilities
9//!
10//! ## HTTP Client
11//!
12//! Use [`HttpClientBuilder`] to create configured HTTP clients:
13//!
14//! ```
15//! use polyoxide_core::HttpClientBuilder;
16//!
17//! let client = HttpClientBuilder::new("https://api.example.com")
18//!     .timeout_ms(60_000)
19//!     .build()
20//!     .unwrap();
21//! ```
22//!
23//! ## Error Handling
24//!
25//! Use the [`impl_api_error_conversions`] macro to reduce boilerplate in error types.
26
27#[macro_use]
28pub mod macros;
29
30pub mod auth;
31pub mod client;
32pub mod error;
33pub mod rate_limit;
34pub mod request;
35
36/// Maximum number of characters to include in log messages containing response bodies.
37const LOG_BODY_MAX_LEN: usize = 512;
38
39/// Truncate a string for safe inclusion in log output.
40///
41/// Returns the original string if it fits within `LOG_BODY_MAX_LEN`,
42/// otherwise truncates at a UTF-8 boundary and appends `... [truncated]`.
43pub fn truncate_for_log(s: &str) -> std::borrow::Cow<'_, str> {
44    if s.len() <= LOG_BODY_MAX_LEN {
45        std::borrow::Cow::Borrowed(s)
46    } else {
47        let truncated = &s[..s.floor_char_boundary(LOG_BODY_MAX_LEN)];
48        std::borrow::Cow::Owned(format!("{}... [truncated]", truncated))
49    }
50}
51
52pub use auth::{current_timestamp, Base64Format, Signer};
53pub use client::{
54    retry_after_header, HttpClient, HttpClientBuilder, DEFAULT_POOL_SIZE, DEFAULT_TIMEOUT_MS,
55};
56pub use error::ApiError;
57pub use rate_limit::{RateLimiter, RetryConfig};
58pub use request::{QueryBuilder, Request, RequestError};
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn test_truncate_for_log_short_string_unchanged() {
66        let short = "hello world";
67        let result = truncate_for_log(short);
68        assert_eq!(result.as_ref(), short);
69    }
70
71    #[test]
72    fn test_truncate_for_log_exact_limit_unchanged() {
73        let exact = "a".repeat(LOG_BODY_MAX_LEN);
74        let result = truncate_for_log(&exact);
75        assert_eq!(result.as_ref(), exact.as_str());
76    }
77
78    #[test]
79    fn test_truncate_for_log_over_limit_truncated() {
80        let long = "x".repeat(LOG_BODY_MAX_LEN + 100);
81        let result = truncate_for_log(&long);
82        assert!(result.ends_with("... [truncated]"));
83        assert!(result.len() < long.len());
84    }
85
86    #[test]
87    fn test_truncate_for_log_multibyte_char_boundary() {
88        // Create a string where the 512th byte falls inside a multi-byte char
89        let mut s = "a".repeat(LOG_BODY_MAX_LEN - 1);
90        s.push('\u{1F600}'); // 4-byte emoji at position 511-514
91        s.push_str("overflow");
92        let result = truncate_for_log(&s);
93        assert!(result.ends_with("... [truncated]"));
94        // Should not panic or produce invalid UTF-8
95        assert!(result.is_char_boundary(0));
96    }
97}