summaryrefslogtreecommitdiffstats
path: root/openpgp
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2023-10-10 18:57:30 +0200
committerJustus Winter <justus@sequoia-pgp.org>2023-10-11 10:20:31 +0200
commitc1a3ab926e4bd4a73b1becdf08b8f4ecb26fc628 (patch)
tree3219b2c5f8df7203ab87bfb91633013be218659c /openpgp
parent3ec528dd304f65b7d7c0e537920c5b93a4d6b9c0 (diff)
openpgp: Add non-allocating accessors for parsed User ID components.
- Now that we use OnceCell for the cache, we can hand out references to the cached data. This closes the gap between UserID and ConventionallyParsedUserID, hence I think this addresses the concern in #377. - Deprecate the allocating variants. - Fixes #377.
Diffstat (limited to 'openpgp')
-rw-r--r--openpgp/NEWS8
-rw-r--r--openpgp/src/packet/userid.rs43
2 files changed, 47 insertions, 4 deletions
diff --git a/openpgp/NEWS b/openpgp/NEWS
index 926380eb..ceb840f0 100644
--- a/openpgp/NEWS
+++ b/openpgp/NEWS
@@ -44,6 +44,10 @@
- crypto::ecdh::decrypt_unwrap2
- packet::Key::generate_dsa
- packet::Key::generate_elgamal
+ - packet::UserID::comment2
+ - packet::UserID::email2
+ - packet::UserID::name2
+ - packet::UserID::uri2
- parse::PacketParser::start_hashing
- parse::PacketParserBuilder::automatic_hashing
- impl Eq, PartialEq for regex::Regex
@@ -63,6 +67,10 @@
** Deprecated functionality
- cert::Preferences::preferred_aead_algorithms
- crypto::ecdh::decrypt_unwrap
+ - packet::UserID::comment
+ - packet::UserID::email
+ - packet::UserID::name
+ - packet::UserID::uri
- packet::signature::SignatureBuilder::set_preferred_aead_algorithms
- packet::signature::subpacket::SubpacketAreas::preferred_aead_algorithms
- packet::signature::subpacket::SubpacketTag::PreferredAEADAlgorithms
diff --git a/openpgp/src/packet/userid.rs b/openpgp/src/packet/userid.rs
index 1b76a660..1ad272f0 100644
--- a/openpgp/src/packet/userid.rs
+++ b/openpgp/src/packet/userid.rs
@@ -872,6 +872,15 @@ impl UserID {
/// See [conventional User ID] for more information.
///
/// [conventional User ID]: #conventional-user-ids
+ pub fn name2(&self) -> Result<Option<&str>> {
+ Ok(self.do_parse()?.name())
+ }
+
+ /// Parses the User ID according to de facto conventions, and
+ /// returns the name component, if any.
+ ///
+ /// Like [`UserID::name2`], but heap-allocates.
+ #[deprecated(note = "Use UserID::name2")]
pub fn name(&self) -> Result<Option<String>> {
Ok(self.do_parse()?.name().map(|s| s.to_string()))
}
@@ -882,6 +891,15 @@ impl UserID {
/// See [conventional User ID] for more information.
///
/// [conventional User ID]: #conventional-user-ids
+ pub fn comment2(&self) -> Result<Option<&str>> {
+ Ok(self.do_parse()?.comment())
+ }
+
+ /// Parses the User ID according to de facto conventions, and
+ /// returns the comment field, if any.
+ ///
+ /// Like [`UserID::comment2`], but heap-allocates.
+ #[deprecated(note = "Use UserID::comment2")]
pub fn comment(&self) -> Result<Option<String>> {
Ok(self.do_parse()?.comment().map(|s| s.to_string()))
}
@@ -892,6 +910,15 @@ impl UserID {
/// See [conventional User ID] for more information.
///
/// [conventional User ID]: #conventional-user-ids
+ pub fn email2(&self) -> Result<Option<&str>> {
+ Ok(self.do_parse()?.email())
+ }
+
+ /// Parses the User ID according to de facto conventions, and
+ /// returns the email address, if any.
+ ///
+ /// Like [`UserID::email2`], but heap-allocates.
+ #[deprecated(note = "Use UserID::email2")]
pub fn email(&self) -> Result<Option<String>> {
Ok(self.do_parse()?.email().map(|s| s.to_string()))
}
@@ -902,6 +929,15 @@ impl UserID {
/// See [conventional User ID] for more information.
///
/// [conventional User ID]: #conventional-user-ids
+ pub fn uri2(&self) -> Result<Option<&str>> {
+ Ok(self.do_parse()?.uri())
+ }
+
+ /// Parses the User ID according to de facto conventions, and
+ /// returns the URI, if any.
+ ///
+ /// Like [`UserID::uri2`], but heap-allocates.
+ #[deprecated(note = "Use UserID::uri2")]
pub fn uri(&self) -> Result<Option<String>> {
Ok(self.do_parse()?.uri().map(|s| s.to_string()))
}
@@ -925,10 +961,9 @@ impl UserID {
/// [empty locale]: https://www.w3.org/International/wiki/Case_folding
/// [Autocrypt]: https://autocrypt.org/level1.html#e-mail-address-canonicalization
pub fn email_normalized(&self) -> Result<Option<String>> {
- match self.email() {
- e @ Err(_) => e,
- Ok(None) => Ok(None),
- Ok(Some(address)) => {
+ match self.email2()? {
+ None => Ok(None),
+ Some(address) => {
let mut iter = address.split('@');
let localpart = iter.next().expect("Invalid email address");
let domain = iter.next().expect("Invalid email address");