Skip to content

Commit d5da4fe

Browse files
g-cqdclaude
andcommitted
Add comprehensive DocC documentation and remove dev artifacts
- Document CSVParser with usage examples, thread safety notes, and performance characteristics (SIMD acceleration, RFC 4180 compliance) - Document CSVRowView zero-copy methods with complexity annotations - Enhance CSVEncoder/CSVDecoder with usage examples, configuration guides, and thread safety guarantees - Document CSVDecodingError intelligent suggestions system - Document CSVEncodingError with nested type handling guidance - Document LocaleUtilities for locale-aware parsing - Remove MIGRATION_PLAN.md development artifact 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 55d9577 commit d5da4fe

7 files changed

Lines changed: 395 additions & 218 deletions

File tree

MIGRATION_PLAN.md

Lines changed: 0 additions & 198 deletions
This file was deleted.

Sources/CSVCoder/Core/CSVDecoder.swift

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,87 @@
77

88
import Foundation
99

10-
/// A decoder that decodes CSV data into Decodable types.
10+
// MARK: - CSVDecoder
11+
12+
/// A type-safe decoder that converts CSV data to `Decodable` types.
13+
///
14+
/// `CSVDecoder` provides a familiar API similar to `JSONDecoder`, with rich
15+
/// configuration options for dates, numbers, booleans, key transformations,
16+
/// and locale-aware parsing.
17+
///
18+
/// ## Basic Usage
19+
///
20+
/// ```swift
21+
/// struct Person: Codable {
22+
/// let name: String
23+
/// let age: Int
24+
/// }
25+
///
26+
/// let csv = """
27+
/// name,age
28+
/// Alice,30
29+
/// Bob,25
30+
/// """
31+
///
32+
/// let decoder = CSVDecoder()
33+
/// let people: [Person] = try decoder.decode(from: csv)
34+
/// ```
35+
///
36+
/// ## Configuration
37+
///
38+
/// Customize decoding via ``Configuration``:
39+
///
40+
/// ```swift
41+
/// var config = CSVDecoder.Configuration()
42+
/// config.delimiter = ";"
43+
/// config.dateDecodingStrategy = .flexible
44+
/// config.keyDecodingStrategy = .convertFromSnakeCase
45+
///
46+
/// let decoder = CSVDecoder(configuration: config)
47+
/// ```
48+
///
49+
/// ## Thread Safety
50+
///
51+
/// `CSVDecoder` is `Sendable` and safe to share across actor boundaries.
52+
/// The decoder is stateless; all configuration is immutable after initialization.
53+
/// Multiple concurrent decodes can safely share the same decoder instance.
54+
///
55+
/// ## Performance
56+
///
57+
/// - Zero-copy parsing using ``CSVParser`` for UTF-8 data
58+
/// - SIMD-accelerated field scanning for large files
59+
/// - Streaming and parallel decoding extensions available
60+
///
61+
/// ## RFC 4180 Compliance
62+
///
63+
/// Supports both lenient (default) and strict parsing modes. Lenient mode
64+
/// tolerates minor violations; strict mode enforces full compliance with
65+
/// detailed error locations.
66+
///
67+
/// ## See Also
68+
///
69+
/// - ``CSVEncoder`` for encoding types to CSV
70+
/// - ``Configuration`` for available options
71+
/// - ``CSVParser`` for low-level parsing
1172
public final class CSVDecoder: Sendable {
1273

13-
/// Configuration for CSV parsing.
74+
/// Configuration options for CSV decoding.
75+
///
76+
/// All properties have sensible defaults. Customize only what you need:
77+
///
78+
/// ```swift
79+
/// var config = CSVDecoder.Configuration()
80+
/// config.delimiter = "\t" // Tab-separated
81+
/// config.trimWhitespace = false
82+
/// ```
83+
///
84+
/// ## Configuration Priority
85+
///
86+
/// Header resolution follows this precedence:
87+
/// 1. ``indexMapping`` - Explicit column index → property name mapping
88+
/// 2. ``hasHeaders`` with ``keyDecodingStrategy`` - Transform header names
89+
/// 3. `CSVIndexedDecodable` conformance - Use type's `CodingKeys` order
90+
/// 4. Generated column names (`column0`, `column1`, ...)
1491
public struct Configuration: Sendable {
1592
/// The delimiter character used to separate fields. Default is comma (,).
1693
public var delimiter: Character

Sources/CSVCoder/Core/CSVDecodingError.swift

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,28 @@
77

88
import Foundation
99

10-
/// Location information for errors in CSV data.
10+
// MARK: - CSVLocation
11+
12+
/// Precise location information for errors in CSV data.
13+
///
14+
/// `CSVLocation` provides detailed context for debugging decoding failures,
15+
/// including row numbers, column identifiers, coding paths, and available
16+
/// keys for generating helpful suggestions.
17+
///
18+
/// ## Usage
19+
///
20+
/// Access location information from ``CSVDecodingError`` via the ``CSVDecodingError/location`` property:
21+
///
22+
/// ```swift
23+
/// do {
24+
/// let items = try decoder.decode([Item].self, from: data)
25+
/// } catch let error as CSVDecodingError {
26+
/// if let location = error.location {
27+
/// print("Error at \(location)")
28+
/// // e.g., "row 5, column 'price', path: items.price"
29+
/// }
30+
/// }
31+
/// ```
1132
public struct CSVLocation: Sendable, Equatable, CustomStringConvertible {
1233
/// The 1-based row number in the CSV file.
1334
public let row: Int?
@@ -52,7 +73,39 @@ public struct CSVLocation: Sendable, Equatable, CustomStringConvertible {
5273
}
5374
}
5475

55-
/// Errors that can occur during CSV decoding.
76+
// MARK: - CSVDecodingError
77+
78+
/// Errors that occur during CSV decoding with intelligent suggestions.
79+
///
80+
/// `CSVDecodingError` provides detailed error information including:
81+
/// - Precise location (row, column, coding path)
82+
/// - Typo detection using edit distance for key mismatches
83+
/// - Context-aware suggestions for fixing common issues
84+
///
85+
/// ## Error Handling
86+
///
87+
/// ```swift
88+
/// do {
89+
/// let items = try decoder.decode([Item].self, from: data)
90+
/// } catch let error as CSVDecodingError {
91+
/// print(error.errorDescription ?? "Unknown error")
92+
/// if let suggestion = error.suggestion {
93+
/// print("Suggestion: \(suggestion)")
94+
/// }
95+
/// }
96+
/// ```
97+
///
98+
/// ## Intelligent Suggestions
99+
///
100+
/// The error system provides context-aware suggestions:
101+
/// - **Key not found**: Suggests similar column names using Levenshtein distance
102+
/// - **Type mismatch**: Suggests appropriate decoding strategies (e.g., `.flexible` for European numbers)
103+
/// - **Parsing errors**: Suggests delimiter or quote fixes
104+
///
105+
/// ## See Also
106+
///
107+
/// - ``CSVLocation`` for error position details
108+
/// - ``CSVDecoder/Configuration`` for strategy options
56109
public enum CSVDecodingError: Error, LocalizedError, Sendable {
57110
/// The data could not be decoded with the specified encoding.
58111
case invalidEncoding

0 commit comments

Comments
 (0)