Skip to content

Commit

Permalink
feat: create restriction macro
Browse files Browse the repository at this point in the history
  • Loading branch information
elihwyma committed Oct 31, 2024
1 parent 2b9cb5b commit 1d1c65e
Show file tree
Hide file tree
Showing 11 changed files with 368 additions and 255 deletions.
6 changes: 6 additions & 0 deletions Sources/PugiSwift/Decoding/MacroDefinitions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ public macro Node(codingKey: String? = nil) =
#externalMacro(module: "PugiSwiftMacros",
type: "NodeMacro")

@attached(extension,
names: arbitrary)
public macro Restriction(codingKey: String? = nil) =
#externalMacro(module: "PugiSwiftMacros",
type: "RestrictionMacro")

24 changes: 14 additions & 10 deletions Sources/PugiSwift/Decoding/Restrictions/FloatingResrictions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,45 @@

import Foundation

public protocol DoubleRestrictions: RestrictionsProtocol {
public protocol FloatingRestrictions: RestrictionsProtocol {

associatedtype T: FloatingPoint

static var fractionDigits: Int { get }

static var maxExclusive: Int { get }
static var maxExclusive: T { get }

static var maxInclusive: Int { get }
static var maxInclusive: T { get }

static var minExclusive: Int { get }
static var minExclusive: T { get }

static var minInclusive: Int { get }
static var minInclusive: T { get }

static var totalDigits: Int { get }

var rawValue: T { get }

}

extension DoubleRestrictions {
extension FloatingRestrictions {

public static var fractionDigits: Int {
0
}

public static var maxExclusive: Int {
public static var maxExclusive: T {
0
}

public static var maxInclusive: Int {
public static var maxInclusive: T {
0
}

public static var minExclusive: Int {
public static var minExclusive: T {
0
}

public static var minInclusive: Int {
public static var minInclusive: T {
0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public protocol ListRestrictions: RestrictionsProtocol {

static var minLength: Int { get }

static var rawValue: any Collection { get }

}

extension ListRestrictions {
Expand Down
20 changes: 12 additions & 8 deletions Sources/PugiSwift/Decoding/Restrictions/NumericRestrictions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,37 @@ import Foundation

public protocol NumericRestrictions: RestrictionsProtocol {

static var maxExclusive: Int { get }
associatedtype T: Numeric

static var maxInclusive: Int { get }
static var maxExclusive: T { get }

static var minExclusive: Int { get }
static var maxInclusive: T { get }

static var minInclusive: Int { get }
static var minExclusive: T { get }

static var minInclusive: T { get }

static var totalDigits: Int { get }

var rawValue: T { get }

}

extension NumericRestrictions {

public static var maxExclusive: Int {
public static var maxExclusive: T {
0
}

public static var maxInclusive: Int {
public static var maxInclusive: T {
0
}

public static var minExclusive: Int {
public static var minExclusive: T {
0
}

public static var minInclusive: Int {
public static var minInclusive: T {
0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public protocol StringRestrictions: RestrictionsProtocol {

static var minLength: Int { get }

static var rawValue: String { get }

}

extension StringRestrictions {
Expand Down
8 changes: 8 additions & 0 deletions Sources/PugiSwiftDemo/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ import PugiSwift
@Element(childrenCodingKey: "record") let records: [Record]
}

@Restriction struct ExampleSimpleType: NumericRestrictions {

var rawValue: Int {
5
}

}

@Node struct Record {
let name: String
let list: Int
Expand Down
20 changes: 20 additions & 0 deletions Sources/PugiSwiftMacros/AttributeMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// ElementMacro 2.swift
// PugiSwift
//
// Created by Amy on 31/10/2024.
//


import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct AttributeMacro: PeerMacro {

public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
[]
}

}
19 changes: 19 additions & 0 deletions Sources/PugiSwiftMacros/ElementMacro.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// ElementMacro.swift
// PugiSwift
//
// Created by Amy on 31/10/2024.
//

import SwiftCompilerPlugin
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros

public struct ElementMacro: PeerMacro {

public static func expansion(of node: SwiftSyntax.AttributeSyntax, providingPeersOf declaration: some SwiftSyntax.DeclSyntaxProtocol, in context: some SwiftSyntaxMacros.MacroExpansionContext) throws -> [SwiftSyntax.DeclSyntax] {
[]
}

}
Loading

0 comments on commit 1d1c65e

Please sign in to comment.