Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Calligraphy is SwiftUI for strings, files, and folders. Components conform to `S
Compose a multi-line string from components:

```swift
let message = String.build {
let message = String {
Line {
"Hello,"
Space()
Expand Down
2 changes: 1 addition & 1 deletion Snippets/Array/MappingElements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Calligraphy
// snippet.map
let items = ["apple", "banana", "cherry"]

let list = String.build {
let list = String {
items.map { item in
"- \(item)"
}
Expand Down
4 changes: 2 additions & 2 deletions Snippets/ComposingStrings/DeclarativeString.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Compose a multi-line string from components and plain strings with `String.build(_:)`.
// Compose a multi-line string from components and plain strings with `String.init(builder:)`.

// snippet.hide
import Calligraphy

// snippet.show
// snippet.string-build
let declarativeString = String.build {
let declarativeString = String {
Line {
"Hello"
Space()
Expand Down
4 changes: 2 additions & 2 deletions Snippets/Data/CreatingData.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Create Foundation `Data` from a single data component, or from several
// combined components with `Data.build(_:)`.
// combined components with `Data.init(builder:)`.

// snippet.hide
import Calligraphy
Expand All @@ -21,7 +21,7 @@ let data = Data(component)
// snippet.end

// snippet.build
let built = Data.build {
let built = Data {
Header()
Data("payload".utf8)
}
Expand Down
4 changes: 2 additions & 2 deletions Snippets/String/CreatingStrings.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Create a Swift `String` from a single string component, or from several
// combined components with `String.build(_:)`.
// combined components with `String.init(builder:)`.

// snippet.hide
import Calligraphy
Expand All @@ -19,7 +19,7 @@ let string = String(component)
// snippet.end

// snippet.build
let built = String.build {
let built = String {
"Hello, World!"
"This is a declarative, multi-line string"
"Created with Calligraphy!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ You can use the @StringBuilder result builder in several different ways:
- By applying the `@StringBuilder` attribute to any Swift function
- When defining your own `StringComponent`, by implementing the `body` property
- Using the ``Group`` struct, which provides you with an entry point to the DSL and a typed value you can store, pass around, modify, and compose into in other string builders.
- Using the ``Swift/String/build(_:)`` type method, which accepts a `@StringBuilder` closure as an argument.
- Using the ``Swift/String/init(builder:)`` initializer, which accepts a `@StringBuilder` closure as an argument.

### Component Separators

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Inside a component, use the ``Environment`` property wrapper to read a value fro

The property wrapper works the same way in a ``DataComponent``, and in types that conform to ``TextFile``, ``DataFile``, or ``Directory``.

The property wrapper is populated by reflection on the enclosing type, so it only works as a stored property of a component. When you need an environment value somewhere else, such as a free `@StringBuilder` function or a `String.build { ... }` closure, move that piece of content into a small component type and read the value there, the same way you would introduce a `View` in SwiftUI.
The property wrapper is populated by reflection on the enclosing type, so it only works as a stored property of a component. When you need an environment value somewhere else, such as a free `@StringBuilder` function or the ``Swift/String/init(builder:)`` initializer, move that piece of content into a small component type and read the value there, the same way you would introduce a `View` in SwiftUI.

## Reading the File Name

Expand Down
13 changes: 13 additions & 0 deletions Sources/Calligraphy/DataComposition/DataBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public enum DataBuilder {
AnyDataComponent(erasing: component)
}

public static func buildFinalResult<Component>(
_ component: Component
) -> Component where Component: DataComponent {
component
}

@_disfavoredOverload
public static func buildFinalResult(
_ component: some DataComponent
) -> Data {
Data(component)
}

public struct _Assembled<each Component>: DataComponent where repeat each Component: DataComponent {

// MARK: - DataComponent
Expand Down
11 changes: 10 additions & 1 deletion Sources/Calligraphy/DataExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import Foundation
///
/// @Snippet(path: "Calligraphy/Snippets/Data/CreatingData", slice: "component")
///
/// You can also pass in multiple combined components using ``Foundation/Data/build(_:)``:
/// You can also pass in multiple combined components using ``Foundation/Data/init(builder:)``:
///
/// @Snippet(path: "Calligraphy/Snippets/Data/CreatingData", slice: "build")
@available(macOS 14.0, macCatalyst 17.0, iOS 17.0, watchOS 10.0, tvOS 17.0, visionOS 1.0, *)
Expand All @@ -52,10 +52,19 @@ extension Data {
/// Create data from multiple data components
/// - Parameter components: The data components
/// - Returns: The rendered data
@available(*, deprecated, message: "Use .init(builder:) instead")
public static func build(
@DataBuilder _ components: () -> some DataComponent
) -> Data {
self.init(components())
}

/// Build data with a `@DataBuilder`
/// - Parameter builder: The components to assemble into data
public init(
@DataBuilder builder: () -> Data
) {
self = builder()
}

}
11 changes: 10 additions & 1 deletion Sources/Calligraphy/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
///
/// @Snippet(path: "Calligraphy/Snippets/String/CreatingStrings", slice: "component")
///
/// You can also pass in multiple combined components using ``Swift/String/build(_:)``:
/// You can also pass in multiple combined components using ``Swift/String/init(builder:)``:
///
/// @Snippet(path: "Calligraphy/Snippets/String/CreatingStrings", slice: "build")
@available(macOS 14.0, macCatalyst 17.0, iOS 17.0, watchOS 10.0, tvOS 17.0, visionOS 1.0, *)
Expand All @@ -50,10 +50,19 @@ extension String {
/// Build a string with a `@StringBuilder`
/// - Parameter components: The components to assemble into a string
/// - Returns: The string
@available(*, deprecated, message: "Use .init(builder:) instead")
public static func build(
@StringBuilder _ components: () -> some StringComponent
) -> String {
.init(components())
}

/// Build a string with a `@StringBuilder`
/// - Parameter builder: The components to assemble into a string
public init(
@StringBuilder builder: () -> String
) {
self = builder()
}

}
60 changes: 60 additions & 0 deletions Tests/CalligraphyTests/DataExtensionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Calligraphy
// DataExtensionsTests.swift
//
// MIT License
//
// Copyright (c) 2026 Varun Santhanam
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
//
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import Calligraphy
import Foundation
import Testing

@Suite("Data Extensions Tests", .tags(.dataComposition))
struct DataExtensionsTests {

@Test("Initializer")
func initializer() {
let data = Data(RawDataComponent(Data([0x01, 0x02])))
#expect(data == Data([0x01, 0x02]))
}

@Test("Initializer with Empty Component")
func initializerEmpty() {
let data = Data(EmptyDataComponent())
#expect(data == Data())
}

@Test("Builder Initializer")
func builder() {
let data = Data {
0x01 as UInt8
Data([0x02, 0x03])
}
#expect(data == Data([0x01, 0x02, 0x03]))
}

@Test("Builder Initializer with Empty Block")
func builderEmpty() {
let data = Data {}
#expect(data == Data())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct LineSpacingTests {

@Test("Modifier")
func modifier() {
let str = String.build {
let str = String {
Lines {
Lines {
"foo"
Expand Down
6 changes: 3 additions & 3 deletions Tests/CalligraphyTests/StringExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ struct StringExtensionsTests {
#expect(str == "")
}

@Test("Build type method")
func build() {
let str = String.build {
@Test("Builder Initializer")
func builder() {
let str = String {
RawStringComponent("Foo")
RawStringComponent("Bar")
}
Expand Down
Loading