diff options
Diffstat (limited to 'src/data/managers.rs')
-rw-r--r-- | src/data/managers.rs | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/src/data/managers.rs b/src/data/managers.rs new file mode 100644 index 0000000..71a9484 --- /dev/null +++ b/src/data/managers.rs @@ -0,0 +1,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!() + } + } + } +} |