github_workflow_update/updater/
mod.rs

1// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4
5//! The resolver can deal with different upstream API's by using the
6//! [`Upd`] trait, which is generic over the currently supported
7//! [`docker`] and [`github`] upstreams.
8
9use async_trait::async_trait;
10
11use crate::entity::Entity;
12use crate::error::Error;
13use crate::error::Result;
14use crate::version::Version;
15
16#[async_trait]
17pub trait Updater: std::fmt::Debug {
18    fn url(&self, resource: &str) -> Option<String>;
19    async fn get_versions(&self, url: &str) -> Result<Vec<Version>>;
20    fn updated_line(&self, entity: &Entity) -> Option<String>;
21}
22
23pub mod docker;
24pub mod github;
25
26#[derive(Debug)]
27pub enum Upd {
28    Docker(docker::Docker),
29    Github(github::Github),
30}
31
32#[async_trait]
33impl Updater for Upd {
34    fn url(&self, resource: &str) -> Option<String> {
35        match self {
36            Upd::Docker(i) => i.url(resource),
37            Upd::Github(i) => i.url(resource),
38        }
39    }
40    async fn get_versions(&self, url: &str) -> Result<Vec<Version>> {
41        match self {
42            Upd::Docker(i) => i.get_versions(url),
43            Upd::Github(i) => i.get_versions(url),
44        }
45        .await
46    }
47    fn updated_line(&self, entity: &Entity) -> Option<String> {
48        match self {
49            Upd::Docker(i) => i.updated_line(entity),
50            Upd::Github(i) => i.updated_line(entity),
51        }
52    }
53}
54
55pub fn updater_for(resource: &str) -> Result<impl Updater> {
56    if let Some(_url) = docker::url(resource) {
57        Ok(Upd::Docker(docker::Docker::default()))
58    } else if let Some(_url) = github::url(resource) {
59        Ok(Upd::Github(github::Github::default()))
60    } else {
61        Err(Error::UpdaterNotFound(resource.into()))
62    }
63}