### Build and Test TOONDecoder Package (Bash) Source: https://github.com/alexey1312/toondecoder/blob/main/CLAUDE.md These bash commands are used to build, test, and specifically filter tests for the TOONDecoder Swift package. Ensure you have Swift installed and are in the project's root directory. ```bash swift build swift test swift test --filter TOONDecoderTests/testName ``` -------------------------------- ### Swift Package Manager Installation for TOONDecoder Source: https://github.com/alexey1312/toondecoder/blob/main/README.md Provides the Swift Package Manager (SPM) dependency declaration required to integrate TOONDecoder into a Swift project. Add this to your `Package.swift` file. ```swift dependencies: [ .package(url: "https://github.com/alexey1312/TOONDecoder.git", from: "0.1.0") ] ``` -------------------------------- ### Getting TOONDecoder Specification Version in Swift Source: https://github.com/alexey1312/toondecoder/blob/main/README.md Demonstrates how to retrieve the supported TOON specification version from the TOONDecoder library. This is useful for checking compatibility with different TOON versions. ```swift import TOONDecoder print(TOONDecoder.specVersion) // "3.0" ``` -------------------------------- ### Basic TOON Decoding in Swift Source: https://github.com/alexey1312/toondecoder/blob/main/README.md Demonstrates how to decode a simple TOON string into a Swift Codable struct using TOONDecoder. This example shows the fundamental usage of the decoder for basic data types. ```swift import TOONDecoder struct User: Codable { let id: Int let name: String let tags: [String] let active: Bool } let toon = """ id: 123 name: Ada tags[2]: reading,gaming active: true """ let decoder = TOONDecoder() let user = try decoder.decode(User.self, from: Data(toon.utf8)) print(user.name) // "Ada" ``` -------------------------------- ### Configure Custom Indentation Size with TOONDecoder Source: https://context7.com/alexey1312/toondecoder/llms.txt Allows customization of the indentation size used for parsing TOON files. This is useful for files that do not adhere to the default 2-space indentation, setting it to 4 spaces in this example. ```swift import Foundation import TOONDecoder struct Settings: Codable { struct Theme: Codable { let color: String } let theme: Theme } let decoder = TOONDecoder() decoder.indent = 4 // 4 spaces per level instead of default 2 let toon = """ theme: color: blue """ let data = toon.data(using: .utf8)! let settings = try decoder.decode(Settings.self, from: data) print(settings.theme.color) // "blue" ``` -------------------------------- ### Implement Comprehensive Error Handling with TOONDecoder Source: https://context7.com/alexey1312/toondecoder/llms.txt Shows how to handle various decoding errors provided by TOONDecoder, such as count mismatches, type mismatches, missing keys, and invalid indentation. Each error case provides diagnostic information. ```swift import Foundation import TOONDecoder struct StrictData: Codable { let count: Int let items: [String] } let decoder = TOONDecoder() let toon = "items[5]: a,b,c" // Declares 5 but provides 3 let data = toon.data(using: .utf8)! do { let result = try decoder.decode(StrictData.self, from: data) print(result) } catch let error as TOONDecodingError { switch error { case .countMismatch(let expected, let actual, let line): print("Line \(line): Expected \(expected) items but got \(actual)") case .typeMismatch(let expected, let actual): print("Type error: expected \(expected) but got \(actual)") case .keyNotFound(let key): print("Missing required key: \(key)") case .invalidIndentation(let line, let message): print("Line \(line): \(message)") default: print("Decoding error: \(error)") } } // Output: Line 1: Expected 5 items but got 3 ``` -------------------------------- ### Enable TOON Path Expansion for Dotted Keys Source: https://context7.com/alexey1312/toondecoder/llms.txt Demonstrates how to enable and use the path expansion feature of TOONDecoder, which automatically converts dotted keys (e.g., `a.b.c`) into nested object structures. This requires setting `expandPaths` to `.safe`. ```swift import Foundation import TOONDecoder struct Config: Codable { struct Database: Codable { struct Connection: Codable { let host: String } let connection: Connection } let database: Database } let decoder = TOONDecoder() decoder.expandPaths = .safe let toon = "database.connection.host: localhost" let data = toon.data(using: .utf8)! let config = try decoder.decode(Config.self, from: data) print(config.database.connection.host) // "localhost" ``` -------------------------------- ### Decode Simple TOON Objects to Swift Codable Types Source: https://context7.com/alexey1312/toondecoder/llms.txt Demonstrates basic decoding of TOON data into a Swift struct that conforms to the Codable protocol. It initializes the TOONDecoder and uses the `decode` method with a target Swift type. ```swift import Foundation import TOONDecoder struct User: Codable { let id: Int let name: String let active: Bool } let decoder = TOONDecoder() let toon = """ id: 123 name: Ada active: true """ let data = toon.data(using: .utf8)! let user = try decoder.decode(User.self, from: data) print(user.id) // 123 print(user.name) // "Ada" print(user.active) // true ``` -------------------------------- ### Handle Special Data Types (Date, URL, Data) with TOONDecoder Source: https://context7.com/alexey1312/toondecoder/llms.txt Demonstrates automatic conversion of Date, URL, and Data types when decoding TOON formatted strings. The decoder handles ISO8601 Date strings, URL strings, and Base64 encoded Data. ```swift import Foundation import TOONDecoder struct Resource: Codable { let createdAt: Date let homepage: URL let signature: Data } let decoder = TOONDecoder() let toon = """ createdAt: "2024-01-15T10:30:00.000Z" homepage: "https://example.com" signature: SGVsbG8gV29ybGQ= """ let data = toon.data(using: .utf8)! let resource = try decoder.decode(Resource.self, from: data) let formatter = ISO8601DateFormatter() print(formatter.string(from: resource.createdAt)) // "2024-01-15T10:30:00.000Z" print(resource.homepage.absoluteString) // "https://example.com" print(String(data: resource.signature, encoding: .utf8)!) // "Hello World" ``` -------------------------------- ### TOON Path Expansion for Nested Objects in Swift Source: https://github.com/alexey1312/toondecoder/blob/main/README.md Shows how to use TOONDecoder's path expansion feature to decode dotted keys into nested Swift Codable objects. This requires setting the `expandPaths` property on the decoder. ```swift import TOONDecoder struct Config: Codable { struct Database: Codable { struct Connection: Codable { let host: String let port: Int } let connection: Connection } let database: Database } let toon = """ database.connection.host: localhost database.connection.port: 5432 """ let decoder = TOONDecoder() decoder.expandPaths = .safe let config = try decoder.decode(Config.self, from: Data(toon.utf8)) print(config.database.connection.host) // "localhost" ``` -------------------------------- ### Decode TOON List Format to Swift Arrays of Objects Source: https://context7.com/alexey1312/toondecoder/llms.txt Explains how to parse TOON's list format, indicated by `- ` prefixes, into Swift arrays of Codable objects. This format is suitable for lists of complex items. ```swift import Foundation import TOONDecoder struct Task: Codable { let id: Int let title: String let completed: Bool } let decoder = TOONDecoder() let toon = """ [2]: - id: 1 title: Review code completed: true - id: 2 title: Write tests completed: false """ let data = toon.data(using: .utf8)! let tasks = try decoder.decode([Task].self, from: data) for task in tasks { let status = task.completed ? "✓" : "○" print("\(status) \(task.title)") } // ✓ Review code // ○ Write tests ``` -------------------------------- ### Decode TOON with Custom Array Delimiters (Tab, Pipe) Source: https://context7.com/alexey1312/toondecoder/llms.txt Shows how to use alternative delimiters like tabs (`\t`) or pipes (`|`) for parsing array values in TOON format. This provides flexibility in data representation. ```swift import Foundation import TOONDecoder struct Data: Codable { let values: [String] } let decoder = TOONDecoder() // Tab delimiter let toonTab = "values[3\t]: alpha\tbeta\tgamma" let dataTab = toonTab.data(using: .utf8)! let resultTab = try decoder.decode(Data.self, from: dataTab) print(resultTab.values) // ["alpha", "beta", "gamma"] // Pipe delimiter let toonPipe = "values[3|]: red|green|blue" let dataPipe = toonPipe.data(using: .utf8)! let resultPipe = try decoder.decode(Data.self, from: dataPipe) print(resultPipe.values) // ["red", "green", "blue"] ``` -------------------------------- ### Decode TOON Inline Arrays to Swift Arrays Source: https://context7.com/alexey1312/toondecoder/llms.txt Shows how to parse TOON data containing inline arrays of primitive types into Swift arrays. The syntax `key[count]: value1,value2,...` is used. ```swift import Foundation import TOONDecoder struct UserPreferences: Codable { let tags: [String] let scores: [Int] } let decoder = TOONDecoder() let toon = """ tags[3]: reading,gaming,coding scores[4]: 95,87,92,88 """ let data = toon.data(using: .utf8)! let prefs = try decoder.decode(UserPreferences.self, from: data) print(prefs.tags) // ["reading", "gaming", "coding"] print(prefs.scores) // [95, 87, 92, 88] ``` -------------------------------- ### Decode Nested TOON Objects using Indentation Source: https://context7.com/alexey1312/toondecoder/llms.txt Illustrates how to decode TOON data with nested structures into corresponding Swift nested structs. Indentation in the TOON format is used to represent the hierarchy. ```swift import Foundation import TOONDecoder struct Profile: Codable { struct Address: Codable { let city: String let zip: String } let name: String let address: Address } let decoder = TOONDecoder() let toon = """ name: Ada address: city: Seattle zip: 98101 """ let data = toon.data(using: .utf8)! let profile = try decoder.decode(Profile.self, from: data) print(profile.address.city) // "Seattle" print(profile.address.zip) // "98101" ``` -------------------------------- ### Decode TOON Tabular Format to Swift Arrays of Structs Source: https://context7.com/alexey1312/toondecoder/llms.txt Demonstrates decoding TOON's tabular format, which represents an array of objects with specified headers, into a Swift array of Codable structs. The format `items[count]{header1,header2,...}: value1,value2,... value1,value2,...` is used. ```swift import Foundation import TOONDecoder struct Product: Codable { let sku: String let qty: Int let price: Double } struct Inventory: Codable { let items: [Product] } let decoder = TOONDecoder() let toon = """ items[3]{sku,qty,price}: A1,5,9.99 B2,3,14.50 C3,1,24.99 """ let data = toon.data(using: .utf8)! let inventory = try decoder.decode(Inventory.self, from: data) for item in inventory.items { print("\(item.sku): \(item.qty) @ $\(item.price)") } // A1: 5 @ $9.99 // B2: 3 @ $14.5 // C3: 1 @ $24.99 ``` -------------------------------- ### Configure Decoding Limits with TOONDecoder Source: https://context7.com/alexey1312/toondecoder/llms.txt Sets resource limits for the TOONDecoder to prevent issues with excessively large or complex input data. This includes maximum input size, nesting depth, object key count, and array element count. ```swift import Foundation import TOONDecoder struct LargeData: Codable { let items: [String] } let decoder = TOONDecoder() decoder.limits = TOONDecoder.DecodingLimits( maxInputSize: 1024 * 1024, // 1 MB maxDepth: 64, // 64 levels deep maxObjectKeys: 1000, // 1000 keys per object maxArrayLength: 10_000 // 10k array elements ) let toon = "items[3]: a,b,c" let data = toon.data(using: .utf8)! do { let result = try decoder.decode(LargeData.self, from: data) print("Decoded \(result.items.count) items") } catch let error as TOONDecodingError { switch error { case .inputTooLarge(let size, let limit): print("Input \(size) bytes exceeds limit \(limit)") case .arrayLengthLimitExceeded(let length, let limit): print("Array length \(length) exceeds limit \(limit)") default: print("Decoding error: \(error)") } } ``` -------------------------------- ### Decoding TOON Tabular Format in Swift Source: https://github.com/alexey1312/toondecoder/blob/main/README.md Illustrates decoding a TOON string with a tabular format into Swift Codable structs. This is useful for handling arrays of uniform objects with field headers. ```swift import TOONDecoder struct Item: Codable { let sku: String let qty: Int let price: Double } struct Order: Codable { let items: [Item] } let toon = """ items[2]{sku,qty,price}: A1,2,9.99 B2,1,14.5 """ let decoder = TOONDecoder() let order = try decoder.decode(Order.self, from: Data(toon.utf8)) print(order.items.count) // 2 ``` -------------------------------- ### Parse Quoted Strings and Escape Sequences with TOONDecoder Source: https://context7.com/alexey1312/toondecoder/llms.txt Illustrates how TOONDecoder handles strings containing special characters, quotes, and escape sequences like newline (\n) and tab (\t). It correctly parses these within quoted string values. ```swift import Foundation import TOONDecoder struct Message: Codable { let text: String let path: String let multiline: String } let decoder = TOONDecoder() let toon = """ text: "This has: special, characters" path: "C:\\Users\\Documents" multiline: "Line 1\nLine 2\tTabbed" """ let data = toon.data(using: .utf8)! let message = try decoder.decode(Message.self, from: data) print(message.text) // "This has: special, characters" print(message.path) // "C:\Users\Documents" print(message.multiline) // "Line 1\nLine 2\tTabbed" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.