use serde::{Deserialize, Serialize};
use crate::error::GithubError;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OrgSlug(String);
impl OrgSlug {
pub fn parse(raw: impl AsRef<str>) -> Result<Self, GithubError> {
let raw = raw.as_ref().trim();
if raw.is_empty() {
return Err(GithubError::InvalidIdentifier("empty".into()));
}
Ok(Self(raw.to_string()))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Organization {
pub login: OrgSlug,
pub html_url: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identifier_rejects_empty() {
assert!(OrgSlug::parse("").is_err());
assert!(OrgSlug::parse(" ").is_err());
}
#[test]
fn entity_serde_round_trips() {
let e = Organization {
login: OrgSlug::parse("STUB-001").unwrap(),
html_url: "Stub Entity".into(),
};
let json = serde_json::to_string(&e).unwrap();
let back: Organization = serde_json::from_str(&json).unwrap();
assert_eq!(back, e);
}
}