Skip to content

Commit

Permalink
refact(codebase)!: move internal logic to lib (#4)
Browse files Browse the repository at this point in the history
This refactors **the whole codebase** and makes some additions, for example:
- better documentation
- performance improvements
- error handling
- integration & unit tests
- other stuff i forgot about

---------

Signed-off-by: Adam Perkowski <adas1per@protonmail.com>
  • Loading branch information
adamperkowski authored Nov 24, 2024
1 parent f2e22b6 commit c0021f0
Show file tree
Hide file tree
Showing 19 changed files with 900 additions and 512 deletions.
20 changes: 18 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ on:
branches: ["main"]
paths:
- '**/*.rs'
- 'src/**'
- 'src/**/*'
- 'Cargo.toml'
- 'Cargo.lock'
pull_request:
paths:
- '**/*.rs'
- 'src/**'
- 'src/**/*'
- 'Cargo.toml'
- 'Cargo.lock'
workflow_dispatch:

env:
CARGO_TERM_COLOR: always

jobs:
check:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -45,3 +48,16 @@ jobs:
cargo build --verbose
cargo build --release --verbose
test:
runs-on: ubuntu-latest
needs: build

steps:
- uses: actions/checkout@v4

- name: Test
run: |
cargo test --lib
cargo test --lib --release
cargo test --doc
cargo test --doc --release
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
newver.json
oldver.json
keyfile.toml
*_old
*.old
2 changes: 2 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Thank you for considering contributing to [nvrs](https://github.com/adamperkowski/nvrs) ❤️

If, while viewing the code, you find any parts unclear or unexplained, please [open an issue](https://github.com/adamperkowski/nvrs/issues/new/choose) with a documentation request.

Note that we have a [Code of Conduct](./CODE_OF_CONDUCT.md). Please follow it in all your interactions with the project.

## Workflow
Expand Down
71 changes: 69 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 12 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nvrs"
version = "0.1.3"
version = "0.1.4-pre1"
authors = ["Adam Perkowski <adas1per@protonmail.com>"]
license = "MIT"
description = "🚦 fast new version checker for software releases 🦀"
Expand All @@ -10,6 +10,7 @@ categories = ["command-line-interface", "command-line-utilities"]
edition = "2021"
include = [
"**/*.rs",
"src/**/*",
"Cargo.toml",
"README.md",
"LICENSE",
Expand All @@ -19,22 +20,26 @@ include = [
]

[features]
default = ["aur", "github", "gitlab"]
aur = ["reqwest"]
github = ["reqwest"]
gitlab = ["reqwest"]
default = ["http", "aur", "github", "gitlab"]
http = ["reqwest"]
aur = ["http"]
github = ["http"]
gitlab = ["http"]

[dependencies]
clap = { version = "4.5.21", features = ["derive", "color", "error-context", "help", "std", "usage"], default-features = false }
colored = "2.1.0"
futures = "0.3.31"
lazy_static = "1.5.0"
reqwest = { version = "0.12.9", features = ["__tls", "charset", "default-tls", "h2", "http2", "json"], default-features = false, optional = true }
serde = { version = "1.0.215", features = ["derive"] }
serde = { version = "1.0.215", features = ["derive"], default-features = false }
serde_json = "1.0.132"
thiserror = "2.0.3"
tokio = { version = "1.41.1", features = ["full"] }
toml = { version = "0.8.19", features = ["parse", "display"], default-features = false }

[dev-dependencies]
tokio-test = "0.4.4"

[profile.release]
lto = "fat"
codegen-units = 1
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
🚦 fast new version checker for software releases 🦀<br>
[nvchecker](https://github.com/lilydjwg/nvchecker) rewritten in Rust

![Build Status](https://img.shields.io/github/actions/workflow/status/adamperkowski/nvrs/rust.yml?style=for-the-badge&labelColor=%23a8127d&color=%23336795) ![docs.rs](https://img.shields.io/docsrs/nvrs?style=for-the-badge&labelColor=%23a8127d&color=%23336795)<br>
![GitHub Contributors](https://img.shields.io/github/contributors-anon/adamperkowski/nvrs?style=for-the-badge&labelColor=%23a8127d&color=%23336795) ![GitHub Repo Size](https://img.shields.io/github/repo-size/adamperkowski/nvrs?style=for-the-badge&labelColor=%23a8127d&color=%23336795) ![Repo Created At](https://img.shields.io/github/created-at/adamperkowski/nvrs?style=for-the-badge&labelColor=%23a8127d&color=%23336795)

![banner](/banner.webp)
Expand All @@ -22,7 +23,7 @@ you may encounter some issues. please consider [submitting feedback](https://git

| command | time per **updated** package | details |
|---------------|------------------------------|--------------------------------------------------------|
| `nvrs` | ~ 0.04s | **API requests included**<br>depends on internet speed |
| `nvrs` | ~ 0.03s | **API requests included**<br>depends on internet speed |
| `nvrs --cmp` | ~ 0.0008s | depends on disk speed |
| `nvrs --take` | ~ 0.001s | depends on disk speed |

Expand Down
6 changes: 6 additions & 0 deletions n_keyfile.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# nvrs
# https://github.com/adamperkowski/nvrs

# this is an example key configuration file
# see `nvrs.toml`

[keys]
# github = "[REDACTED]"
# gitlab = "[REDACTED]"
3 changes: 3 additions & 0 deletions nvrs.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# nvrs
# https://github.com/adamperkowski/nvrs

# this is an example configuration file for nvrs
# use `use_latest_release = true` for maximum nvchecker compatibility

[__config__]
oldver = "oldver.json"
newver = "newver.json"
Expand Down
82 changes: 44 additions & 38 deletions src/api/aur.rs
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
use reqwest::{
header::{HeaderMap, HeaderValue, USER_AGENT},
StatusCode,
};
use crate::{api, error};

pub fn get_latest(package: String, _: Vec<String>, _: String) -> crate::api::ReleaseFuture {
#[derive(serde::Deserialize)]
struct AURResponse {
results: Vec<AURResult>,
}

#[allow(non_snake_case)]
#[derive(serde::Deserialize)]
struct AURResult {
Version: String,
}

pub fn get_latest(args: api::ApiArgs) -> api::ReleaseFuture {
Box::pin(async move {
let url = format!("https://aur.archlinux.org/rpc/v5/info/{}", package);
let mut headers = HeaderMap::new();
headers.insert(USER_AGENT, HeaderValue::from_static("nvrs"));
let client = reqwest::Client::new();

let result = client.get(url).headers(headers).send().await.unwrap();

match result.status() {
StatusCode::OK => (),
status => {
crate::custom_error("GET request didn't return 200", format!("\n{}", status), "");
return None;
}
}
let url = format!("https://aur.archlinux.org/rpc/v5/info/{}", args.args[0]);
let client = args.request_client;

let result = client.get(url).headers(api::setup_headers()).send().await?;
api::match_statuscode(&result.status(), args.package.clone())?;

let json: serde_json::Value = result.json().await.unwrap();
let first_result = json.get("results").unwrap().get(0).unwrap();

Some(crate::api::Release {
tag_name: first_result
.get("Version")
.unwrap()
.to_string()
.split('-')
.next()
.unwrap_or("")
.replace("\"", "")
.to_string(),
html_url: first_result
.get("URL")
.unwrap()
.to_string()
.replace("\"", ""),
})
let json: AURResponse = result.json().await?;

if let Some(first) = json.results.first() {
let version = first.Version.split_once('-').unwrap();

Ok(api::Release {
name: version.0.to_string(),
tag: None,
url: String::new(),
})
} else {
Err(error::Error::NoVersion(args.package))
}
})
}

#[tokio::test]
async fn request_test() {
let package = "permitter".to_string();
let args = api::ApiArgs {
package: package.clone(),
args: vec![package],
api_key: String::new(),
request_client: reqwest::Client::new(),
};

assert!(get_latest(args).await.is_ok());
}
Loading

0 comments on commit c0021f0

Please sign in to comment.