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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
// This file is part of GAnarchy - decentralized development hub
// Copyright (C) 2021 Soni L.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Standard managers.
//!
//! Managers combine multiple data sources together and apply any desired
//! filtering.
use std::collections::BTreeSet;
use std::error;
use std::fmt;
use std::sync::Arc;
use impl_trait::impl_trait;
use qcell::{QCell, QCellOwner};
use super::DataSourceBase;
use super::DataSource;
#[cfg(doc)]
use super::effective::EffectiveDataSource;
use super::effective::EffectiveKind;
use super::kinds::{InstanceTitle, InstanceBaseUrl, RepoListUrl, ProjectFork};
use super::sources::DefaultsDataSource;
use super::Update;
/// A wrapper around multiple [`DataSource`]s of [`ProjectFork`]s.
///
/// While a `RepoListManager` doesn't have to be wrapped in an
/// [`EffectiveDataSource`] for correctness, it's usually desired to do so for
/// consistency.
#[derive(Default)]
pub struct RepoListManager {
repos: Vec<Box<dyn DataSource<EffectiveKind<ProjectFork>> + Send + Sync>>,
valid: usize,
}
/// A wrapper around multiple [`DataSource`]s of kinds generally used for
/// configuration.
///
/// `ConfigManager` wraps the following [`DataSource`] kinds:
///
/// - [`InstanceTitle`]
/// - [`InstanceBaseUrl`]
/// - [`RepoListUrl`]
///
/// Generally, a `ConfigManager` must be wrapped in an [`EffectiveDataSource`]
/// for correctness.
#[derive(Default)]
pub struct ConfigManager {
owner: QCellOwner,
resources: Vec<Resource>,
// add_source can be called after update.
valid: usize,
}
/// Helper for adding [`DataSource`]s to [`ConfigManager`]s.
///
/// See the documentation for [`ConfigManager::add_source`] for details.
pub struct AddConfigSource<'a, T: DataSourceBase + Send + Sync + 'static> {
resource: &'a mut Resource,
source: Arc<QCell<T>>,
}
/// Error type for managers in general.
#[derive(Debug)]
pub struct MultiResult {
pub results: Vec<Result<(), Box<dyn error::Error + Send + Sync>>>,
}
struct Resource {
// actual resource
base: Arc<QCell<dyn DataSourceBase + Send + Sync>>,
// views of the actual resource
title: Option<Arc<QCell<dyn DataSource<InstanceTitle> + Send + Sync>>>,
url: Option<Arc<QCell<dyn DataSource<InstanceBaseUrl> + Send + Sync>>>,
repolist: Option<Arc<QCell<dyn DataSource<RepoListUrl> + Send + Sync>>>,
}
impl Resource {
fn new(base: Arc<QCell<dyn DataSourceBase + Send + Sync>>) -> Self {
Self {
base,
title: None,
url: None,
repolist: None,
}
}
}
impl_trait! {
impl MultiResult {
impl trait error::Error {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
// TODO return first error?
None
}
}
impl trait fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Multiple errors.")
}
}
}
}
impl_trait! {
impl RepoListManager {
/// Creates a new, empty `RepoListManager`. Equivalent to
/// `Default::default()`.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::RepoListManager;
///
/// let repos = RepoListManager::new();
/// ```
pub fn new() -> Self {
Default::default()
}
/// Adds the given [`DataSource`] to this `RepoListManager`.
///
/// # Examples
///
/// ```
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::managers::RepoListManager;
///
/// let mut repos = RepoListManager::default();
/// repos.add_source(DefaultsDataSource);
/// ```
pub fn add_source<T>(&mut self, source: T)
where
T: DataSource<EffectiveKind<ProjectFork>> + Send + Sync + 'static,
{
self.repos.push(Box::new(source));
}
impl trait DataSourceBase {
/// Updates the contained `DataSource`s, and returns any relevant
/// update stats.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::RepoListManager;
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::DataSourceBase;
///
/// let mut repos = RepoListManager::default();
/// repos.add_source(DefaultsDataSource);
/// let update = repos.update();
/// # assert!(update.errors.is_empty());
/// ```
fn update(&mut self) -> Update {
let mut errors = Vec::new();
self.repos.iter_mut().for_each(|e| {
let ret = e.update();
errors.extend(ret.errors);
});
self.valid = self.repos.len();
Update {
errors: errors,
}
}
/// Returns whether this data source contains any (valid) data.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::RepoListManager;
/// use ganarchy::data::DataSourceBase;
///
/// let mut repos = RepoListManager::default();
/// // An empty RepoListManager has no data.
/// assert!(!repos.exists());
/// ```
fn exists(&self) -> bool {
self.repos[..self.valid].iter().any(|e| e.exists())
}
}
impl trait std::fmt::Display {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "repo list: [")?;
for e in self.repos.iter() {
write!(f, "{}, ", e)?;
}
write!(f, "]")
}
}
/// Returns all [`ProjectFork`]s.
impl trait DataSource<EffectiveKind<ProjectFork>> {
fn get_values(&self) -> BTreeSet<EffectiveKind<ProjectFork>> {
self.repos[..self.valid].iter().flat_map(|e| {
e.get_values()
}).collect()
}
}
}
}
impl_trait! {
impl ConfigManager {
/// Creates a new, empty `ConfigManager`. Equivalent to
/// `Default::default()`.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::ConfigManager;
///
/// let cm = ConfigManager::new();
/// ```
pub fn new() -> Self {
Default::default()
}
/// Adds a [`DefaultsDataSource`] to this `ConfigManager`, for all
/// supported properties.
///
/// The added source will override any further data sources, so this
/// should be called last, after all other data sources have been
/// added.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::ConfigManager;
///
/// let mut cm = ConfigManager::default();
/// cm.add_defaults();
/// ```
pub fn add_defaults(&mut self) {
self.add_source(DefaultsDataSource).for_all();
}
/// Adds the given [`DataSource`] to this `ConfigManager` and returns
/// a helper for setting which properties this `DataSource` will
/// provide through this `ConfigManager`.
///
/// # Examples
///
/// ```
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::managers::ConfigManager;
///
/// let mut cm = ConfigManager::default();
/// cm.add_source(DefaultsDataSource).for_repo_lists();
/// ```
pub fn add_source<T>(&mut self, source: T) -> AddConfigSource<'_, T>
where
T: DataSourceBase + Send + Sync + 'static,
{
let arc = Arc::new(QCell::new(&self.owner, source));
self.resources.push(Resource::new(arc.clone()));
AddConfigSource {
resource: self.resources.last_mut().unwrap(),
source: arc,
}
}
impl trait DataSourceBase {
/// Updates the contained `DataSource`s, and returns any relevant
/// update stats.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::ConfigManager;
/// use ganarchy::data::DataSourceBase;
///
/// let mut cm = ConfigManager::default();
/// cm.add_defaults();
/// let update = cm.update();
/// # assert!(update.errors.is_empty());
/// ```
fn update(&mut self) -> Update {
let owner = &mut self.owner;
let mut errors = Vec::new();
self.resources.iter().for_each(|e| {
let ret = owner.rw(&*e.base).update();
errors.extend(ret.errors);
});
self.valid = self.resources.len();
Update {
errors: errors,
}
}
/// Returns whether this data source contains any (valid) data.
///
/// # Examples
///
/// ```
/// use ganarchy::data::managers::ConfigManager;
/// use ganarchy::data::DataSourceBase;
///
/// let mut cm = ConfigManager::default();
/// // An empty ConfigManager has no data.
/// assert!(!cm.exists());
/// ```
fn exists(&self) -> bool {
self.resources[..self.valid].iter().any(|e| {
self.owner.ro(&*e.base).exists()
})
}
}
impl trait std::fmt::Display {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "config: [")?;
for e in self.resources.iter() {
write!(f, "{}, ", self.owner.ro(&*e.base))?;
}
write!(f, "]")
}
}
/// Returns the first [`InstanceTitle`] available.
impl trait DataSource<InstanceTitle> {
fn get_values(&self) -> Option<InstanceTitle> {
self.resources[..self.valid].iter().flat_map(|e| {
e.title.as_ref()
}).flat_map(|e| {
self.owner.ro(&*e).get_values()
}).next()
}
}
/// Returns the first [`InstanceBaseUrl`] available.
impl trait DataSource<InstanceBaseUrl> {
fn get_values(&self) -> Option<InstanceBaseUrl> {
self.resources[..self.valid].iter().flat_map(|e| {
e.url.as_ref()
}).flat_map(|e| {
self.owner.ro(&*e).get_values()
}).next()
}
}
/// Returns all [`RepoListUrl`]s.
///
/// For correct results, wrap this [`ConfigManager`] in an
/// [`EffectiveDataSource`].
impl trait DataSource<RepoListUrl> {
fn get_values(&self) -> Vec<RepoListUrl> {
self.resources[..self.valid].iter().flat_map(|e| {
e.repolist.as_ref()
}).flat_map(|e| {
self.owner.ro(&*e).get_values()
}).collect()
}
}
}
}
impl<'a, T: DataSourceBase + Send + Sync + 'static> AddConfigSource<'a, T> {
/// Adds this data source as a provider for [`InstanceTitle`].
///
/// # Examples
///
/// ```
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::managers::ConfigManager;
///
/// let mut cm = ConfigManager::default();
/// cm.add_source(DefaultsDataSource).for_title();
/// ```
pub fn for_title(self) -> Self where T: DataSource<InstanceTitle> {
let arc = &self.source;
self.resource.title.get_or_insert_with(|| {
arc.clone()
});
self
}
/// Adds this data source as a provider for [`InstanceBaseUrl`].
///
/// # Examples
///
/// ```
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::managers::ConfigManager;
///
/// let mut cm = ConfigManager::default();
/// cm.add_source(DefaultsDataSource).for_base_url();
/// ```
pub fn for_base_url(self) -> Self where T: DataSource<InstanceBaseUrl> {
let arc = &self.source;
self.resource.url.get_or_insert_with(|| {
arc.clone()
});
self
}
/// Adds this data source as a provider for [`RepoListUrl`].
///
/// # Examples
///
/// ```
/// use ganarchy::data::sources::DefaultsDataSource;
/// use ganarchy::data::managers::ConfigManager;
///
/// let mut cm = ConfigManager::default();
/// cm.add_source(DefaultsDataSource).for_repo_lists();
/// ```
pub fn for_repo_lists(self) -> Self where T: DataSource<RepoListUrl> {
let arc = &self.source;
self.resource.repolist.get_or_insert_with(|| {
arc.clone()
});
self
}
// pub(crate): we wanna be able to make breaking changes to this function
// without affecting downstream users.
pub(crate) fn for_all(self) -> Self
where
T: DataSource<InstanceTitle>,
T: DataSource<InstanceBaseUrl>,
T: DataSource<RepoListUrl>,
{
self.for_title().for_base_url().for_repo_lists()
}
}
|