Skip to content

Commit

Permalink
Add accessors for email / name on Contact (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer authored Oct 28, 2024
1 parent 1a2dbe5 commit 1d8328f
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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]"));
}
}

0 comments on commit 1d8328f

Please sign in to comment.