Skip to content

Commit

Permalink
Add FileName for the name property of files
Browse files Browse the repository at this point in the history
Resolves #2
  • Loading branch information
danielctull committed Jun 26, 2024
1 parent c4e6d2a commit 3ee050a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Sources/FileBuilder/File/DataFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import Foundation

public struct DataFile: File {

private let name: String
private let name: FileName
private let data: Data

public init(_ name: String, data: Data) {
public init(_ name: FileName, data: Data) {
self.name = name
self.data = data
}

public var file: some File {
BuiltinFile { directory, _ in
let url = directory.appendingPathComponent(name)
let url = directory.appending(name)
try data.write(to: url)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/FileBuilder/File/Directory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import Foundation

public struct Directory<Content: File>: File {

private let name: String
private let name: FileName
private let content: Content

public init(
_ name: String,
_ name: FileName,
@FileBuilder content: () -> Content
) {
self.name = name
Expand All @@ -16,7 +16,7 @@ public struct Directory<Content: File>: File {

public var file: some File {
BuiltinFile { directory, environment in
let url = directory.appendingPathComponent(name)
let url = directory.appending(name)
try FileManager().createDirectory(at: url, withIntermediateDirectories: true)
try content.write(in: url, environment: environment)
}
Expand All @@ -25,7 +25,7 @@ public struct Directory<Content: File>: File {

extension Directory<Empty> {

public init(_ name: String) {
public init(_ name: FileName) {
self.init(name) {
Empty()
}
Expand Down
30 changes: 30 additions & 0 deletions Sources/FileBuilder/File/FileName.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Foundation

public struct FileName: Equatable, Hashable {
fileprivate let value: String
public init(_ value: String) {
self.value = value
}
}

extension FileName: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}

extension FileName: ExpressibleByStringInterpolation {}

extension FileName: CustomStringConvertible {
public var description: String { value }
}

extension FileName: CustomDebugStringConvertible {
public var debugDescription: String { value }
}

extension URL {
func appending(_ name: FileName) -> URL {
appendingPathComponent(name.value)
}
}
8 changes: 4 additions & 4 deletions Sources/FileBuilder/File/TextFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import Foundation
public struct TextFile<Content: Text>: File {

@Environment(\.stringEncoding) private var encoding: String.Encoding
private let name: String
private let name: FileName
private let text: Content

public init(
_ name: String,
_ name: FileName,
@TextBuilder text: () -> Content
) {
self.name = name
Expand All @@ -21,12 +21,12 @@ public struct TextFile<Content: Text>: File {
guard let data = string.data(using: encoding) else {
throw TextFileFailure(name: name)
}
let url = directory.appendingPathComponent(name)
let url = directory.appending(name)
try data.write(to: url)
}
}
}

struct TextFileFailure: Error {
let name: String
let name: FileName
}

0 comments on commit 3ee050a

Please sign in to comment.