|
1 | | -### CornucopiaCore |
| 1 | +# CornucopiaCore |
2 | 2 |
|
3 | | -_:shell: The "horn of plenty" – a symbol of abundance._ |
| 3 | +🌽 **The "horn of plenty"** – a symbol of abundance. |
4 | 4 |
|
5 | | -[](https://swift.org) |
| 5 | +[](https://swift.org) |
6 | 6 | [](https://github.com/Cornucopia-Swift/CornucopiaCore/actions?query=workflow%3ASwift) |
7 | 7 |
|
8 | | -### Introduction |
| 8 | +## Overview |
9 | 9 |
|
10 | | -This is a library with _essentials_ for Swift programmers. Although primarily motivated by my work on apps for the Apple platforms, it should run on all platforms where the Swift Package Manager is available. |
| 10 | +CornucopiaCore is a comprehensive Swift Package Manager library that provides essential utilities for Swift developers across all Apple platforms (iOS 16+, macOS 13+, tvOS 16+, watchOS 9+). It's designed as a "horn of plenty" – offering a rich collection of extensions, utilities, and helper classes to augment Foundation and platform-specific frameworks. |
11 | 11 |
|
12 | | -### History & Status |
| 12 | +## Features |
13 | 13 |
|
14 | | -This is pretty much an early work-in-progress. With a decade of Objective-C experience and a solid bunch of such frameworks, I have only recently started developing with Swift. |
15 | | -Rather than blindly rewriting everything, I'm going to add stuff on-demand when I need it in my projects. |
| 14 | +### 🛠 Extensions |
| 15 | +Powerful extensions to Foundation and platform types: |
| 16 | +- **String**: Hex encoding/decoding, validation, crypto utilities, IPv4 handling |
| 17 | +- **Data**: Array conversion, number encoding/decoding, string utilities |
| 18 | +- **Array & Collection**: Dictionary conversion, hex operations, chunked sequences |
| 19 | +- **Date & Time**: Pretty printing, past/present/future checks, ISO8601 formatting |
| 20 | +- **FileManager**: Compression, path utilities, extended attributes |
| 21 | +- **Networking**: HTTP utilities, URL request enhancements, status codes |
| 22 | +- **And many more**: All extensions use the `CC_` prefix to avoid naming conflicts |
16 | 23 |
|
17 | | -### Contributions |
| 24 | +### 📊 Data Structures |
| 25 | +Thread-safe and efficient data structures: |
| 26 | +- **ChunkedSequence & ReverseChunkedSequence**: Process collections in chunks |
| 27 | +- **ThreadSafeDictionary**: Concurrent access to dictionaries |
| 28 | +- **CyclicSequenceNumber**: Rolling sequence numbers |
18 | 29 |
|
19 | | -Feel free to use under the terms of the MIT, if you find anything helpful here. Contributions are always welcome! Stay safe and sound! |
| 30 | +### 🏷 Property Wrappers |
| 31 | +Convenient property wrappers for common patterns: |
| 32 | +- **@Clamped**: Automatically clamp values to specified ranges |
| 33 | +- **@HexEncoded**: Automatic hex string encoding/decoding for Data |
| 34 | +- **@Protected**: Thread-safe property access with locking |
| 35 | +- **@Default**: Codable properties with fallback values |
| 36 | + |
| 37 | +### 📱 Device & System |
| 38 | +Cross-platform device information and system utilities: |
| 39 | +- **DeviceInfo**: Hardware model, OS version, user agent strings |
| 40 | +- **Device**: Persistent UUID generation with Keychain storage |
| 41 | +- **Environment**: Easy access to environment variables |
| 42 | +- **SysLog**: System logging integration |
| 43 | + |
| 44 | +### 🔐 Security & Storage |
| 45 | +Secure storage and cryptographic utilities: |
| 46 | +- **Keychain**: Simple, secure storage interface |
| 47 | +- **JWT**: JSON Web Token support with cryptographic validation |
| 48 | +- **PKCS12**: Certificate and private key handling |
| 49 | +- **Extended File Attributes**: Metadata storage on files |
| 50 | + |
| 51 | +### 📋 Logging System |
| 52 | +Flexible, configurable logging with multiple output targets: |
| 53 | +- **Multi-sink support**: Print, file, syslog (UDP/TCP), OSLog |
| 54 | +- **Environment configuration**: Control via `LOGLEVEL` and `LOGSINK` variables |
| 55 | +- **Thread-safe**: Background dispatch queue processing |
| 56 | +- **Level filtering**: Trace, debug, info, notice, error, fault |
| 57 | + |
| 58 | +### ⚡ Concurrency & Performance |
| 59 | +Modern async/await utilities and performance tools: |
| 60 | +- **AsyncWithTimeout**: Timeout support for async operations |
| 61 | +- **Benchmarking**: Performance measurement utilities |
| 62 | +- **Task**: Sleep utilities for async contexts |
| 63 | + |
| 64 | +### 🌐 Networking |
| 65 | +HTTP utilities and networking helpers: |
| 66 | +- **HTTPConstants**: Standard status codes and headers |
| 67 | +- **URL extensions**: String interpolation, path utilities |
| 68 | +- **URLRequest**: Authorization headers, range requests |
| 69 | + |
| 70 | +## Installation |
| 71 | + |
| 72 | +### Swift Package Manager |
| 73 | + |
| 74 | +Add CornucopiaCore to your `Package.swift`: |
| 75 | + |
| 76 | +```swift |
| 77 | +dependencies: [ |
| 78 | + .package(url: "https://github.com/Cornucopia-Swift/CornucopiaCore.git", from: "1.0.0") |
| 79 | +] |
| 80 | +``` |
| 81 | + |
| 82 | +Or add it through Xcode: |
| 83 | +1. File → Add Package Dependencies |
| 84 | +2. Enter: `https://github.com/Cornucopia-Swift/CornucopiaCore.git` |
| 85 | + |
| 86 | +## Usage Examples |
| 87 | + |
| 88 | +### Basic Logging |
| 89 | + |
| 90 | +```swift |
| 91 | +import CornucopiaCore |
| 92 | + |
| 93 | +let logger = Cornucopia.Core.Logger() |
| 94 | + |
| 95 | +logger.info("Application started") |
| 96 | +logger.debug("Debug information") |
| 97 | +logger.error("Something went wrong") |
| 98 | + |
| 99 | +// Configure via environment variables |
| 100 | +// LOGLEVEL=DEBUG LOGSINK=print:// ./your-app |
| 101 | +``` |
| 102 | + |
| 103 | +### Property Wrappers |
| 104 | + |
| 105 | +```swift |
| 106 | +import CornucopiaCore |
| 107 | + |
| 108 | +struct GameScore { |
| 109 | + @Cornucopia.Core.Clamped(to: 0...100) |
| 110 | + var health: Int = 100 |
| 111 | + |
| 112 | + @Cornucopia.Core.HexEncoded |
| 113 | + var sessionId: Data = Data() |
| 114 | + |
| 115 | + @Cornucopia.Core.Protected |
| 116 | + var score: Int = 0 |
| 117 | +} |
| 118 | + |
| 119 | +var game = GameScore() |
| 120 | +game.health = 150 // Automatically clamped to 100 |
| 121 | +game.sessionId = "deadbeef".CC_hexDecodedData |
| 122 | +``` |
| 123 | + |
| 124 | +### Device Information |
| 125 | + |
| 126 | +```swift |
| 127 | +import CornucopiaCore |
| 128 | + |
| 129 | +let device = Cornucopia.Core.Device.current |
| 130 | +print("Device: \(device.info.model)") |
| 131 | +print("OS: \(device.info.operatingSystem) \(device.info.operatingSystemVersion)") |
| 132 | +print("User Agent: \(device.info.userAgent)") |
| 133 | +print("Persistent UUID: \(device.uuid)") |
| 134 | +``` |
| 135 | + |
| 136 | +### String Extensions |
| 137 | + |
| 138 | +```swift |
| 139 | +import CornucopiaCore |
| 140 | + |
| 141 | +// Hex operations |
| 142 | +let data = "deadbeef".CC_hexDecodedData |
| 143 | +let hexString = data.CC_hexEncodedString() |
| 144 | + |
| 145 | +// Content validation |
| 146 | +let isValidEmail = "[email protected]". CC_isValidEmail |
| 147 | +let isValidIPv4 = "192.168.1.1".CC_isValidIPv4Address |
| 148 | + |
| 149 | +// Crypto |
| 150 | +let sha256 = "Hello World".CC_sha256 |
| 151 | +``` |
| 152 | + |
| 153 | +### Secure Storage |
| 154 | + |
| 155 | +```swift |
| 156 | +import CornucopiaCore |
| 157 | + |
| 158 | +let keychain = Cornucopia.Core.Keychain.standard |
| 159 | + |
| 160 | +// Store sensitive data |
| 161 | +let apiKey = "secret-api-key".data(using: .utf8)! |
| 162 | +keychain.save(data: apiKey, for: "api-key") |
| 163 | + |
| 164 | +// Retrieve data |
| 165 | +if let storedKey = keychain.load(key: "api-key") { |
| 166 | + let keyString = String(data: storedKey, encoding: .utf8) |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +### Data Structures |
| 171 | + |
| 172 | +```swift |
| 173 | +import CornucopiaCore |
| 174 | + |
| 175 | +// Thread-safe dictionary |
| 176 | +let safeDict = Cornucopia.Core.ThreadSafeDictionary<String, Int>() |
| 177 | +safeDict["key"] = 42 |
| 178 | + |
| 179 | +// Process collections in chunks |
| 180 | +let numbers = Array(1...1000) |
| 181 | +for chunk in numbers.CC_chunked(by: 10) { |
| 182 | + // Process 10 numbers at a time |
| 183 | + print(chunk) |
| 184 | +} |
| 185 | +``` |
| 186 | + |
| 187 | +### Concurrency |
| 188 | + |
| 189 | +```swift |
| 190 | +import CornucopiaCore |
| 191 | + |
| 192 | +// Timeout for async operations |
| 193 | +let result = try await Cornucopia.Core.asyncWithTimeout(seconds: 5) { |
| 194 | + return await someAsyncOperation() |
| 195 | +} |
| 196 | + |
| 197 | +// Benchmarking |
| 198 | +let duration = Cornucopia.Core.benchmark { |
| 199 | + // Code to measure |
| 200 | + expensiveOperation() |
| 201 | +} |
| 202 | +print("Operation took: \(duration) seconds") |
| 203 | +``` |
| 204 | + |
| 205 | +## Platform Support |
| 206 | + |
| 207 | +- **iOS**: 16.0+ |
| 208 | +- **macOS**: 13.0+ |
| 209 | +- **tvOS**: 16.0+ |
| 210 | +- **watchOS**: 9.0+ |
| 211 | +- **macCatalyst**: 13.0+ |
| 212 | + |
| 213 | +## Dependencies |
| 214 | + |
| 215 | +CornucopiaCore has minimal external dependencies: |
| 216 | +- [swift-crypto](https://github.com/apple/swift-crypto): Apple's cryptographic library |
| 217 | +- [SWCompression](https://github.com/tsolomko/SWCompression): Compression utilities |
| 218 | +- [AnyCodable](https://github.com/mickeyl/AnyCodable): Type-erased Codable support |
| 219 | + |
| 220 | +## Architecture |
| 221 | + |
| 222 | +All public APIs are organized under the `Cornucopia.Core` namespace to avoid naming conflicts. The library is structured into logical modules: |
| 223 | + |
| 224 | +- **Extensions/**: Foundation and platform type extensions |
| 225 | +- **Features/**: Standalone functionality (DeviceInfo, JWT, etc.) |
| 226 | +- **Logging/**: Flexible logging system |
| 227 | +- **PropertyWrappers/**: Utility property wrappers |
| 228 | +- **Storage/**: Data persistence abstractions |
| 229 | +- **Networking/**: HTTP utilities and helpers |
| 230 | + |
| 231 | +## Contributing |
| 232 | + |
| 233 | +Contributions are welcome! This library grows on-demand as real-world projects require new functionality. Feel free to: |
| 234 | + |
| 235 | +1. Report issues or request features |
| 236 | +2. Submit pull requests with new utilities |
| 237 | +3. Improve documentation and examples |
| 238 | + |
| 239 | +## License |
| 240 | + |
| 241 | +CornucopiaCore is available under the MIT license. See LICENSE for details. |
| 242 | + |
| 243 | +--- |
| 244 | + |
| 245 | +**Cornucopia** – *Dr. Lauer Information Technology* |
0 commit comments