A package for parsing and evaluating semantic versioning in Swift.
This is a binding to semver, which uses Cargo's flavor of semantic versioning. See semver's README for more information.
Bridge between Rust and Swift is generated using Swift Bridge.
In your Package.swift
dependencies: [
.package(url: "https://github.com/jomy10/SemverSwift", from: "1.0.0")
],
targets: [
.target(
name: "YourTarget",
dependencies: [
.product(name: "Semver", package: "SemverSwift")
]
)
]
-
Instructions on adding a package to your XCode project (brief)
- Replace the url they ask you to put in with
https://github.com/jomy10/SemverSwift
- Replace the url they ask you to put in with
let version = Version(major: 1, minor: 0, patch: 3)
let versionParsed = try Version.parse("1.0.3").get()
XCTAssertEqual(version, versionParsed)
Of course in an actual application, you might want to implement some bother error handling such as guard
or switch
;
let versionParsedResult = Version.parse("1.0.3")
guard case .success(let versionParsed) = versionParsedResult else {
XCTFail()
}
You can test if a version matches a specific version requirement using the VersionReq.match
method:
let versionReq = try VersionReq.parse(">=1.0.0").get()
let version = try Version.parse("1.0.0").get()
let lowerVersion = try Version.parse("0.9.8").get()
XCTAssert(
versionReq.matches(version: version)
)
XCTAssertFalse(
versionReq.matches(version: lowerVersion)
)
The *
operator matches any version:
let versionReq = try VersionReq.parse("*").get()
let otherVersionReq = VersionReq.STAR
XCTAssertEqual(versionReq, otherVersionReq)
let version = Version(major: 1, minor: 50, patch: 69)
// Will always match
XCTAssert(versionReq.matches(version: version))
You can also define ranges:
let versionReq = try VersionReq.parse(">=1.2.3, <1.8").get()
let versionIn = try Version.parse("1.6.3").get()
let versionOut = try Version.parse("1.8.0").get()
XCTAssert(versionReq.matches(version: versionIn))
XCTAssertFalse(versionReq.matches(version: versionOut))
The package currently supports macOS, iOS and iOS Simulator.
Support for Linux is possible if you compile your program using swiftc
.
More information can be found in the Swift Bridge
documentation.
In this case, it will boil down to copying the files in the generated
folder (after building) and copying the semver.swift
file
in the Semver
package to your project (and removing the import statements, except for Foundation). Then also copying
the static library obtained using cargo build --target {linux-target}
. After that, compile your program with swiftc
according
to the Swift Bridge documentation. If you need Linux support, feel free to open an issue and I will guide you through the process
and write a more thorough documentation.
Support for other Apple platforms has not been tested, but we will probably need to compile Rust to those platforms, there is no official support for the other Apple platforms yet however.
This directory contains the Rust source files that bridge the Rust semver
package to Swift using swift-bridge
.
This is the package generated by Swift Bridge.
This is the package that further refines the generated package for Swift, providing a nicer public API.
Clone this repository
git clone https://github.com/jomy10/SemverSwift
To create a Swift package from the Rust source files, you will need to have the swift-bridge-cli
installed:
cargo install -f swift-bridge-cli
To build the project for macOS, iOS, and iOS Simulator, run the following:
If you use bash, or any other shell than zsh, you can change the interpreter at the top of the build.sh
file.
If you would like the SwiftBridge
package to be build somewhere else, go ahead and change that first (the default is the folder above the cloned repo)
cd SemverRustBridge
./build.sh RELEASE
This will build the SemverBridge
package. In the Package.swift
of this cloned repo, change the dependency to point to this generated file.
To build a debug version for macOS, run:
./build.sh
# or
./build.sh DEBUG
To run the tests:
swift test
This project will follow updates of semver. The current version is 1.0.6
.
A number of features are currently unimplemented and are planned to be implemented soon (help is appreciated).
BuildMetadata
See CONTRIBUTING.md.
This package is dual licensed under Apache 2.0 and MIT licenses, just like the original crate.