Skip to main content

things3_common/
lib.rs

1//! Things Common - Shared utilities and types for Things 3 integration
2//!
3//! This crate provides shared utilities and constants for Things 3 integration.
4//!
5//! # Examples
6//!
7//! ```
8//! use things3_common::{DATABASE_FILENAME, truncate_string, format_date};
9//! use chrono::NaiveDate;
10//!
11//! // Use constants
12//! assert_eq!(DATABASE_FILENAME, "main.sqlite");
13//!
14//! // Use utility functions
15//! let truncated = truncate_string("hello world", 5);
16//! assert_eq!(truncated, "he...");
17//!
18//! let date = NaiveDate::from_ymd_opt(2023, 12, 25).unwrap();
19//! let formatted = format_date(&date);
20//! assert_eq!(formatted, "2023-12-25");
21//! ```
22
23pub mod constants;
24pub mod utils;
25
26pub use constants::*;
27pub use utils::*;
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn test_re_exported_constants() {
35        // Test that constants are properly re-exported from the crate root
36        assert_eq!(DATABASE_FILENAME, "main.sqlite");
37        assert_eq!(DATABASE_DIR, "Things Database.thingsdatabase");
38        assert_eq!(THINGS_CONTAINER, "JLMPQHK8H4.com.culturedcode.Things3");
39        assert_eq!(DEFAULT_QUERY_LIMIT, 100);
40        assert_eq!(MAX_QUERY_LIMIT, 1000);
41        assert_eq!(DEFAULT_MCP_PORT, 3000);
42
43        // Test that arrays are accessible
44        assert_eq!(DATE_FORMATS.len(), 3);
45        assert_eq!(DATETIME_FORMATS.len(), 3);
46
47        // Test that we can access specific array elements
48        assert_eq!(DATE_FORMATS[0], "%Y-%m-%d");
49        assert_eq!(DATETIME_FORMATS[0], "%Y-%m-%d %H:%M:%S");
50    }
51
52    #[test]
53    fn test_re_exported_functions() {
54        // Test that utility functions are properly re-exported from the crate root
55        use chrono::{NaiveDate, Utc};
56
57        // Test format_date
58        let date = NaiveDate::from_ymd_opt(2023, 12, 25).unwrap();
59        let formatted = format_date(&date);
60        assert_eq!(formatted, "2023-12-25");
61
62        // Test format_datetime
63        let dt = Utc::now();
64        let formatted = format_datetime(&dt);
65        assert!(formatted.contains("UTC"));
66
67        // Test parse_date
68        let result = parse_date("2023-12-25");
69        assert!(result.is_ok());
70
71        // Test is_valid_uuid
72        assert!(is_valid_uuid("550e8400-e29b-41d4-a716-446655440000"));
73        assert!(!is_valid_uuid("invalid-uuid"));
74
75        // Test truncate_string
76        assert_eq!(truncate_string("hello world", 5), "he...");
77        assert_eq!(truncate_string("hi", 10), "hi");
78    }
79
80    #[test]
81    fn test_module_accessibility() {
82        // Test that modules are accessible and contain expected items
83
84        // Test constants module
85        assert_eq!(constants::DATABASE_FILENAME, "main.sqlite");
86        assert_eq!(constants::DEFAULT_QUERY_LIMIT, 100);
87
88        // Test utils module
89        let result = utils::truncate_string("hello world", 5);
90        assert_eq!(result, "he...");
91
92        // Test that we can use module-qualified names
93        assert!(utils::is_valid_uuid("550e8400-e29b-41d4-a716-446655440000"));
94        assert_eq!(utils::truncate_string("test", 2), "...");
95    }
96
97    #[test]
98    fn test_crate_level_imports() {
99        // Test that importing from crate root works as expected
100        // This ensures the pub use statements are working correctly
101
102        // Test importing constants directly
103        use crate::{DATABASE_FILENAME, DATE_FORMATS, DEFAULT_QUERY_LIMIT};
104        // Test importing functions directly
105        use crate::{format_date, is_valid_uuid, truncate_string};
106
107        assert_eq!(DATABASE_FILENAME, "main.sqlite");
108        assert_eq!(DEFAULT_QUERY_LIMIT, 100);
109        assert_eq!(DATE_FORMATS.len(), 3);
110
111        assert!(is_valid_uuid("550e8400-e29b-41d4-a716-446655440000"));
112        assert_eq!(truncate_string("hello", 3), "...");
113
114        let date = chrono::NaiveDate::from_ymd_opt(2023, 1, 1).unwrap();
115        let formatted = format_date(&date);
116        assert_eq!(formatted, "2023-01-01");
117    }
118
119    #[test]
120    fn test_wildcard_imports() {
121        // Test that wildcard imports work correctly
122        // This tests the pub use *; statements
123
124        // Create a scope to test wildcard import
125        {
126            use crate::constants::*;
127            assert_eq!(DATABASE_FILENAME, "main.sqlite");
128            assert_eq!(DEFAULT_QUERY_LIMIT, 100);
129        }
130
131        {
132            use crate::utils::*;
133            assert!(is_valid_uuid("550e8400-e29b-41d4-a716-446655440000"));
134            assert_eq!(truncate_string("test", 2), "...");
135
136            let date = chrono::NaiveDate::from_ymd_opt(2023, 6, 15).unwrap();
137            let formatted = format_date(&date);
138            assert_eq!(formatted, "2023-06-15");
139        }
140    }
141}