use serde::{Deserialize, Serialize};
use super::{validate_orcid, Affiliation, ContributorRole, ValidationError};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Author {
pub name: String,
pub orcid: Option<String>,
pub affiliations: Vec<Affiliation>,
pub roles: Vec<ContributorRole>,
}
impl Author {
pub fn new(name: impl Into<String>) -> Self {
Self { name: name.into(), orcid: None, affiliations: Vec::new(), roles: Vec::new() }
}
pub fn with_orcid(mut self, orcid: impl Into<String>) -> Result<Self, ValidationError> {
let id = orcid.into();
if !validate_orcid(&id) {
return Err(ValidationError::InvalidOrcid(id));
}
self.orcid = Some(id);
Ok(self)
}
pub fn with_affiliation(mut self, affiliation: Affiliation) -> Self {
self.affiliations.push(affiliation);
self
}
pub fn with_role(mut self, role: ContributorRole) -> Self {
if !self.roles.contains(&role) {
self.roles.push(role);
}
self
}
pub fn with_roles(mut self, roles: impl IntoIterator<Item = ContributorRole>) -> Self {
for role in roles {
if !self.roles.contains(&role) {
self.roles.push(role);
}
}
self
}
pub fn last_name(&self) -> &str {
self.name.split_whitespace().next_back().unwrap_or(&self.name)
}
}