Validate workspace color during cmux.json decode (#2112)
Reject invalid color values at parse time with a clear error message instead of silently ignoring them. Colors are normalized to #RRGGBB via WorkspaceTabColorSettings.normalizedHex during decode. Co-authored-by: Lawrence Chen <lawrencecchen@users.noreply.github.com>
This commit is contained in:
parent
321f8c14c8
commit
23253e6ddf
1 changed files with 27 additions and 0 deletions
|
|
@ -95,6 +95,33 @@ struct CmuxWorkspaceDefinition: Codable, Sendable {
|
|||
var cwd: String?
|
||||
var color: String?
|
||||
var layout: CmuxLayoutNode?
|
||||
|
||||
init(name: String? = nil, cwd: String? = nil, color: String? = nil, layout: CmuxLayoutNode? = nil) {
|
||||
self.name = name
|
||||
self.cwd = cwd
|
||||
self.color = color
|
||||
self.layout = layout
|
||||
}
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
name = try container.decodeIfPresent(String.self, forKey: .name)
|
||||
cwd = try container.decodeIfPresent(String.self, forKey: .cwd)
|
||||
layout = try container.decodeIfPresent(CmuxLayoutNode.self, forKey: .layout)
|
||||
|
||||
if let rawColor = try container.decodeIfPresent(String.self, forKey: .color) {
|
||||
guard let normalized = WorkspaceTabColorSettings.normalizedHex(rawColor) else {
|
||||
throw DecodingError.dataCorruptedError(
|
||||
forKey: .color,
|
||||
in: container,
|
||||
debugDescription: "Invalid color \"\(rawColor)\". Expected 6-digit hex format: #RRGGBB"
|
||||
)
|
||||
}
|
||||
color = normalized
|
||||
} else {
|
||||
color = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
indirect enum CmuxLayoutNode: Codable, Sendable {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue