c4-swift — Swift Library

c4-swift is a native Swift implementation of C4 content identification. It parses and encodes c4m manifests, diffs and merges them, and identifies content using CryptoKit — all with zero external dependencies and full Swift 6 strict concurrency support.

GitHub: Avalanche-io/c4-swift

Install

Add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/Avalanche-io/c4-swift.git", from: "1.0.5")
]

Quick start

Identify content

import C4

let id = C4ID.identify(data: fileData)
print(id.string)  // c45xZeXwKjQ2...

let parsed = C4ID("c45xZeXwKjQ2...")!
print(id == parsed)  // true

Parse and diff manifests

import C4

let old = try Manifest.unmarshal(oldText)
let current = try Manifest.unmarshal(currentText)
let result = diff(lhs: old, rhs: current)

print("\(result.added.entries.count) added")
print("\(result.removed.entries.count) removed")
print("\(result.modified.entries.count) modified")

Build manifests with the fluent API

import C4

let manifest = ManifestBuilder()
    .addFile(name: "README.md", mode: .file644, size: 1024, c4id: readmeId)
    .addDir(name: "src/", mode: .dir755)
        .addFile(name: "main.swift", mode: .file644, size: 2048, c4id: mainId)
        .end()
    .build()

Three-way merge

import C4

let result = merge(base: ancestor, local: myChanges, remote: theirChanges)
if result.conflicts.isEmpty {
    print("Clean merge")
} else {
    for conflict in result.conflicts {
        print("Conflict at \(conflict.path)")
    }
}

Key features

Interoperability

c4-swift produces byte-identical C4 IDs and c4m files as every other tool in the ecosystem. Cross-language compatibility is verified by shared test vectors from the Go reference implementation.