nvrs/
lib.rs

1//! nvrs - fast new version checker for software releases 🚦🦀
2//!
3//! <div class="warning">
4//!
5//! nvrs is still a WIP
6//!
7//! new features & bugfixes are being pushed every day
8//!
9//! you may encounter some issues. please consider [submitting feedback](https://github.com/adamperkowski/nvrs/issues/new/choose) if you do.
10//!
11//! </div>
12
13pub mod api;
14pub mod config;
15pub mod error;
16pub mod keyfile;
17pub mod verfiles;
18
19/// example "core" struct that holds all the necessary data for the library
20///
21/// # example usage
22/// ```rust
23/// # tokio_test::block_on(async {
24/// use nvrs::*;
25///
26/// let config = config::load(&None).await.unwrap();
27/// let verfiles = verfiles::load(&config.0.__config__).await.unwrap();
28/// let keyfile = keyfile::load(&config.0.__config__).await.unwrap();
29///
30/// Core {
31///     config,
32///     verfiles,
33///     client: reqwest::Client::new(),
34///     keyfile,
35/// };
36/// # })
37/// ```
38pub struct Core {
39    /// configuration file content and path
40    pub config: (config::Config, std::path::PathBuf),
41    /// verfiles contents
42    pub verfiles: (verfiles::Verfile, verfiles::Verfile),
43    /// reqwest HTTP client
44    pub client: reqwest::Client,
45    /// keyfile contents
46    pub keyfile: Option<keyfile::Keyfile>,
47}
48
49/// an asynchronous function that package's source and gets the latest release
50/// # example usage
51/// ```rust
52/// # tokio_test::block_on(async {
53/// use nvrs::{run_source, config};
54///
55/// let package_name = "nvrs".to_string();
56/// let package = config::Package::new("github".to_string(), "adamperkowski/nvrs".to_string(), false, "v".to_string()).unwrap();
57///
58/// let client = reqwest::Client::new();
59///
60/// run_source((package_name, package), client, None).await;
61/// # })
62/// ```
63/// see [crate::config::Package] for `package`
64pub async fn run_source(
65    package: (String, config::Package),
66    client: reqwest::Client,
67    keyfile: Option<keyfile::Keyfile>,
68) -> error::Result<api::Release> {
69    let (source, api_args) = package.1.get_api();
70
71    if let Some(api) = api::API_LIST.iter().find(|a| a.name == source) {
72        let api_key = if let Some(keyfile_content) = keyfile {
73            keyfile_content.get_key(api.name).await
74        } else {
75            String::new()
76        };
77
78        let args = api::ApiArgs {
79            request_client: client,
80            package: package.0,
81            use_max_tag: package.1.use_max_tag,
82            args: api_args,
83            api_key,
84        };
85
86        Ok((api.func)(args).await?)
87    } else {
88        Err(error::Error::SourceNotFound(source.to_string()))
89    }
90}