Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Sources/HTMLKit/Framework/Environment/Condition.swift
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
/// A type representing a conditional that compares an environment value against another value
/// A type representing a conditional that compares an environment value against another value.
@_documentation(visibility: internal)
public struct Condition {

/// An enumeration of potential comparison.
public enum Comparison {

/// Indicates an equal comparison
/// Indicates an equal comparison.
case equal

/// Indicates a not-equal comparison
/// Indicates a not-equal comparison.
case unequal

/// Indicates a greater-than comparison
/// Indicates a greater-than comparison.
case greater

/// Indicates a less-than comparison
/// Indicates a less-than comparison.
case less
}

/// The left-hand side value
/// The left-hand side value.
internal let lhs: EnvironmentValue

/// The right-hand side value to test against
/// The right-hand side value to test against.
internal let rhs: any Comparable

/// The comparison to perfom
/// The comparison to perfom.
internal let comparison: Comparison

/// Initializes a condition
/// Creates a condition.
///
/// - Parameters:
/// - lhs: The origin value
/// - rhs: The value to atest against
/// - operation: The comparison to perfom
/// - lhs: The origin value.
/// - rhs: The value to atest against.
/// - operation: The comparison to perfom.
public init(lhs: EnvironmentValue, rhs: any Comparable, comparison: Comparison) {

self.lhs = lhs
Expand Down
10 changes: 5 additions & 5 deletions Sources/HTMLKit/Framework/Environment/Conditional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
@_documentation(visibility: internal)
public indirect enum Conditional {

/// Holds an optional
/// Holds an optional.
case optional(EnvironmentValue)

/// Holds an relation
/// Holds a relation.
case relation(Relation)

/// Holds a condition
/// Holds a condition.
case condition(Condition)

/// Holds a negation
/// Holds a negation.
case negation(Negation)

/// Holds a value
/// Holds a value.
case value(EnvironmentValue)
}
131 changes: 70 additions & 61 deletions Sources/HTMLKit/Framework/Environment/Environment.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Foundation
import NIOConcurrencyHelpers

/// A class that represents the environment
/// A type that represents the environment.
///
/// The environment provides storage for various settings used by the renderer
/// The environment provides storage for various settings used by the renderer.
@_documentation(visibility: internal)
public final class Environment: @unchecked Sendable {

Expand Down Expand Up @@ -54,67 +54,76 @@ public final class Environment: @unchecked Sendable {
self.lock = .init()
}

/// The current time zone of the environment
/// Creates an environment.
///
/// - Parameter environment: The environment to derive from.
public init(from environment: Environment) {

self.storage = environment.storage
self.lock = .init()
}

/// The current time zone of the environment.
public var timeZone: TimeZone? {

get {
retrieve(for: \EnvironmentKeys.timeZone) as? TimeZone
}
}

/// The current calendar of the environment
/// The current calendar of the environment.
public var calendar: Calendar? {

get {
retrieve(for: \EnvironmentKeys.calendar) as? Calendar
}
}

/// The current locale of the environment
/// The current locale of the environment.
public var locale: Locale? {

get {
retrieve(for: \EnvironmentKeys.locale) as? Locale
}
}

/// The current color scheme of the environment
/// The current color scheme of the environment.
public var colorScheme: String? {

get {
retrieve(for: \EnvironmentKeys.colorScheme) as? String
}
}

/// Retrieves a value from environment for a given key path
/// Retrieves a value from environment for a given key path.
///
/// - Parameter path: The key path used to look up the value
/// - Parameter path: The key path used to look up the value.
///
/// - Returns: The value
/// - Returns: The value.
public func retrieve(for path: AnyKeyPath) -> Any? {

return lock.withLock {
return storage[path]
}
}

/// Inserts or updates a value in the environment for the given key path
/// Inserts or updates a value in the environment for the given key path.
///
/// - Parameters:
/// - value: The value to be stored or updated
/// - path: The key path that identifies where the value is stored
/// - value: The value to be stored or updated.
/// - path: The key path that identifies where the value is stored.
public func upsert<T>(_ value: T, for path: AnyKeyPath) {

lock.withLock {
storage[path] = value
}
}

/// Resolves an environment value
/// Resolves an environment value.
///
/// - Parameter value: The environment value to resolve
/// - Parameter value: The environment value to resolve.
///
/// - Returns: The resolved environment value
/// - Returns: The resolved environment value.
internal func resolve(value: EnvironmentValue) throws -> Any {

guard let parent = retrieve(for: value.parentPath) else {
Expand All @@ -128,11 +137,11 @@ public final class Environment: @unchecked Sendable {
return value
}

/// Evaluates an environment value
/// Evaluates an environment value.
///
/// - Parameter value: The value to evaluate
/// - Parameter value: The value to evaluate.
///
/// - Returns: The result of evaluation
/// - Returns: The result of evaluation.
internal func evaluate(value: EnvironmentValue) throws -> Bool {

guard let boolValue = try resolve(value: value) as? Bool else {
Expand All @@ -142,11 +151,11 @@ public final class Environment: @unchecked Sendable {
return boolValue
}

/// Evaluates an environment relation
/// Evaluates an environment relation.
///
/// - Parameter relation: The relation to evaluate
/// - Parameter relation: The relation to evaluate.
///
/// - Returns: The result of the evaluation
/// - Returns: The result of the evaluation.
internal func evaluate(relation: Relation) throws -> Bool {

switch relation.term {
Expand Down Expand Up @@ -242,11 +251,11 @@ public final class Environment: @unchecked Sendable {
}
}

/// Evaluates an environment condition
/// Evaluates an environment condition.
///
/// - Parameter condition: The condition to evaluate
/// - Parameter condition: The condition to evaluate.
///
/// - Returns: The result of the evaluation
/// - Returns: The result of the evaluation.
internal func evaluate(condition: Condition) throws -> Bool {

let value = try resolve(value: condition.lhs)
Expand All @@ -266,20 +275,20 @@ public final class Environment: @unchecked Sendable {
}
}

/// Evaluates an environment negation
/// Evaluates an environment negation.
///
/// - Parameter negation: The negation to evaluate
/// - Parameter negation: The negation to evaluate.
///
/// - Returns: The result of the evaluation
/// - Returns: The result of the evaluation.
internal func evaluate(negation: Negation) throws -> Bool {
return try !evaluate(value: negation.value)
}

/// Evaluates an environment optional
/// Evaluates an environment optional.
///
/// - Parameter optional: The optional to evaluate
/// - Parameter optional: The optional to evaluate.
///
/// - Returns: The result of the evaluation
/// - Returns: The result of the evaluation.
internal func evaluate(optional: EnvironmentValue) throws -> Bool {

guard let optionalValue = try resolve(value: optional) as? Nullable else {
Expand All @@ -296,82 +305,82 @@ public final class Environment: @unchecked Sendable {

extension Environment {

/// Evaluates one condition
/// Evaluates one condition.
///
/// - Parameters:
/// - condition: The condition to evaluate
/// - content: The content for the true statement
/// - condition: The condition to evaluate.
/// - content: The content for the true statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func check(_ condition: EnvironmentValue, @ContentBuilder<Content> content: () -> [Content]) -> Statement {
return Statement(compound: .value(condition), first: content(), second: [])
}

/// Evaluates one condition
/// Evaluates one condition.
///
/// - Parameters:
/// - condition: The condition to evaluate
/// - content: The content for the true statement
/// - then: The content for the false statement
/// - condition: The condition to evaluate.
/// - content: The content for the true statement.
/// - then: The content for the false statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func check(_ condition: EnvironmentValue, @ContentBuilder<Content> content: () -> [Content], @ContentBuilder<Content> then: () -> [Content]) -> Statement {
return Statement(compound: .value(condition), first: content(), second: then())
}

/// Evaluates one condition
/// Evaluates one condition.
///
/// - Parameters:
/// - condition: The condition to evaluate
/// - content: The content for the true statement
/// - condition: The condition to evaluate.
/// - content: The content for the true statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func check(_ condition: Conditional, @ContentBuilder<Content> content: () -> [Content]) -> Statement {
return Statement(compound: condition, first: content(), second: [])
}

/// Evaluates one condition
/// Evaluates one condition.
///
/// - Parameters:
/// - condition: The condition to evaluate
/// - content: The content for the true statement
/// - then: The content for the false statement
/// - condition: The condition to evaluate.
/// - content: The content for the true statement.
/// - then: The content for the false statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func check(_ condition: Conditional, @ContentBuilder<Content> content: () -> [Content], @ContentBuilder<Content> then: () -> [Content]) -> Statement {
return Statement(compound: condition, first: content(), second: then())
}

/// Unwraps a optional environment value
/// Unwraps an optional environment value.
///
/// - Parameters:
/// - value: The optional value to unwrap
/// - content: The content for some statement
/// - value: The optional value to unwrap.
/// - content: The content for some statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func unwrap(_ value: EnvironmentValue, @ContentBuilder<Content> content: (EnvironmentValue) -> [Content]) -> Statement {
return Statement(compound: .optional(value), first: content(value), second: [])
}

/// Unwraps a optional environment value
/// Unwraps a noptional environment value.
///
/// - Parameters:
/// - value: The optional value to unwrap
/// - content: The content for some statement
/// - then: The content for none statement
/// - value: The optional value to unwrap.
/// - content: The content for some statement.
/// - then: The content for none statement.
///
/// - Returns: A environment statement
/// - Returns: An environment statement.
public static func unwrap(_ value: EnvironmentValue, @ContentBuilder<Content> content: (EnvironmentValue) -> [Content], @ContentBuilder<Content> then: () -> [Content]) -> Statement {
return Statement(compound: .optional(value), first: content(value), second: then())
}

/// Iterates through a sequence of values
/// Iterates through a sequence of values.
///
/// - Parameters:
/// - sequence: The sequence to iterate over
/// - content: The content for the iteration
/// - sequence: The sequence to iterate over.
/// - content: The content for the iteration.
///
/// - Returns: A environment sequence
/// - Returns: An environment sequence.
public static func loop(_ sequence: EnvironmentValue, @ContentBuilder<Content> content: (EnvironmentValue) -> [Content]) -> Sequence {
return Sequence(value: sequence, content: content(sequence))
}
Expand Down
10 changes: 5 additions & 5 deletions Sources/HTMLKit/Framework/Environment/EnvironmentKeys.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import Foundation

/// A set of predefined environment keys
/// A set of predefined environment keys.
///
/// The keys are used on the environment modifiers to configure various settings for the environment.
public struct EnvironmentKeys: Hashable {

/// A key used to configure the environment's calendar
/// A key used to configure the environment's calendar.
public var calendar: Calendar

/// A key used to configure the environment's time zone
/// A key used to configure the environment's time zone.
public var timeZone: TimeZone

/// A key used to configure the environment's locale
/// A key used to configure the environment's locale.
public var locale: Locale

/// A key used to configure the environment's color scheme
/// A key used to configure the environment's color scheme.
public var colorScheme: String
}
Loading
Loading