PrismFoundation defines the base protocols used across every Prism module. They provide consistent patterns for modeling data, handling errors, generating test fixtures, and logging.
The standard protocol for data models. Combines Codable, Equatable, Hashable, CustomStringConvertible, and PrismLogger into a single conformance with automatic JSON serialization and logging.
Defining an Entity
import PrismFoundationstruct User: PrismEntity { let id: UUID let name: String let email: String}let user = User(id: UUID(), name: "Alice", email: "alice@example.com")// Auto-generated JSON descriptionprint(user.description)// {"id":"...","name":"Alice","email":"alice@example.com"}// Log the entity via os.Loggeruser.log()
PrismEntity arrays also conform automatically — [User] is itself a PrismEntity and can call .log() directly.
A typed error protocol that extends Error, LocalizedError, and PrismLogger. Provides structured error descriptions with failure reasons and recovery suggestions.
Defining a Custom Error
import PrismFoundationenum PaymentError: PrismError { case insufficientFunds(required: Decimal, available: Decimal) case cardExpired var errorDescription: String? { switch self { case .insufficientFunds(let required, let available): return "Insufficient funds: need \(required), have \(available)" case .cardExpired: return "Payment card has expired" } } var failureReason: String? { switch self { case .insufficientFunds: return "The account balance is below the required amount." case .cardExpired: return "The card expiration date is in the past." } } var recoverySuggestion: String? { switch self { case .insufficientFunds: return "Add funds to your account and try again." case .cardExpired: return "Update your payment method with a valid card." } }}// Logs error, failure reason, and recovery suggestion automaticallyPaymentError.cardExpired.log()