lucaswkuipers/safetypesmacros
SafeTypesMacros is a _Swift_ package that extends [SafeTypes](https://github.com/lucaswkuipers/SafeTypes) by adding macro literal initializers for its types.
Features
- Macros for non optional initializers for SafeTypes custom data types through literals (values checked at compile time).
- [x] Type-safe containers that prevent invalid states
- [x] Enforced runtime constraints at compile time
- [x] Enhanced code readability and maintainability
- [x] Simplified method interfaces and APIs
- [x] Streamlined unit testing by eliminating redundant unhappy-path checks
Installation
Swift Package Manager
Add the following to your Package.swift file's dependencies:
.package(url: "https://github.com/lucaswkuipers/SafeTypesMacros.git", from: "1.0.0")Or File > Add Package Dependencies, and enter the following URL:
https://github.com/lucaswkuipers/SafeTypesMacros.gitIf you are manipulating the resulting data type it's recommended installing the SafeTypes base library too:
.package(url: "https://github.com/lucaswkuipers/SafeTypes.git", from: "1.0.0")or File > Add Package Dependencies, and enter the following URL:
https://github.com/lucaswkuipers/SafeTypes.gitAnd then import wherever needed:
import SafeTypesMacros
import SafeTypes // OptionallyUsage
Below are some of the Macros provided for SafeTypes and brief examples of their usage:
Collections
NonEmptyString
Macro for string that's guaranteed to contain at least one character (can be blank or invisible character).
// ✅ Compiles
#NonEmptyString("Alice") // NonEmptyString
#NonEmptyString(" Bob ") // NonEmptyString
#NonEmptyString(" ") // NonEmptyString
// ❌ Fails
#NonEmptyString("") // String can't be empty
#NonEmptyString() // No argumentNumbers
Where number is:
typealias Number = Numeric & ComparablePositive
Macro for a number that is guaranteed to be greater than zero (value > 0)
// ✅ Compiles
#Positive(123) // Positive<Int>
#Positive(42.69) // Positive<Double>
// ❌ Fails
#Positive(-1) // Can't be negative
#Positive(0) // Can't be zero
#Positive() // No argumentNegative
Macro for A number that is guaranteed to be less than zero (value < 0)
// ✅ Compiles
#Negative(-123) // Negative<Int>
#Negative(-42.69) // Negative<Double>
// ❌ Fails
#Negative(1) // Can't be positive
#Negative(0) // Can't be zero
#Negative() // No argumentNonPositive
Macro for a number that is guaranteed to be less than or equal to zero (value <= 0)
// ✅ Compiles
#NonPositive(-123) // NonPositive<Int>
#NonPositive(-42.69) // NonPositive<Double>
#NonPositive(0) // NonPositive<Int>
#NonPositive(0.0) // NonPositive<Double>
// ❌ Fails
#NonPositive(1) // Can't be positive
#NonPositive() // No argumentNonNegative
Macro for number that is guaranteed to be greater than or equal to zero (value >= 0)
// ✅ Compiles
#NonNegative(123) // NonNegative<Int>
#NonNegative(42.69) // NonNegative<Double>
#NonNegative(0) // NonNegative<Int>
#NonNegative(0.0) // NonNegative<Double>
// ❌ Fails
#NonNegative(-273.15) // Can't be negative
#NonNegative() // No argumentNonZero
Macro for a number that is guaranteed to be different than zero (value != 0)
// ✅ Non Optional Initializers
#NonZero(123) // NonZero<Int>
#NonZero(42.69) // NonZero<Double>
#NonZero(-123) // NonZero<Int>
#NonZero(-42.69) // NonZero<Double>
// ❌ Fails to compile
#NonZero(0) // Can't be zero
#NonZero(0.0) // Can't be zero
#NonZero() // No argumentMinusOneToOne
Macro for number that's within the range of -1 to 1, inclusive. ([-1, 1])
// ✅ Compiles
#MinusOneToOne(-1) // MinusOneToOne<Int>
#MinusOneToOne(-1.0) // MinusOneToOne<Double>
#MinusOneToOne(-0.314159) // MinusOneToOne<Double>
#MinusOneToOne(0) // MinusOneToOne<Int>
#MinusOneToOne(0.0) // MinusOneToOne<Double>
#MinusOneToOne(0.1234) // MinusOneToOne<Double>
#MinusOneToOne(1) // MinusOneToOne<Double>
// ❌ Fails
#MinusOneToOne(-1.1) // Can't be less than -1
#MinusOneToOne(42.1) // Can't be greater than 1
#MinusOneToOne() // No ArgumentZeroToOne
Macro for number from 0 to 1, inclusive.
// ✅ Compiles
#ZeroToOne(0) // ZeroToOne<Int>
#ZeroToOne(0.0) // ZeroToOne<Double>
#ZeroToOne(0.1234) // ZeroToOne<Double>
#ZeroToOne(1) // ZeroToOne<Double>
// ❌ Fails
#ZeroToOne(-0.5) // Can't be less than 0
#ZeroToOne(42.1) // Can't be greater than 1
#ZeroToOne() // No argumentEach type guarantees compliance with its stated constraints so that your functions and methods can rely on those qualities and pass them on (preserving information).
Contributing
Contributions are what make the open-source community such a fantastic place to learn, inspire, and create. Any contributions you make are greatly appreciated.
What to contribute?
If you want to start contributing but don't know what to work on, try looking at the open Issues Tab
Steps to contribute
- Fork the Project
- Create your Branch (
git checkout -b your_branch_name) - Commit your Changes (
git commit -m 'Add some amazing feature') - Push to the Branch (
git push origin your_branch_name) - Open a Pull Request
- Oh and don't forget to add or update tests when applicable! :D
Thank you so much for contributing <3
License
Distributed under the MIT License. See LICENSE for more information.
Contact
Feel free to reach out to me:
[[image]](https://www.linkedin.com/in/lucaswk/)
Swift Package Iindex
SafeTypes can be found at Swift Package Index
Acknowledgements
Some of the relevant sources of inspiration:
Thank you so much for considering SafeTypes and SafeTypesMacros for your next Swift project – I hope you find it as enjoyable to use as I found it to write!
Package Metadata
Repository: lucaswkuipers/safetypesmacros
Default branch: master
README: README.md