Skip to content

Commit

Permalink
Fixing linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Jan 25, 2024
1 parent ec811bf commit 4ef9c94
Show file tree
Hide file tree
Showing 44 changed files with 686 additions and 80 deletions.
6 changes: 6 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ function_parameter_count: 8
line_length:
- 90
- 90
type_name:
excluded:
- iTunesDuration
- iTunesEpisode
- iTunesOwner
- iTunesImage
identifier_name:
excluded:
- id
Expand Down
1 change: 1 addition & 0 deletions Sources/SyndiKit/Common/EntryCategory.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Abstract category type.
public protocol EntryCategory {
/// Term used for the category
var term: String { get }
}
12 changes: 12 additions & 0 deletions Sources/SyndiKit/Formats/Blogs/CategoryDescriptor.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
/**
A descriptor for a category.
- Note: This struct is publicly accessible.
- Important: The `title` and `description` properties are read-only.
- SeeAlso: `Category`
*/
public struct CategoryDescriptor {
/// The title of the category.
public let title: String

/// The description of the category.
public let description: String
}
25 changes: 25 additions & 0 deletions Sources/SyndiKit/Formats/Blogs/CategoryLanguage.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
/**
A struct representing a category in a specific language.
- Parameters:
- languageCategory: The category in a specific language.
- language: The language of the category.
- Note: This struct is used internally.
- SeeAlso: `SiteCategoryType`
- SeeAlso: `CategoryDescriptor`
- SeeAlso: `SiteLanguageType`
*/
public struct CategoryLanguage {
/// The type of the category.
public let type: SiteCategoryType

/// The descriptor of the category.
public let descriptor: CategoryDescriptor

/// The language of the category.
public let language: SiteLanguageType

/**
Initializes a `CategoryLanguage` instance.
- Parameters:
- languageCategory: The category in a specific language.
- language: The language of the category.
*/
internal init(languageCategory: SiteLanguageCategory, language: SiteLanguageType) {
type = languageCategory.slug
descriptor = CategoryDescriptor(
Expand Down
20 changes: 20 additions & 0 deletions Sources/SyndiKit/Formats/Blogs/Site.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
import Foundation

/// A struct representing a website.
public struct Site {
/// The title of the website.
public let title: String

/// The author of the website.
public let author: String

/// The URL of the website.
public let siteURL: URL

/// The URL of the website's feed.
public let feedURL: URL

/// The URL of the website's Twitter page, if available.
public let twitterURL: URL?

/// The category of the website.
public let category: SiteCategoryType

/// The language of the website.
public let language: SiteLanguageType

/// Initializes a new `Site` instance.
///
/// - Parameters:
/// - site: The `SiteLanguageCategory.Site` instance to use as a base.
/// - categoryType: The category type of the website.
/// - languageType: The language type of the website.
internal init(
site: SiteLanguageCategory.Site,
categoryType: SiteCategoryType,
Expand Down
28 changes: 28 additions & 0 deletions Sources/SyndiKit/Formats/Blogs/SiteCategory.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
/**
A struct representing a site category.
- Note: This struct is used to categorize sites based on their type and descriptors.
- Parameters:
- type: The type of the site category.
- descriptors: A dictionary mapping site language types to category descriptors.
- Important: This struct should not be used directly.
Instead, use the `SiteCategoryBuilder` to create instances of `SiteCategory`.
- SeeAlso: `SiteCategoryType`
- SeeAlso: `CategoryDescriptor`
- SeeAlso: `CategoryLanguage`
- SeeAlso: `SiteCategoryBuilder`
*/
public struct SiteCategory {
/// The type of the site category.
public let type: SiteCategoryType

/// A dictionary mapping site language types to category descriptors.
public let descriptors: [SiteLanguageType: CategoryDescriptor]

/**
Initializes a `SiteCategory` instance with the given languages.
- Parameter languages: An array of `CategoryLanguage` instances.
- Returns: A new `SiteCategory` instance
if at least one language is provided, `nil` otherwise.
*/
internal init?(languages: [CategoryLanguage]) {
guard let type = languages.first?.type else {
return nil
Expand Down
1 change: 1 addition & 0 deletions Sources/SyndiKit/Formats/Blogs/SiteCategoryType.swift
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// A type alias representing a site category.
public typealias SiteCategoryType = String
1 change: 1 addition & 0 deletions Sources/SyndiKit/Formats/Blogs/SiteCollection.swift
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/// A collection of site language content.
public typealias SiteCollection = [SiteLanguageContent]
112 changes: 69 additions & 43 deletions Sources/SyndiKit/Formats/Blogs/SiteDirectory.swift
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
import Foundation

/// A directory of site collections.
public struct SiteCollectionDirectory: SiteDirectory {
/// A sequence of sites.
public typealias SiteSequence = [Site]

public typealias LanguageSequence =
Dictionary<SiteLanguageType, SiteLanguage>.Values
/// A sequence of languages.
public typealias LanguageSequence = Dictionary<SiteLanguageType, SiteLanguage>.Values

public typealias CategorySequence =
Dictionary<SiteCategoryType, SiteCategory>.Values
/// A sequence of categories.
public typealias CategorySequence = Dictionary<SiteCategoryType, SiteCategory>.Values

/// The internal structure of the site collection directory.
internal struct Instance {
internal let allSites: [Site]
internal let languageDictionary: [SiteLanguageType: SiteLanguage]
internal let categoryDictionary: [SiteCategoryType: SiteCategory]
internal let languageIndicies: [SiteLanguageType: Set<Int>]
internal let categoryIndicies: [SiteCategoryType: Set<Int>]
internal let languageIndices: [SiteLanguageType: Set<Int>]
internal let categoryIndices: [SiteCategoryType: Set<Int>]

// swiftlint:disable:next function_body_length
internal func sites(
withLanguage language: SiteLanguageType?,
withCategory category: SiteCategoryType?
) -> [Site] {
let languageIndicies: Set<Int>?
let languageIndices: Set<Int>?
if let language = language {
languageIndicies = self.languageIndicies[language] ?? .init()
languageIndices = self.languageIndices[language] ?? .init()
} else {
languageIndicies = nil
languageIndices = nil
}

let categoryIndicies: Set<Int>?
let categoryIndices: Set<Int>?
if let category = category {
categoryIndicies = self.categoryIndicies[category] ?? .init()
categoryIndices = self.categoryIndices[category] ?? .init()
} else {
categoryIndicies = nil
categoryIndices = nil
}

var indicies: Set<Int>?
var indices: Set<Int>?

if let languageIndicies = languageIndicies {
indicies = languageIndicies
if let languageIndices = languageIndices {
indices = languageIndices
}

if let categoryIndicies = categoryIndicies {
if let current = indicies {
indicies = current.intersection(categoryIndicies)
if let categoryIndices = categoryIndices {
if let current = indices {
indices = current.intersection(categoryIndices)
} else {
indicies = categoryIndicies
indices = categoryIndices
}
}

if let current = indicies {
if let current = indices {
return current.map { self.allSites[$0] }
} else {
return allSites
Expand All @@ -61,14 +64,14 @@ public struct SiteCollectionDirectory: SiteDirectory {
var categories = [CategoryLanguage]()
var languages = [SiteLanguage]()
var sites = [Site]()
var languageIndicies = [SiteLanguageType: Set<Int>]()
var categoryIndicies = [SiteCategoryType: Set<Int>]()
var languageIndices = [SiteLanguageType: Set<Int>]()
var categoryIndices = [SiteCategoryType: Set<Int>]()

for languageContent in blogs {
let language = SiteLanguage(content: languageContent)
var thisLanguageIndicies = [Int]()
var thisLanguageIndices = [Int]()
for languageCategory in languageContent.categories {
var thisCategoryIndicies = [Int]()
var thisCategoryIndices = [Int]()
let category = CategoryLanguage(
languageCategory: languageCategory,
language: language.type
Expand All @@ -81,13 +84,13 @@ public struct SiteCollectionDirectory: SiteDirectory {
languageType: language.type
)
sites.append(site)
thisCategoryIndicies.append(index)
thisLanguageIndicies.append(index)
thisCategoryIndices.append(index)
thisLanguageIndices.append(index)
}
categoryIndicies.formUnion(thisCategoryIndicies, key: category.type)
categoryIndices.formUnion(thisCategoryIndices, key: category.type)
categories.append(category)
}
languageIndicies.formUnion(thisLanguageIndicies, key: language.type)
languageIndices.formUnion(thisLanguageIndices, key: language.type)
languages.append(language)
}

Expand All @@ -96,30 +99,37 @@ public struct SiteCollectionDirectory: SiteDirectory {
languageDictionary = Dictionary(
uniqueKeysWithValues: languages.map { ($0.type, $0) }
)
self.languageIndicies = languageIndicies
self.categoryIndicies = categoryIndicies
self.languageIndices = languageIndices
self.categoryIndices = categoryIndices
allSites = sites
}
}

private let instance: Instance

public var languages: Dictionary<
SiteLanguageType, SiteLanguage
>.Values {
/// A sequence of languages in the site collection directory.
public var languages: Dictionary<SiteLanguageType, SiteLanguage>.Values {
instance.languageDictionary.values
}

public var categories: Dictionary<
SiteCategoryType, SiteCategory
>.Values {
/// A sequence of categories in the site collection directory.
public var categories: Dictionary<SiteCategoryType, SiteCategory>.Values {
instance.categoryDictionary.values
}

/// Initializes a new instance of the `SiteCollectionDirectory` struct.
///
/// - Parameter blogs: The site collection to use.
internal init(blogs: SiteCollection) {
instance = .init(blogs: blogs)
}

/// Retrieves a list of sites based on the specified language and category.
///
/// - Parameters:
/// - language: The language of the sites to retrieve.
/// - category: The category of the sites to retrieve.
/// - Returns: A list of sites matching the specified language and category.
public func sites(
withLanguage language: SiteLanguageType?,
withCategory category: SiteCategoryType?
Expand All @@ -128,24 +138,40 @@ public struct SiteCollectionDirectory: SiteDirectory {
}
}

/// A protocol for site directories.
public protocol SiteDirectory {
associatedtype SiteSequence: Sequence
where SiteSequence.Element == Site
associatedtype LanguageSequence: Sequence
where LanguageSequence.Element == SiteLanguage
associatedtype CategorySequence: Sequence
where CategorySequence.Element == SiteCategory

/// List of Sites
associatedtype SiteSequence: Sequence where SiteSequence.Element == Site
/// List of Languages
associatedtype LanguageSequence: Sequence where LanguageSequence.Element == SiteLanguage
/// List of Categories
associatedtype CategorySequence: Sequence where CategorySequence.Element == SiteCategory

/// A sequence of languages in the site directory.
var languages: LanguageSequence { get }

/// A sequence of categories in the site directory.
var categories: CategorySequence { get }

/// Retrieves a list of sites based on the specified language and category.
///
/// - Parameters:
/// - language: The language of the sites to retrieve.
/// - category: The category of the sites to retrieve.
/// - Returns: A list of sites matching the specified language and category.
func sites(
withLanguage language: SiteLanguageType?,
withCategory category: SiteCategoryType?
) -> SiteSequence
}

extension SiteDirectory {
/// Retrieves a list of sites based on the specified language and category.
///
/// - Parameters:
/// - language: The language of the sites to retrieve.
/// - category: The category of the sites to retrieve.
/// - Returns: A list of sites matching the specified language and category.
public func sites(
withLanguage language: SiteLanguageType? = nil,
withCategory category: SiteCategoryType? = nil
Expand Down
20 changes: 20 additions & 0 deletions Sources/SyndiKit/Formats/Blogs/SiteDirectoryBuilder.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,33 @@
import Foundation

/// A builder for creating a site collection directory.
public struct SiteCollectionDirectoryBuilder: SiteDirectoryBuilder {
/// Initializes a new instance of `SiteCollectionDirectoryBuilder`.
public init() {}

/**
Creates a site collection directory from a site collection.
- Parameter blogs: The site collection to build the directory from.
- Returns: A new instance of `SiteCollectionDirectory`.
*/
public func directory(fromCollection blogs: SiteCollection) -> SiteCollectionDirectory {
SiteCollectionDirectory(blogs: blogs)
}
}

/// A protocol for building site directories.
public protocol SiteDirectoryBuilder {
/// The type of site directory to build.
associatedtype SiteDirectoryType: SiteDirectory

/**
Creates a site directory from a site collection.
- Parameter blogs: The site collection to build the directory from.
- Returns: A new instance of `SiteDirectoryType`.
*/
func directory(fromCollection blogs: SiteCollection) -> SiteDirectoryType
}
Loading

0 comments on commit 4ef9c94

Please sign in to comment.