This repository was archived by the owner on Jan 27, 2024. It is now read-only.
Refactor to idiomatic Swift #1
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Changes
Removed
import Glibcbecause it's redundant withimport Foundation.Removed
SwiftLinuxSerial-prefix from type names because Swift identifiers are already namespaced by module name.Changed enumerations from
SCREAMING_CASEto idiomaticcamelCaseconvention.Convert
BaudRatetospeed_tvia aspeedValueproperty.Convert
DataBitsSizetotcflag_tvia aflagValueproperty.Renamed
SwiftLinuxSerialclass toSerialPort. Since macOS support is added, consider renaming package name (see below).Use optional
fileDescriptorinstead of-1to represent nil/error state.Replaced conditionals checking for magic numbers with
guardclauses.Removed
openPort()'s parameter defaults and added convenience function that callsopenPort(toReceive: true, andTransmit: true)instead. Previously, API wasn't intuitive because to open a read-only serial port, one callsopenPort(transmit: false).Changed
setPortSettings()signature to be Swiftier.Prefer typealiases (e.g.
tcflag_t) over regular integer types, and removed redundant casting.Use platform-specific named tuples for
c_cctuple element access.Moved reading and writing methods into extensions for clarity.
Throw errors from reading and writing methods instead of returning magic numbers.
Removed
-ToPortBlockingsuffixes from method names. The absence of a trailing closure parameter implies blocking call.Changed
readTillCharacterBlocking(characterRep: UnicodeScalar)toreadUntilChar(_: CChar)to better reflect underlying algorithm, i.e. byte-sizeCChars are read in sequence, notUnicodeScalars.Improved
readUntilChar(_:)algorithm to append toDatainstead ofString, thus supporting UTF8 strings with multibyte characters.Updated SwiftLinuxSerialTest to use new APIs.
Use string interpolation instead of concatenation in
print()statements.Changed indentation from tabs to 4 spaces. 83% of Swift repos use 4 spaces.
Additions
import Darwin).PortErrortype. Far more idiomatic than inferring errors from returned tuples or magic numbers.Suggestions
Testsdirectory), rather than creating a separateSwiftLinuxSerialTestpackage.