[Improvement] Naming of libraries and package (#22)
This PR contains the work done to fix the naming of the libraries and the package, along with tweaks to the folder structure. Reviewed-on: #22 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #22.
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public enum BundleError: Error {
|
||||
case bundleNotFound
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public extension Bool {
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
/// Initialise a boolean primitive out of a given string.
|
||||
/// - Parameter string: A string to initialise the boolean with.
|
||||
init(_ string: String) {
|
||||
let strings: [String] = [
|
||||
.Constants.oneNumber,
|
||||
.Constants.oneWord,
|
||||
.Constants.true,
|
||||
.Constants.yes
|
||||
]
|
||||
|
||||
self = strings.contains(string.lowercased())
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
enum Constants {
|
||||
static let yes = "yes"
|
||||
static let `true` = "true"
|
||||
static let oneWord = "one"
|
||||
static let oneNumber = "1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Bundle {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Retrieve a localisation bundle for a given language code or identifier, if exist inside a certain bundle.
|
||||
/// - Parameter languageCode: A string that represent a language code or identifier.
|
||||
/// - Returns: A `Bundle` instance that contains localised resources based on a given language code or identifier.
|
||||
/// - Throws: A `BundleError` error in case the localisation bundle for a given language code or identifier is not found inside a certain bundle.
|
||||
func localisation(for languageCode: String) throws -> Bundle {
|
||||
guard
|
||||
let path = path(forResource: languageCode, ofType: .ResourceType.localisationBundle),
|
||||
let bundle = Bundle(path: path)
|
||||
else {
|
||||
throw BundleError.bundleNotFound
|
||||
}
|
||||
|
||||
return bundle
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - String+Constants
|
||||
|
||||
private extension String {
|
||||
enum ResourceType {
|
||||
static let localisationBundle = "lproj"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public extension Collection {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A Boolean value indicating whether a collection is not empty.
|
||||
var isNotEmpty: Bool { !isEmpty }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public extension Optional {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A Boolean value indicating whether an optional has not been set.
|
||||
var isNil: Bool { self == nil }
|
||||
|
||||
/// A Boolean value indicating whether an optional has been set.
|
||||
var isNotNil: Bool { !isNil }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public extension String {
|
||||
|
||||
// MARK: Constants
|
||||
|
||||
/// A string that represents an empty string.
|
||||
static let empty = ""
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A Boolean value indicating whether a string is not empty.
|
||||
var isNotEmpty: Bool { !isEmpty }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension String {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Localise a string based on a given language code or identifier in an specific bundle.
|
||||
/// - Parameters:
|
||||
/// - languageCode: A string that represent a language code or identifier.
|
||||
/// - bundle: A bundle in which to retrieve a localisation bundle.
|
||||
/// - value: A default value to return if key is nil or if a localized string for key can't be found in the table.
|
||||
/// - table: The receiver's string table to search. In case of nil or an empty string, the method attempts to use the table in `Localizable.strings`.
|
||||
/// - Returns: A localized version of the string in case it is found. Otherwise, it returns the original string or a default string, if provided.
|
||||
func localise(
|
||||
for languageCode: String,
|
||||
in bundle: Bundle,
|
||||
value: String? = nil,
|
||||
table: String? = nil
|
||||
) -> String {
|
||||
do {
|
||||
return try bundle
|
||||
.localisation(for: languageCode)
|
||||
.localizedString(
|
||||
forKey: self,
|
||||
value: value,
|
||||
table: table
|
||||
)
|
||||
} catch {
|
||||
return value ?? self
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import Foundation
|
||||
|
||||
public extension TimeZone {
|
||||
|
||||
// MARK: Zones
|
||||
|
||||
/// A time zone indicating it is Greenwich Mean Time or UTC+0
|
||||
static let gmt = TimeZone(secondsFromGMT: 0)
|
||||
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the SwiftLibs open source project
|
||||
//
|
||||
// Copyright (c) 2023 Röck+Cöde VoF. and the SwiftLibs project authors
|
||||
// Licensed under the EUPL 1.2 or later.
|
||||
//
|
||||
// See LICENSE.txt for license information
|
||||
// See CONTRIBUTORS.txt for the list of SwiftLibs project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
/// This struct (that could also be used as a property wrapper as well) provides a generic type that acts as a thin wrapper around an array of `Elements` instances to allow a lossy decoding and or encoding process.
|
||||
///
|
||||
/// This implementation is heavily influenced by this [article](https://www.swiftbysundell.com/articles/ignoring-invalid-json-elements-codable/).
|
||||
@propertyWrapper
|
||||
public struct LossyCodableList<Element> {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
private var elements: [Element]
|
||||
|
||||
/// Provides read/write access to the array of `Element` instances.
|
||||
public var wrappedValue: [Element] {
|
||||
get { elements }
|
||||
set { elements = newValue }
|
||||
}
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
/// Initialises this struct.
|
||||
public init() {
|
||||
self.elements = []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Decodable
|
||||
|
||||
extension LossyCodableList: Decodable where Element: Decodable {
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
/// Initialises the struct with a lossy decoder.
|
||||
/// - Parameter decoder: The decoder to use for the lossy decoder process.
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
let wrappers = try container.decode([ElementWrapper].self)
|
||||
|
||||
self.elements = wrappers.compactMap(\.element)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Encodable
|
||||
|
||||
extension LossyCodableList: Encodable where Element: Encodable {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Encodes an array of `Element` instances loosely.
|
||||
/// - Parameter encoder: The encoder to use for the lossy encoding process.
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.unkeyedContainer()
|
||||
|
||||
elements.forEach { element in
|
||||
try? container.encode(element)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Structs
|
||||
|
||||
private extension LossyCodableList where Element: Decodable {
|
||||
struct ElementWrapper: Decodable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
var element: Element?
|
||||
|
||||
// MARK: Initialisers
|
||||
|
||||
init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
|
||||
self.element = try? container.decode(Element.self)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user