use monostate::MustBe;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use super::verification::Obj as Verification;
#[derive(Debug, Eq, PartialEq, Clone, Serialize, Deserialize, TypedBuilder)]
#[serde(rename_all = "camelCase")]
#[serde(rename = "Issuer")]
pub struct Obj {
#[serde(rename = "@context")]
#[builder(default = MustBe!("https://w3id.org/openbadges/v2"))]
pub context: MustBe!("https://w3id.org/openbadges/v2"),
#[builder(default = MustBe!("Issuer"))]
pub r#type: MustBe!("Issuer"),
#[builder(setter(into))]
pub id: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub telephone: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub image: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub public_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
pub verification: Option<Verification>,
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default)]
#[builder(setter(strip_option))]
#[builder(setter(into))]
pub revocation_list: Option<String>,
}
impl Obj {
pub fn new<S: Into<String>>(id: S) -> Self {
Self {
context: MustBe!("https://w3id.org/openbadges/v2"),
r#type: MustBe!("Issuer"),
id: id.into(),
name: None,
url: None,
telephone: None,
description: None,
image: None,
email: None,
public_key: None,
verification: None,
revocation_list: None,
}
}
}
impl super::ToJsonLd for Obj {}
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::ToJsonLd;
const EXP_JSON_LD_SIMPLE: &str = r#"{
"@context": "https://w3id.org/openbadges/v2",
"type": "Issuer",
"id": "http://abc.de/org.json",
"name": "John Doe",
"url": "https://abc.de/"
}"#;
const EXP_JSON_LD_WITH_KEY: &str = r#"{
"@context": "https://w3id.org/openbadges/v2",
"type": "Issuer",
"id": "http://abc.de/org.json",
"name": "John Doe",
"url": "https://abc.de/",
"publicKey": "http://abc.de/key.json"
}"#;
#[test]
fn test_new_simple() -> Result<(), Box<dyn std::error::Error>> {
let mut obj = Obj::new("http://abc.de/org.json");
obj.name = Some("John Doe".to_string());
obj.url = Some("https://abc.de/".to_string());
let json_ld = obj.to_json_ld()?;
assert_eq!(&json_ld, EXP_JSON_LD_SIMPLE);
Ok(())
}
#[test]
fn test_new_with_key() -> Result<(), Box<dyn std::error::Error>> {
let mut obj = Obj::new("http://abc.de/org.json");
obj.name = Some("John Doe".to_string());
obj.url = Some("https://abc.de/".to_string());
obj.public_key = Some("http://abc.de/key.json".to_string());
let json_ld = obj.to_json_ld()?;
assert_eq!(&json_ld, EXP_JSON_LD_WITH_KEY);
Ok(())
}
#[test]
fn test_builder_simple() -> Result<(), Box<dyn std::error::Error>> {
let obj = Obj::builder()
.id("http://abc.de/org.json")
.name("John Doe")
.url("https://abc.de/")
.build();
let json_ld = obj.to_json_ld()?;
assert_eq!(&json_ld, EXP_JSON_LD_SIMPLE);
Ok(())
}
#[test]
fn test_builder_with_key() -> Result<(), Box<dyn std::error::Error>> {
let obj = Obj::builder()
.id("http://abc.de/org.json")
.name("John Doe")
.url("https://abc.de/")
.public_key("http://abc.de/key.json")
.build();
let json_ld = obj.to_json_ld()?;
assert_eq!(&json_ld, EXP_JSON_LD_WITH_KEY);
Ok(())
}
}