Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryu0118 committed Nov 10, 2023
1 parent 5709f20 commit a3d7476
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 523 deletions.
7 changes: 7 additions & 0 deletions Sources/TypedDate/Date+scope.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,10 @@ extension TypedDate {
self.components = (Year(year), Month(month), Day(day), Hour(hour), Minute(minute), Second(second), Nanosecond(nanosecond))
}
}

//struct Hoge {
// init() {
// let hoge = TypedDate(Year(2022), Month(1))
// hoge.erase(to: <#T##KeyPath<TypedDate<(Year, Month)>._MonthEraseContext, (TypedDate<Sendable>.Type, Sendable)>#>)
// }
//}
60 changes: 54 additions & 6 deletions Sources/TypedDate/TypedDate+erase.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,67 @@
import Foundation

@EraseContext
public extension TypedDate<(Year, Month)> {}
public extension TypedDate<(Year, Month)> {
func erase<T>(
to keyPath: KeyPath<_MonthEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _MonthEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}

@EraseContext
public extension TypedDate<(Year, Month, Day)> {}
public extension TypedDate<(Year, Month, Day)> {
func erase<T>(
to keyPath: KeyPath<_DayEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _DayEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}

@EraseContext
public extension TypedDate<(Year, Month, Day, Hour)> {}
public extension TypedDate<(Year, Month, Day, Hour)> {
func erase<T>(
to keyPath: KeyPath<_HourEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _HourEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}

@EraseContext
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {
func erase<T>(
to keyPath: KeyPath<_MinuteEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _MinuteEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}

@EraseContext
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {
func erase<T>(
to keyPath: KeyPath<_SecondEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _SecondEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}

@EraseContext
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> {
func erase<T>(
to keyPath: KeyPath<_NanosecondEraseContext, (TypedDate<T>.Type, T)>,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _NanosecondEraseContext(base: components) [keyPath: keyPath]
return context.0.init(context.1, calendar: calendar)
}
}
72 changes: 66 additions & 6 deletions Sources/TypedDate/TypedDate+fillIn.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,79 @@
import Foundation

@FillInContext
public extension TypedDate<Year> {}
public extension TypedDate<Year> {
func fill<T, U>(
to keyPath: KeyPath<_YearFillInContext,(U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _YearFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}

@FillInContext
public extension TypedDate<(Year, Month)> {}
public extension TypedDate<(Year, Month)> {
func fill<T, U>(
to keyPath: KeyPath<_MonthFillInContext,(U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _MonthFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}

@FillInContext
public extension TypedDate<(Year, Month, Day)> {}
public extension TypedDate<(Year, Month, Day)> {
func fill<T, U>(
to keyPath: KeyPath<_DayFillInContext, (U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _DayFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}

@FillInContext
public extension TypedDate<(Year, Month, Day, Hour)> {}
public extension TypedDate<(Year, Month, Day, Hour)> {
func fill<T, U>(
to keyPath: KeyPath<_HourFillInContext, (U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _HourFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}

@FillInContext
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {
func fill<T, U>(
to keyPath: KeyPath<_MinuteFillInContext, (U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _MinuteFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}

@FillInContext
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {
func fill<T, U>(
to keyPath: KeyPath<_SecondFillInContext, (U) -> T>,
arguments: U,
calendar: Calendar = .current
) -> TypedDate<T> {
let context = _SecondFillInContext(base: components)
let transform = context[keyPath: keyPath]
return .init(transform(arguments), calendar: calendar)
}
}
3 changes: 0 additions & 3 deletions Sources/TypedDate/TypedDate+init.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,6 @@ public extension TypedDate {
}

extension TypedDate {
init(components: Components, calendar: Calendar = .current) {
self.init(components, calendar: calendar)
}
init(_ components: Components, calendar: Calendar = .current) {
self.components = components

Expand Down
173 changes: 166 additions & 7 deletions Sources/TypedDate/TypedDate+modify.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,181 @@
import Foundation

/// Extension for `TypedDate<Year>`. Allows for modifying the year component of a date.
@ModifyContext
public extension TypedDate<Year> {}
public extension TypedDate<Year> {
/// Modifies the year component of `TypedDate<Year>` with a provided modification closure.
/// - Parameters:
/// - keyPath: KeyPath to the year modify context.
/// - modify: Closure that takes an inout parameter of type T for modification.
/// - calendar: Calendar used for date calculations, defaults to the current calendar.
/// - Returns: A new `TypedDate<Components>` instance with modified year component.
func modifying<T>(
_ keyPath: KeyPath<_YearModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _YearModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year component of `TypedDate<Year>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_YearModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year' and 'Month'.
@ModifyContext
public extension TypedDate<(Year, Month)> {}
public extension TypedDate<(Year, Month)> {
/// Modifies the year and month components of `TypedDate<Year, Month>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_MonthModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _MonthModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year and month components of `TypedDate<Year, Month>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_MonthModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year', 'Month', and 'Day'.
@ModifyContext
public extension TypedDate<(Year, Month, Day)> {}
public extension TypedDate<(Year, Month, Day)> {
/// Modifies the year, month, and day components of `TypedDate<Year, Month, Day>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_DayModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _DayModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year, month, and day components of `TypedDate<Year, Month, Day>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_DayModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', and 'Hour'.
@ModifyContext
public extension TypedDate<(Year, Month, Day, Hour)> {}
public extension TypedDate<(Year, Month, Day, Hour)> {
/// Modifies the year, month, day, and hour components of `TypedDate<Year, Month, Day, Hour>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_HourModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _HourModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year, month, day, and hour components of `TypedDate<Year, Month, Day, Hour>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_HourModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', and 'Minute'.
@ModifyContext
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute)> {
/// Modifies the year, month, day, hour, and minute components of `TypedDate<Year, Month, Day, Hour, Minute>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_MinuteModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _MinuteModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year, month, day, hour, and minute components of `TypedDate<Year, Month, Day, Hour, Minute>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_MinuteModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', 'Minute', and 'Second'.
@ModifyContext
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second)> {
/// Modifies the year, month, day, hour, minute, and second components of `TypedDate<Year, Month, Day, Hour, Minute, Second>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_SecondModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _SecondModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year, month, day, hour, minute, and second components of `TypedDate<Year, Month, Day, Hour, Minute, Second>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_SecondModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}

/// Extension for `TypedDate` combining 'Year', 'Month', 'Day', 'Hour', 'Minute', 'Second', and 'Nanosecond'.
@ModifyContext
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> {}
public extension TypedDate<(Year, Month, Day, Hour, Minute, Second, Nanosecond)> {
/// Modifies the year, month, day, hour, minute, second, and nanosecond components of `TypedDate<Year, Month, Day, Hour, Minute, Second, Nanosecond>` with a provided modification closure.
func modifying<T>(
_ keyPath: KeyPath<_NanosecondModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) -> TypedDate<Components> {
let context = _NanosecondModifyContext(base: components)
var (target, transform) = context[keyPath: keyPath]
modify(&target)
return .init(transform(target), calendar: calendar)
}

/// Mutates the year, month, day, hour, minute, second, and nanosecond components of `TypedDate<Year, Month, Day, Hour, Minute, Second, Nanosecond>` instance.
mutating func modify<T>(
_ keyPath: KeyPath<_NanosecondModifyContext, (T, (T) -> Components)>,
calendar: Calendar = .current,
modify: (inout T) -> Void
) {
self = modifying(keyPath, calendar: calendar, modify: modify)
}
}
Loading

0 comments on commit a3d7476

Please sign in to comment.