identity_core/common/
context.rs

1// Copyright 2020-2022 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::fmt::Debug;
5use core::fmt::Formatter;
6use core::fmt::Result;
7
8use serde;
9use serde::Deserialize;
10use serde::Serialize;
11
12use crate::common::Object;
13use crate::common::Url;
14
15/// A reference to a JSON-LD context
16///
17/// [More Info](https://www.w3.org/TR/vc-data-model/#contexts)
18#[derive(Clone, PartialEq, Eq, Deserialize, Serialize)]
19#[serde(untagged)]
20pub enum Context {
21  /// A JSON-LD context expressed as a Url.
22  Url(Url),
23  /// A JSON-LD context expressed as a JSON object.
24  Obj(Object),
25}
26
27impl Debug for Context {
28  fn fmt(&self, f: &mut Formatter<'_>) -> Result {
29    match self {
30      Self::Url(inner) => Debug::fmt(inner, f),
31      Self::Obj(inner) => Debug::fmt(inner, f),
32    }
33  }
34}
35
36impl From<Url> for Context {
37  fn from(other: Url) -> Self {
38    Self::Url(other)
39  }
40}
41
42impl From<Object> for Context {
43  fn from(other: Object) -> Self {
44    Self::Obj(other)
45  }
46}
47
48impl<T> PartialEq<T> for Context
49where
50  T: AsRef<str> + ?Sized,
51{
52  fn eq(&self, other: &T) -> bool {
53    match self {
54      Self::Url(inner) => inner.as_str() == other.as_ref(),
55      Self::Obj(_) => false,
56    }
57  }
58}