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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::reference::Reference;
use crate::{Record, TypedValue, UntypedRecord};
use std::fmt;

/// A trait to be implemented on [TypedValue] structs that contain reference fields.
#[async_trait::async_trait]
pub trait Resolvable: TypedValue {
    /// Resolve all reference with the passed resolver.
    async fn resolve_refs<R: Resolver + Send + Sync>(
        &mut self,
        _resolver: &R,
    ) -> Result<(), MissingRefsError> {
        Ok(())
    }

    /// Extract all loaded records from resolved references, resetting them to their id state.
    fn extract_refs(&mut self) -> Vec<UntypedRecord> {
        vec![]
    }
}

/// A trait to be implemented on data stores that can resolve records by their type and id strings.
#[async_trait::async_trait]
pub trait Resolver {
    type Error: std::error::Error + Send + Sync + 'static;

    /// Resolve (load) a single record by its id.
    async fn resolve<T: TypedValue>(&self, id: &str) -> Result<Record<T>, Self::Error>;

    /// Resolve (load) all records with their ids.
    ///
    /// The method is generic over the record type, thus only supports loading records of a single
    /// type.
    async fn resolve_all<T: TypedValue + Send>(
        &self,
        ids: &[&str],
    ) -> Vec<Result<Record<T>, Self::Error>> {
        let futs: Vec<_> = ids.iter().map(|id| self.resolve(id)).collect();
        let results = futures_util::future::join_all(futs).await;
        results
    }

    async fn resolve_all_refs<T: Resolvable + Send>(
        &self,
        records: &mut [Record<T>],
    ) -> Result<(), MissingRefsError>
    where
        Self: Sized + Send,
    {
        let futs: Vec<_> = records
            .iter_mut()
            .map(|record| record.resolve_refs(&*self))
            .collect();
        let results = futures_util::future::join_all(futs).await;
        let errs: Vec<ResolveError> = results
            .into_iter()
            .filter_map(|r| r.err())
            .map(|e| e.0)
            .flatten()
            .collect();
        match errs.is_empty() {
            true => Ok(()),
            false => Err(MissingRefsError(errs)),
        }
    }

    /// Resolve a list of references.
    async fn resolve_refs<T: TypedValue + Send>(
        &self,
        references: &mut [Reference<T>],
    ) -> Result<(), MissingRefsError> {
        let unresolved_refs: Vec<(usize, String)> = references
            .iter()
            .enumerate()
            .filter_map(|(i, r)| match r {
                Reference::Id(id) => Some((i, id.clone())),
                _ => None,
            })
            .collect();
        let unresolved_ids: Vec<&str> = unresolved_refs.iter().map(|(_, id)| id.as_str()).collect();
        let results = self.resolve_all(&unresolved_ids).await;
        let mut errs: Vec<ResolveError> = vec![];
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Ok(record) => references.get_mut(i).unwrap().set_resolved(record),
                Err(err) => errs.push(ResolveError::new(
                    references.get(i).unwrap().id(),
                    err.into(),
                )),
            }
        }
        match errs.len() {
            0 => Ok(()),
            _ => Err(MissingRefsError(errs)),
        }
    }
}

/// An error that occurs while resolving (loading) records.
#[derive(Debug)]
pub struct ResolveError {
    id: String,
    error: anyhow::Error,
    // typ: String,
}
// pub struct ResolveError {
//     id: String,
//     error: anyhow::Error,
//     typ: PhantomData<T>,
// }
//

/// Error type for records that failed to resolve.
///
/// Contains a list of [ResolveError]s that map record ids to errors.
#[derive(Debug)]
pub struct MissingRefsError(pub Vec<ResolveError>);

impl fmt::Display for MissingRefsError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Failed to resolve {} refs", self.0.len())
    }
}

impl std::error::Error for MissingRefsError {}

impl ResolveError {
    pub fn new(id: &str, error: anyhow::Error) -> Self {
        Self {
            id: id.to_string(),
            error,
            // typ: PhantomData
        }
    }

    /// Convert the error into a unresolved [Reference].
    pub fn into_reference<T: Clone>(self) -> Reference<T> {
        Reference::Id(self.id)
    }
}

impl<T: TypedValue> From<ResolveError> for Reference<T> {
    fn from(err: ResolveError) -> Self {
        Reference::Id(err.id)
    }
}

impl std::error::Error for ResolveError {}
impl fmt::Display for ResolveError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Failed to resolve {}: {:?}", self.id, self.error)
    }
}