Skip to content

Commit 44152c2

Browse files
committed
Add round-trip conversion initializers for RFC email address types
- Add RFC_5321.EmailAddress.init(_ emailAddress: EmailAddress) - Add RFC_5322.EmailAddress.init(_ emailAddress: EmailAddress) - Add RFC_6531.EmailAddress.init(_ emailAddress: EmailAddress) These initializers enable cleaner conversion syntax: let rfc5322 = try RFC_5322.EmailAddress(emailAddress) Instead of: let rfc5322 = try RFC_5322.EmailAddress(emailAddress.rawValue) Placement in swift-emailaddress-type avoids creating reverse dependencies in RFC packages while providing convenient conversion APIs.
1 parent 682808a commit 44152c2

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

Sources/EmailAddress/EmailAddress+RFC5321.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,18 @@ extension EmailAddress {
2727
self.displayName = rfc5321.displayName
2828
}
2929
}
30+
31+
extension RFC_5321.EmailAddress {
32+
/// Initialize from EmailAddress
33+
///
34+
/// Enables round-trip conversion between EmailAddress and RFC_5321.EmailAddress.
35+
///
36+
/// - Parameter emailAddress: The EmailAddress to convert
37+
/// - Throws: If the EmailAddress doesn't have a valid RFC 5321 representation
38+
public init(_ emailAddress: EmailAddress) throws {
39+
guard let rfc5321 = emailAddress.rfc5321 else {
40+
throw EmailAddress.Error.conversionFailure
41+
}
42+
self = rfc5321
43+
}
44+
}

Sources/EmailAddress/EmailAddress+RFC5322.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,19 @@ extension EmailAddress {
2323
self.displayName = rfc5322.displayName
2424
}
2525
}
26+
27+
extension RFC_5322.EmailAddress {
28+
/// Initialize from EmailAddress
29+
///
30+
/// Enables round-trip conversion between EmailAddress and RFC_5322.EmailAddress.
31+
/// This is particularly useful when converting Email objects to RFC 5322 Message format.
32+
///
33+
/// - Parameter emailAddress: The EmailAddress to convert
34+
/// - Throws: If the EmailAddress doesn't have a valid RFC 5322 representation
35+
public init(_ emailAddress: EmailAddress) throws {
36+
guard let rfc5322 = emailAddress.rfc5322 else {
37+
throw EmailAddress.Error.conversionFailure
38+
}
39+
self = rfc5322
40+
}
41+
}

Sources/EmailAddress/EmailAddress+RFC6531.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ extension EmailAddress {
1212
self.displayName = rfc6531.displayName
1313
}
1414
}
15+
16+
extension RFC_6531.EmailAddress {
17+
/// Initialize from EmailAddress
18+
///
19+
/// Enables round-trip conversion between EmailAddress and RFC_6531.EmailAddress.
20+
/// RFC 6531 is the most permissive format and supports internationalized email addresses.
21+
///
22+
/// - Parameter emailAddress: The EmailAddress to convert
23+
public init(_ emailAddress: EmailAddress) {
24+
self = emailAddress.rfc6531
25+
}
26+
}

0 commit comments

Comments
 (0)