-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add accessors for email / name on Contact (#27)
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -177,6 +177,24 @@ pub enum Contact { | |
Email { email: String }, | ||
} | ||
|
||
impl Contact { | ||
/// Returns the name of the contact. | ||
pub fn name(&self) -> Option<&str> { | ||
match self { | ||
Contact::NameEmail { name, .. } | Contact::Name { name } => Some(name), | ||
Contact::Email { .. } => None, | ||
} | ||
} | ||
|
||
/// Returns the email of the contact. | ||
pub fn email(&self) -> Option<&str> { | ||
match self { | ||
Contact::NameEmail { email, .. } | Contact::Email { email } => Some(email), | ||
Contact::Name { .. } => None, | ||
} | ||
} | ||
} | ||
|
||
/// The `[dependency-groups]` section of pyproject.toml, as specified in PEP 735 | ||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] | ||
#[serde(transparent)] | ||
|
@@ -503,4 +521,29 @@ a table with 'name' and/or 'email' keys | |
" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_contact_accessors() { | ||
let contact = super::Contact::NameEmail { | ||
name: "John Doe".to_string(), | ||
email: "[email protected]".to_string(), | ||
}; | ||
|
||
assert_eq!(contact.name(), Some("John Doe")); | ||
assert_eq!(contact.email(), Some("[email protected]")); | ||
|
||
let contact = super::Contact::Name { | ||
name: "John Doe".to_string(), | ||
}; | ||
|
||
assert_eq!(contact.name(), Some("John Doe")); | ||
assert_eq!(contact.email(), None); | ||
|
||
let contact = super::Contact::Email { | ||
email: "[email protected]".to_string(), | ||
}; | ||
|
||
assert_eq!(contact.name(), None); | ||
assert_eq!(contact.email(), Some("[email protected]")); | ||
} | ||
} |