1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::{
    document,
    resolution::{self, DerefError},
};
use serde::Serialize;

#[derive(Serialize)]
pub enum ResolutionResult {
    Success {
        content: String,
        metadata: resolution::Metadata,
        document_metadata: document::Metadata,
    },
    Failure {
        error: ErrorCode,
    },
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub enum ErrorCode {
    InvalidDid,
    NotFound,
    RepresentationNotSupported,
    MethodNotSupported,
    InternalError,
}

impl From<resolution::Error> for ErrorCode {
    fn from(value: resolution::Error) -> Self {
        match value {
            resolution::Error::MethodNotSupported(_) => Self::MethodNotSupported,
            resolution::Error::NotFound => Self::NotFound,
            resolution::Error::NoRepresentation => Self::InternalError,
            resolution::Error::RepresentationNotSupported(_) => Self::RepresentationNotSupported,
            resolution::Error::InvalidData(_) => Self::InternalError,
            resolution::Error::InvalidMethodSpecificId(_) => Self::InvalidDid,
            resolution::Error::InvalidOptions => Self::InternalError,
            resolution::Error::Internal(_) => Self::InternalError,
        }
    }
}

impl From<DerefError> for ErrorCode {
    fn from(value: DerefError) -> Self {
        match value {
            DerefError::Resolution(e) => e.into(),
            DerefError::MissingServiceEndpoint(_) => Self::InternalError,
            DerefError::UnsupportedServiceEndpointMap => Self::InternalError,
            DerefError::UnsupportedMultipleServiceEndpoints => Self::InternalError,
            DerefError::ServiceEndpointConstructionFailed(_) => Self::InternalError,
            DerefError::FragmentConflict => Self::InternalError,
            DerefError::NullDereference => Self::InternalError,
            DerefError::NotFound => Self::NotFound,
            DerefError::ResourceNotFound(_) => Self::NotFound,
        }
    }
}