wip: `as_components()` returns `c` for all variants
parent
53000ca5d1
commit
e5d645c7ee
|
@ -19,7 +19,7 @@ pub enum Address {
|
|||
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
pub struct AddressComponents<'a> {
|
||||
pub t: &'a str,
|
||||
pub c: Option<&'a str>,
|
||||
pub c: Option<String>,
|
||||
}
|
||||
|
||||
// multihash SHA2-256
|
||||
|
@ -76,13 +76,13 @@ impl Address {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn as_components(&self) -> AddressComponents {
|
||||
pub fn as_components<'a>(&'a self) -> AddressComponents<'a> {
|
||||
// TODO: make this automatically derive from `Address` definition
|
||||
let (entity_type, entity_content) = match self {
|
||||
Address::Hash(_) => ("Hash", None),
|
||||
Address::Uuid(_) => ("Uuid", None),
|
||||
Address::Attribute(attribute) => ("Attribute", Some(attribute.as_str())),
|
||||
Address::Url(url) => ("Url", Some(url.as_str())),
|
||||
Address::Hash(digest) => ("Hash", Some(b58_encode(digest))),
|
||||
Address::Uuid(uuid) => ("Uuid", Some(uuid.to_string())),
|
||||
Address::Attribute(attribute) => ("Attribute", Some(attribute.clone())),
|
||||
Address::Url(url) => ("Url", Some(url.to_string())),
|
||||
};
|
||||
|
||||
AddressComponents {
|
||||
|
@ -91,9 +91,9 @@ impl Address {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_components(components: &AddressComponents) -> Result<Self> {
|
||||
pub fn from_components(components: AddressComponents) -> Result<Self> {
|
||||
// TODO: make this automatically derive from `Address` definition
|
||||
let address = match *components {
|
||||
let address = match components {
|
||||
AddressComponents { t: "Attribute", c } => Address::Attribute(
|
||||
c.map(|x| x.to_string())
|
||||
.ok_or(anyhow!("Missing attribute."))?,
|
||||
|
|
|
@ -333,10 +333,9 @@ impl TryInto<Address> for InAddress {
|
|||
fn try_into(self) -> Result<Address, Self::Error> {
|
||||
Ok(match self {
|
||||
InAddress::Address(address) => address.parse()?,
|
||||
InAddress::Components { t, c } => Address::from_components(&AddressComponents {
|
||||
t: &t,
|
||||
c: c.as_ref().map(|x| x.as_str()),
|
||||
})?,
|
||||
InAddress::Components { t, c } => {
|
||||
Address::from_components(AddressComponents { t: &t, c })?
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -1013,14 +1012,16 @@ mod tests {
|
|||
>(None, vec![], get_state()))
|
||||
.await;
|
||||
|
||||
let address = Address::Hash(Digest(vec![1, 2, 3, 4]));
|
||||
let digest = Digest(vec![1, 2, 3, 4]);
|
||||
let digest_str = b58_encode(&digest);
|
||||
let address = Address::Hash(digest);
|
||||
let req = actix_web::test::TestRequest::get()
|
||||
.uri(&format!("/api/obj/{}", address.to_string()))
|
||||
.to_request();
|
||||
let result: serde_json::Value = actix_web::test::call_and_read_body_json(&app, req).await;
|
||||
|
||||
assert_eq!(result["entity"]["t"], "Hash");
|
||||
assert_eq!(result["entity"]["c"], serde_json::Value::Null);
|
||||
assert_eq!(result["entity"]["c"], digest_str);
|
||||
|
||||
let address = Address::Attribute("TEST".to_string());
|
||||
let req = actix_web::test::TestRequest::get()
|
||||
|
@ -1048,7 +1049,7 @@ mod tests {
|
|||
let result: serde_json::Value = actix_web::test::call_and_read_body_json(&app, req).await;
|
||||
|
||||
assert_eq!(result["entity"]["t"], "Uuid");
|
||||
assert_eq!(result["entity"]["c"], serde_json::Value::Null);
|
||||
assert_eq!(result["entity"]["c"], uuid.to_string());
|
||||
}
|
||||
|
||||
fn get_state() -> State {
|
||||
|
|
Loading…
Reference in New Issue