summary refs log tree commit diff stats
path: root/src/data/managers.rs
blob: 71a94849b54d518d4760854629a00377dd4ef416 (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
94
95
96
97
98
// 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/>.

use std::error;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::RwLock;
use std::time::Duration;

use impl_trait::impl_trait;

use super::DataSourceBase;
use super::DataSource;
use super::effective::EffectiveKind;
//use super::Kind;
use super::kinds::{InstanceTitle, InstanceBaseUrl, RepoListUrl, ProjectFork};
//use super::OverridableKind;

/// Stores multiple DataSource capable of ProjectFork
#[derive(Default)]
pub struct RepoListManager {
    repos: Vec<Box<dyn DataSource<EffectiveKind<ProjectFork>> + Send + Sync>>,
    durations: Vec<Duration>,
    valid: usize,
}

/// Stores multiple DataSource capable of InstanceTitle, InstanceBaseUrl and
/// RepoListUrl
#[derive(Default)]
pub struct ConfigManager {
    // conceptually the actual objects
    bases: Vec<Arc<RwLock<dyn DataSourceBase + Send + Sync>>>,
    // conceptually just views of the above objects
    titles: Vec<Option<Arc<RwLock<dyn DataSource<InstanceTitle> + Send + Sync>>>>,
    urls: Vec<Option<Arc<RwLock<dyn DataSource<InstanceBaseUrl> + Send + Sync>>>>,
    repolists: Vec<Option<Arc<RwLock<dyn DataSource<RepoListUrl> + Send + Sync>>>>,
    durations: Vec<Duration>,
    // add_source can be called after update.
    valid: usize,
}

impl_trait! {
    impl ConfigManager {
        /// Creates a new `ConfigManager`.
        pub fn new() -> Self {
            Default::default()
        }

        /// Adds the given combined `DataSource` to this `ConfigManager`.
        pub fn add_source<T>(&mut self, source: T)
        where
            T: DataSource<InstanceTitle>,
            T: DataSource<InstanceBaseUrl>,
            T: DataSource<RepoListUrl>,
            T: Send + Sync + 'static,
        {
            let arc = Arc::new(RwLock::new(source));
            self.bases.push(arc.clone());
            self.titles.push(Some(arc.clone()));
            self.urls.push(Some(arc.clone()));
            self.repolists.push(Some(arc));
        }

        impl trait DataSourceBase {
            /// Updates the contained `DataSource`s, and returns the shortest
            /// duration for the next update.
            /// 
            /// # Errors
            ///
            /// Returns an error if any `DataSource` returns an error. Always
            /// updates all `DataSource`s.
            fn update(&mut self) -> (
                Duration,
                Result<(), Box<dyn error::Error + Send + Sync + 'static>>,
            ) {
                todo!()
            }

            /// Returns whether this data source contains any (valid) data.
            fn exists(&self) -> bool {
                todo!()
            }
        }
    }
}