Skip to content

Commit e991184

Browse files
committed
Add round-trip conversions and replace toRFC*() with init pattern
Cross-RFC Conversion Files (now with bidirectional inits): - RFC_5321+RFC_5322.swift: RFC_5321 ↔ RFC_5322 - RFC_5321+RFC_6531.swift: RFC_5321 ↔ RFC_6531 - RFC_5322+RFC_6531.swift: RFC_5322 ↔ RFC_6531 EmailAddress Conversions: - Replaced .toRFC5321() with RFC_5321.EmailAddress(...) - Replaced .toRFC5322() with RFC_5322.EmailAddress(...) - Updated EmailAddress+RFC5322.swift and EmailAddress+RFC6531.swift Benefits: - Cleaner init pattern: RFC_5321.EmailAddress(rfc5322) - Round-trip conversions available for all RFC pairs - Avoids circular dependencies between RFC packages
1 parent 44152c2 commit e991184

File tree

5 files changed

+115
-3
lines changed

5 files changed

+115
-3
lines changed

Sources/EmailAddress/EmailAddress+RFC5322.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import RFC_6531
66
extension EmailAddress {
77
/// Initialize from RFC5322
88
public init(rfc5322: RFC_5322.EmailAddress) throws {
9-
self.rfc5321 = try? rfc5322.toRFC5321()
9+
self.rfc5321 = try? RFC_5321.EmailAddress(rfc5322)
1010
self.rfc5322 = rfc5322
1111
self.rfc6531 = try {
1212
guard

Sources/EmailAddress/EmailAddress+RFC6531.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import RFC_6531
66
extension EmailAddress {
77
/// Initialize from RFC6531
88
public init(rfc6531: RFC_6531.EmailAddress) {
9-
self.rfc5321 = try? rfc6531.toRFC5321()
10-
self.rfc5322 = try? rfc6531.toRFC5322()
9+
self.rfc5321 = try? RFC_5321.EmailAddress(rfc6531)
10+
self.rfc5322 = try? RFC_5322.EmailAddress(rfc6531)
1111
self.rfc6531 = rfc6531
1212
self.displayName = rfc6531.displayName
1313
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
import RFC_5321
3+
import RFC_5322
4+
5+
extension RFC_5321.EmailAddress {
6+
/// Initialize from RFC 5322 email address
7+
///
8+
/// Converts an RFC 5322 (Internet Message Format) email address to RFC 5321 (SMTP) format.
9+
/// RFC 5321 has more restrictive rules than RFC 5322.
10+
///
11+
/// - Parameter rfc5322: The RFC 5322 email address to convert
12+
/// - Throws: If the address contains characters or patterns not allowed in RFC 5321
13+
public init(_ rfc5322: RFC_5322.EmailAddress) throws {
14+
try self.init(
15+
displayName: rfc5322.displayName,
16+
localPart: .init(rfc5322.localPart.stringValue),
17+
domain: rfc5322.domain
18+
)
19+
}
20+
}
21+
22+
extension RFC_5322.EmailAddress {
23+
/// Initialize from RFC 5321 email address
24+
///
25+
/// Converts an RFC 5321 (SMTP) email address to RFC 5322 (Internet Message Format).
26+
/// RFC 5322 allows a superset of RFC 5321, so this conversion always succeeds.
27+
///
28+
/// - Parameter rfc5321: The RFC 5321 email address to convert
29+
/// - Throws: If the local part cannot be represented in RFC 5322 format
30+
public init(_ rfc5321: RFC_5321.EmailAddress) throws {
31+
try self.init(
32+
displayName: rfc5321.displayName,
33+
localPart: .init(rfc5321.localPart.stringValue),
34+
domain: rfc5321.domain
35+
)
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Foundation
2+
import RFC_1123
3+
import RFC_5321
4+
import RFC_6531
5+
6+
extension RFC_5321.EmailAddress {
7+
/// Initialize from RFC 6531 email address
8+
///
9+
/// Converts an RFC 6531 (SMTPUTF8) internationalized email address to RFC 5321 (SMTP) format.
10+
/// Only works for addresses that use ASCII characters only.
11+
///
12+
/// - Parameter rfc6531: The RFC 6531 email address to convert
13+
/// - Throws: If the address contains non-ASCII characters or patterns not allowed in RFC 5321
14+
public init(_ rfc6531: RFC_6531.EmailAddress) throws {
15+
try self.init(
16+
displayName: rfc6531.displayName,
17+
localPart: .init(rfc6531.localPart.stringValue),
18+
domain: .init(rfc6531.domain.name)
19+
)
20+
}
21+
}
22+
23+
extension RFC_6531.EmailAddress {
24+
/// Initialize from RFC 5321 email address
25+
///
26+
/// Converts an RFC 5321 (SMTP) email address to RFC 6531 (SMTPUTF8) format.
27+
/// RFC 6531 is a superset of RFC 5321, so this conversion always succeeds.
28+
///
29+
/// - Parameter rfc5321: The RFC 5321 email address to convert
30+
/// - Throws: If the conversion fails
31+
public init(_ rfc5321: RFC_5321.EmailAddress) throws {
32+
try self.init(
33+
displayName: rfc5321.displayName,
34+
localPart: .init(rfc5321.localPart.stringValue),
35+
domain: rfc5321.domain
36+
)
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Foundation
2+
import RFC_5322
3+
import RFC_6531
4+
5+
extension RFC_5322.EmailAddress {
6+
/// Initialize from RFC 6531 email address
7+
///
8+
/// Converts an RFC 6531 (SMTPUTF8) internationalized email address to RFC 5322 (Internet Message Format).
9+
/// Only works for addresses that use ASCII characters only.
10+
///
11+
/// - Parameter rfc6531: The RFC 6531 email address to convert
12+
/// - Throws: If the address contains non-ASCII characters or patterns not allowed in RFC 5322
13+
public init(_ rfc6531: RFC_6531.EmailAddress) throws {
14+
try self.init(
15+
displayName: rfc6531.displayName,
16+
localPart: .init(rfc6531.localPart.stringValue),
17+
domain: rfc6531.domain
18+
)
19+
}
20+
}
21+
22+
extension RFC_6531.EmailAddress {
23+
/// Initialize from RFC 5322 email address
24+
///
25+
/// Converts an RFC 5322 (Internet Message Format) email address to RFC 6531 (SMTPUTF8) format.
26+
/// RFC 6531 is a superset of RFC 5322, so this conversion always succeeds.
27+
///
28+
/// - Parameter rfc5322: The RFC 5322 email address to convert
29+
/// - Throws: If the conversion fails
30+
public init(_ rfc5322: RFC_5322.EmailAddress) throws {
31+
try self.init(
32+
displayName: rfc5322.displayName,
33+
localPart: .init(rfc5322.localPart.stringValue),
34+
domain: rfc5322.domain
35+
)
36+
}
37+
}

0 commit comments

Comments
 (0)