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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use crate::samplic::{
    Curve, Description, DeviceDescription, Icon, Measurement,
    MeasurementMetadata, MeasurementResults, ProfileDescription, Sample,
    SampleMetadata, Sampling, Translation, UnitDescription,
};
use crate::{error, Result};
use indexmap::{IndexMap, IndexSet};
use uuid::Uuid;

pub trait LoadableDevice {
    fn load_unit_index(&self) -> Result<IndexSet<String>>;

    fn load_unit(&self, unit_id: &str) -> Result<UnitDescription>;

    fn load_translation_index(&self) -> Result<IndexSet<String>>;

    fn load_translation(
        &self,
        translation_id: &str,
    ) -> Result<Translation>;

    fn load_sample_metadata(
        &self,
        sample_id: Uuid,
    ) -> Result<SampleMetadata>;

    fn load_measurement_metadata(
        &self,
        sample_id: Uuid,
        measurement_index: usize,
    ) -> Result<MeasurementMetadata>;

    fn load_curve(
        &self,
        sample_id: Uuid,
        measurement_index: usize,
        curve_id: &str,
    ) -> Result<Curve>;

    fn load_curve_index(
        &self,
        sample_id: Uuid,
        measurement_index: usize,
    ) -> Result<IndexSet<String>>;

    fn load_results(
        &self,
        sample_id: Uuid,
        measurement_index: usize,
    ) -> Result<MeasurementResults>;

    fn load_sampling_index(&self) -> Result<IndexSet<String>>;

    fn load_measurement_index(
        &self,
        sample_id: Uuid,
    ) -> Result<IndexSet<usize>>;

    fn load_sample_index(&self) -> Result<IndexSet<Uuid>>;

    fn load_device_description(&self) -> Result<DeviceDescription>;

    fn load_system_profile(
        &self,
        profile_id: Uuid,
    ) -> Result<ProfileDescription>;

    fn load_user_profile(
        &self,
        profile_id: Uuid,
    ) -> Result<ProfileDescription>;

    fn load_system_profile_index(&self) -> Result<IndexSet<Uuid>>;

    fn load_user_profile_index(&self) -> Result<IndexSet<Uuid>>;

    fn load_sampling(&self, sampling_id: &str) -> Result<Sampling>;

    fn load_icon(&self, icon_id: &str) -> Result<Icon>;

    fn load_icon_index(&self) -> Result<IndexSet<String>>;

    fn load_profile_index(&self) -> Result<IndexSet<Uuid>> {
        let mut profiles = self.load_system_profile_index()?;
        profiles.extend(self.load_user_profile_index()?);
        Ok(profiles)
    }

    fn load_profile(
        &self,
        profile_id: Uuid,
    ) -> Result<ProfileDescription> {
        if let Ok(profile) = self.load_system_profile(profile_id) {
            Ok(profile)
        } else if let Ok(profile) = self.load_user_profile(profile_id) {
            Ok(profile)
        } else {
            error::NotFound {
                kind: "profile",
                id: profile_id.to_hyphenated_ref().to_string(),
            }
            .fail()?
        }
    }

    fn load_units(&self) -> Result<IndexMap<String, UnitDescription>> {
        self.load_unit_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_unit(&id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_translations(
        &self,
    ) -> Result<IndexMap<String, IndexMap<String, String>>> {
        self.load_translation_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_translation(&id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_curves(
        &self,
        sample_id: Uuid,
        measurement_index: usize,
    ) -> Result<IndexMap<String, Curve>> {
        self.load_curve_index(sample_id, measurement_index)?
            .into_iter()
            .map(|id| {
                let item =
                    self.load_curve(sample_id, measurement_index, &id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_measurement(
        &self,
        sample_id: Uuid,
        index: usize,
    ) -> Result<Measurement> {
        Ok(Measurement {
            curves: self.load_curves(sample_id, index)?,
            metadata: self.load_measurement_metadata(sample_id, index)?,
            results: self.load_results(sample_id, index)?,
        })
    }

    fn load_measurements(
        &self,
        sample_id: Uuid,
    ) -> Result<IndexMap<usize, Measurement>> {
        self.load_measurement_index(sample_id)?
            .into_iter()
            .map(|id| {
                let item = self.load_measurement(sample_id, id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_sample(&self, sample_id: Uuid) -> Result<Sample> {
        Ok(Sample {
            measurements: self.load_measurements(sample_id)?,
            metadata: self.load_sample_metadata(sample_id)?,
        })
    }

    fn load_samples(&self) -> Result<IndexMap<Uuid, Sample>> {
        self.load_sample_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_sample(id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_system_profiles(
        &self,
    ) -> Result<IndexMap<Uuid, ProfileDescription>> {
        self.load_system_profile_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_system_profile(id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_user_profiles(
        &self,
    ) -> Result<IndexMap<Uuid, ProfileDescription>> {
        self.load_user_profile_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_user_profile(id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_samplings(&self) -> Result<IndexMap<String, Sampling>> {
        self.load_sampling_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_sampling(&id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }

    fn load_description(&self) -> Result<Description> {
        Ok(Description {
            device: self.load_device_description()?,
            system_profiles: self.load_system_profiles()?,
            user_profiles: self.load_user_profiles()?,
            samplings: self.load_samplings()?,
            units: self.load_units()?,
            translations: self.load_translations()?,
        })
    }

    fn load_icons(&self) -> Result<IndexMap<String, String>> {
        self.load_icon_index()?
            .into_iter()
            .map(|id| {
                let item = self.load_icon(&id)?;
                Ok((id, item))
            })
            .collect::<Result<_>>()
    }
}