summary refs log tree commit diff stats
path: root/src/data.rs
blob: 3910dda56a1382f4aff67ee31e1c26ca77d51e8e (plain) (blame)
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
// 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/>.

//! This module handles data source retrieval, parsing and processing.
//!
//! Data sources identify where to find repos and projects, among other things.

use std::error;
use std::hash::Hash;
use std::time::Duration;

pub mod effective;
pub mod kinds;
pub mod managers;

/// A source of data.
pub trait DataSourceBase {
    /// Refreshes the data associated with this source, if necessary, and
    /// returns the interval at which this should be called again.
    ///
    /// # Errors
    ///
    /// Returns an error if the source could not be updated. If an error is
    /// returned, an attempt should be made to **not** invalidate this data
    /// source, but instead carry on using stale data. Note that an aggregate
    /// data source may partially update and return an aggregate error type.
    fn update(&mut self) -> (
        Duration,
        Result<(), Box<dyn error::Error + Send + Sync + 'static>>,
    );

    /// Returns whether this data source contains any (valid) data.
    ///
    /// # Panics
    ///
    /// This is allowed (but not required) to panic if called before `update`,
    /// but should not panic if `update` has been called, but failed.
    fn exists(&self) -> bool;
}

/// A source of data of some specified kind.
pub trait DataSource<T: Kind>: DataSourceBase {
    /// Returns the values associated with this kind.
    ///
    /// # Panics
    ///
    /// This is allowed (but not required) to panic if called before `update`,
    /// but should not panic if `update` has been called, but failed.
    fn get_values(&self) -> T::Values;

    /// Returns the value associated with this kind.
    ///
    /// This is the same as [`get_values`] but allows using the singular name,
    /// for kinds with up to only one value.
    ///
    /// # Panics
    ///
    /// This is allowed (but not required) to panic if called before `update`,
    /// but should not panic if `update` has been called, but failed.
    fn get_value(&self) -> Option<T> where T: Kind<Values=Option<T>> {
        self.get_values()
    }
}

/// A kind of value that can be retrieved from a data source.
pub trait Kind {
    /// Values of this kind.
    type Values: IntoIterator<Item=Self>;
}

/// A kind of value that can be retrieved from a data source, which can
/// override values of the same kind.
pub trait OverridableKind: Kind {
    /// The key, as in a map key, that gets overridden.
    type Key: Hash + Eq + Ord;

    /// Returns the key for use in a key-value map such as a `BTreeMap` or
    /// a set such as `BTreeSet`.
    fn as_key(&self) -> &Self::Key;
}