From 902fdaf43887bef0de8847950d7c4df4ed1afe3c Mon Sep 17 00:00:00 2001 From: Leif Date: Tue, 30 Jan 2024 18:51:09 -0700 Subject: [PATCH] Handle string value case --- .../Types/Helper/FileManager+AppState.swift | 25 +++++++++++++++++++ .../Types/State/Application+FileState.swift | 9 ++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift b/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift index 9518e38..492a0ae 100644 --- a/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift +++ b/Sources/AppState/Application/Types/Helper/FileManager+AppState.swift @@ -51,6 +51,31 @@ extension FileManager { return try JSONDecoder().decode(Value.self, from: data) } + /// Read a file's data as the type `String` + /// + /// - Parameters: + /// - path: The path to the directory containing the file. The default is `.`, which means the current working directory. + /// - filename: The name of the file to read. + /// - encoding: The String.Encoding to use to get the string. + /// - Returns: The file's data decoded as an instance of `String`. + /// - Throws: If there's an error reading the file or decoding its data. + func string( + path: String = ".", + filename: String, + encoding: String.Encoding = .utf8 + ) throws -> String { + let data = try data( + path: path, + filename: filename + ) + + guard let string = String(data: data, encoding: encoding) else { + throw FileError.invalidStringFromData + } + + return string + } + /// Read a file's data /// /// - Parameters: diff --git a/Sources/AppState/Application/Types/State/Application+FileState.swift b/Sources/AppState/Application/Types/State/Application+FileState.swift index 9b1ac52..5476019 100644 --- a/Sources/AppState/Application/Types/State/Application+FileState.swift +++ b/Sources/AppState/Application/Types/State/Application+FileState.swift @@ -26,7 +26,14 @@ extension Application { } do { - return try fileManager.in(path: path, filename: filename) + if + Value.self == String.self, + let stringValue = try? fileManager.string(path: path, filename: filename) as? Value + { + return stringValue + } else { + return try fileManager.in(path: path, filename: filename) + } } catch { log( error: error,