Skip to content

Commit

Permalink
fix: lint-issues
Browse files Browse the repository at this point in the history
  • Loading branch information
devahmedshendy committed Aug 3, 2023
1 parent 0de8805 commit 8cc80e0
Show file tree
Hide file tree
Showing 22 changed files with 356 additions and 276 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/brightdigit/Contribute.git",
"state" : {
"revision" : "2dbdcdc99861f589639f1828b10ec3c459323639",
"version" : "1.0.0-alpha.2"
"branch" : "v1.0.0",
"revision" : "72290ab018d72870cf243fe99366fab63a7ef979"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
dependencies: [
.package(
url: "https://github.com/brightdigit/Contribute.git",
from: "1.0.0-alpha.2"
branch: "v1.0.0"
),
.package(
url: "https://github.com/brightdigit/SyndiKit.git",
Expand Down
13 changes: 6 additions & 7 deletions Sources/ContributeWordPress/Decoder/BaseURLSite.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import Foundation
import SyndiKit

protocol BaseURLSite {
/// A protocol representing a site with a base URL.
public protocol BaseURLSite {
/// The main URL of the site.
var link: URL { get }

/// The base URL of the blog, if available.
var baseBlogURL: URL? { get }
}

extension BaseURLSite {
/// The base URL of the site.
public var baseURL: URL {
baseBlogURL ?? link
}

Check warning on line 17 in Sources/ContributeWordPress/Decoder/BaseURLSite.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/BaseURLSite.swift#L15-L17

Added lines #L15 - L17 were not covered by tests
}

extension RSSChannel: BaseURLSite {
var baseBlogURL: URL? {
wpBaseBlogURL
}
}
20 changes: 0 additions & 20 deletions Sources/ContributeWordPress/Decoder/PostsExportDecoder.swift

This file was deleted.

20 changes: 20 additions & 0 deletions Sources/ContributeWordPress/Decoder/SitesExportDecoder.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation
import SyndiKit

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

/// A typealias that represents the section name for the blog
public typealias SectionName = String

/// A protocol for decoding WordPress sites from exports.
public protocol SitesExportDecoder {
/// Returns a dictionary of WordPress sites keyed by the filename of
/// the export file as section name.
///
/// - Parameter directoryURL: The URL of the directory containing the exports.
/// - Returns: A dictionary of WordPress sites keyed by section name.
/// - Throws: An error if sites couldn't be extracted from any of the export files.
func sites(fromExportsAt directoryURL: URL) throws -> [SectionName: WordPressSite]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SyndiKit
#endif

/// A type that decodes WordPress export files using the `SynDecoder`.
public struct PostsExportSynDecoder: PostsExportDecoder {
public struct SitesExportSynDecoder: SitesExportDecoder {
/// The decoder used to decode the WordPress export file.
private let decoder: WordPressDecoder = SynDecoder()

Check warning on line 12 in Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift#L12

Added line #L12 was not covered by tests

Expand All @@ -21,48 +21,44 @@ public struct PostsExportSynDecoder: PostsExportDecoder {

public init() {}

Check warning on line 22 in Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift#L22

Added line #L22 was not covered by tests

/// The method decodes posts from the given file URL.
/// The method decodes `WordPressSite` from the given file URL.
///
/// - Parameters:
/// - url: The URL of file containing WordPress posts to be decoded.
/// - url: The URL of export file containing the WordPress site with its posts.
/// - decoder: The WordPress decoder.
/// - Returns: An array of WordPress posts.
/// - Throws: An error of posts couldn't be decoded from the given file..
private static func postsFromURL(
/// - Returns: The decoded WordPress site, or nil if decoding failed.
/// - Throws: An error of site couldn't be decoded from the given file..
private static func siteFromURL(
_ url: URL,
using decoder: WordPressDecoder
) throws -> WordPressSite? {
let data = try Data(contentsOf: url)
return try decoder.decodePosts(fromData: data, allowInvalidCharacters: true)
return try decoder.decodeSites(fromData: data, allowInvalidCharacters: true)
}

Check warning on line 37 in Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift#L34-L37

Added lines #L34 - L37 were not covered by tests

/// Returns a dictionary of WordPress posts keyed by filename.
/// Returns a dictionary of WordPress sites keyed by filename.
///
/// - Parameter directoryURL: The URL of the directory containing the exports.
/// - Returns: A dictionary of WordPress posts keyed by section name.
/// - Throws: An error if posts couldn't be extracted from any of the export files.
public func posts(
/// - Returns: A dictionary of WordPress sites keyed by section name.
/// - Throws: An error if sites couldn't be extracted from any of the export files.
public func sites(
fromExportsAt directoryURL: URL
) throws -> [SectionName: WordPressSite] {
let files = try fileURLsFromDirectory(directoryURL)

let feedPairs = try files.map { url -> (String, WordPressSite?) in
try (self.keyFromURL(url), Self.postsFromURL(url, using: decoder))
try (self.keyFromURL(url), Self.siteFromURL(url, using: decoder))
}

return Dictionary(uniqueKeysWithValues: feedPairs).compactMapValues { $0 }
}

Check warning on line 54 in Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SitesExportSynDecoder.swift#L46-L54

Added lines #L46 - L54 were not covered by tests
}

extension PostsExportSynDecoder {
extension SitesExportSynDecoder {
/// A default logic for finding all files found at given directory.
///
/// If the `directoryURL` is not valid directory, this method
/// throws `ImportError.directory(:_)` error.
///
/// - Parameter directoryURL: The directory URL.
/// - Returns: An array of export file URLs that can be found in the given directory.
/// - Throws: An error if the directory could not be enumerated.
/// - Returns: An array of URLs for export files found in the given directory.
private static func defaultFileURLs(atDirectory directoryURL: URL) -> [URL] {
let enumerator = FileManager.default.enumerator(
at: directoryURL,
Expand Down
34 changes: 7 additions & 27 deletions Sources/ContributeWordPress/Decoder/SynDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,19 @@ import SyndiKit
import FoundationNetworking
#endif

/// An extension that enables SynDecoder to decode WordPress posts.
/// An extension that enables SynDecoder to decode WordPress sites.
extension SynDecoder: WordPressDecoder {
public func decodePosts(
fromData data: Data,
allowInvalidCharacters: Bool
) throws -> WordPressSite? {
let text = String(bytes: data, encoding: .utf8)?
.replacingOccurrences(of: "\u{10}", with: "")
.data(using: .utf8, allowLossyConversion: true)

let newData: Data
if let text = text, allowInvalidCharacters {
newData = text
} else {
newData = data
}

let feed = try decode(newData)
let rss = feed as? RSSFeed
return try rss.map(\.channel).map(WordPressSite.init)
}

/// Decodes an array of WordPress posts from the given data.
/// Decodes an array of WordPress sites from the given data.
///
/// - Parameters:
/// - data: The data to decode.
/// - allowInvalidCharacters: Whether to allow invalid characters in decoded data.
/// - Returns: An array of WordPress posts, or nil if decoding failed.
/// - allowInvalidCharacters: Whether to allow invalid characters in the data.
/// - Returns: The decoded WordPress site, or nil if decoding failed.
/// - Throws: An error if data couldn't be decoded.
public func decodePosts(
public func decodeSites(
fromData data: Data,
allowInvalidCharacters: Bool
) throws -> [WordPressPost]? {
) throws -> WordPressSite? {

Check warning on line 20 in Sources/ContributeWordPress/Decoder/SynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SynDecoder.swift#L20

Added line #L20 was not covered by tests
let text = String(bytes: data, encoding: .utf8)?
.replacingOccurrences(of: "\u{10}", with: "")
.data(using: .utf8, allowLossyConversion: true)
Expand All @@ -51,6 +31,6 @@ extension SynDecoder: WordPressDecoder {

let feed = try decode(newData)
let rss = feed as? RSSFeed
return rss?.channel.items.compactMap(\.wpPost)
return try rss.map(\.channel).map(WordPressSite.init)

Check warning on line 34 in Sources/ContributeWordPress/Decoder/SynDecoder.swift

View check run for this annotation

Codecov / codecov/patch

Sources/ContributeWordPress/Decoder/SynDecoder.swift#L34

Added line #L34 was not covered by tests
}
}
8 changes: 4 additions & 4 deletions Sources/ContributeWordPress/Decoder/WordPressDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import SyndiKit
import FoundationNetworking
#endif

/// A protocol for decoding WordPress posts from raw data.
/// A protocol for decoding WordPress sites from raw data.
public protocol WordPressDecoder {
/// Decodes an array of WordPress posts from the given data.
/// Decodes an array of WordPress sites from the given data.
///
/// - Parameters:
/// - data: The data to decode.
/// - allowInvalidCharacters: Whether to allow invalid characters in the data.
/// - Returns: An array of WordPress posts, or nil if decoding failed.
/// - Returns: An array of WordPress sites, or nil if decoding failed.
/// - Throws: An error if data couldn't be decoded.
func decodePosts(
func decodeSites(
fromData data: Data,
allowInvalidCharacters: Bool
) throws -> WordPressSite?
Expand Down
Loading

0 comments on commit 8cc80e0

Please sign in to comment.