identity_credential/credential/
issuer.rsuse serde::Deserialize;
use serde::Serialize;
use identity_core::common::Object;
use identity_core::common::Url;
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct IssuerData {
pub id: Url,
#[serde(flatten)]
pub properties: Object,
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Issuer {
Url(Url),
Obj(IssuerData),
}
impl Issuer {
pub fn url(&self) -> &Url {
match self {
Self::Url(url) => url,
Self::Obj(obj) => &obj.id,
}
}
}
impl<T> From<T> for Issuer
where
T: Into<Url>,
{
fn from(other: T) -> Self {
Self::Url(other.into())
}
}
#[cfg(test)]
mod tests {
use identity_core::convert::FromJson;
use crate::credential::Issuer;
const JSON1: &str = include_str!("../../tests/fixtures/issuer-1.json");
const JSON2: &str = include_str!("../../tests/fixtures/issuer-2.json");
#[test]
fn test_from_json() {
let issuer: Issuer = Issuer::from_json(JSON1).unwrap();
assert!(matches!(issuer, Issuer::Url(_)));
assert_eq!(issuer.url(), "https://example.edu/issuers/14");
let issuer: Issuer = Issuer::from_json(JSON2).unwrap();
assert!(matches!(issuer, Issuer::Obj(_)));
assert_eq!(issuer.url(), "did:example:76e12ec712ebc6f1c221ebfeb1f");
}
}