diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-04-06 12:37:14 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2021-04-06 12:37:14 +0200 |
commit | 7d1806f7b09a8b610cfe2cce12795c444fbef8cb (patch) | |
tree | 80473f1aa20e8244abe07367da13dbb68fbb6556 | |
parent | c8580bc601330d3d00bfe492a00464f5c403ccb5 (diff) |
Restructure for use with reqwest
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | librepology/Cargo.toml | 10 | ||||
-rw-r--r-- | librepology/src/lib.rs | 16 | ||||
-rw-r--r-- | librepology/src/v1/api.rs | 19 | ||||
-rw-r--r-- | librepology/src/v1/buffer.rs | 38 | ||||
-rw-r--r-- | librepology/src/v1/error.rs | 6 | ||||
-rw-r--r-- | librepology/src/v1/mod.rs | 3 | ||||
-rw-r--r-- | librepology/src/v1/restapi.rs | 70 | ||||
-rw-r--r-- | librepology/src/v1/types/mod.rs | 37 | ||||
-rw-r--r-- | librepology/src/v1/types/request/mod.rs | 0 | ||||
-rw-r--r-- | librepology/src/v1/types/response/category.rs (renamed from librepology/src/v1/types/category.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/download.rs (renamed from librepology/src/v1/types/download.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/effname.rs (renamed from librepology/src/v1/types/effname.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/license.rs (renamed from librepology/src/v1/types/license.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/maintainer.rs (renamed from librepology/src/v1/types/maintainer.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/mod.rs | 35 | ||||
-rw-r--r-- | librepology/src/v1/types/response/name.rs (renamed from librepology/src/v1/types/name.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/package.rs (renamed from librepology/src/v1/types/package.rs) | 4 | ||||
-rw-r--r-- | librepology/src/v1/types/response/problem.rs (renamed from librepology/src/v1/types/problem.rs) | 4 | ||||
-rw-r--r-- | librepology/src/v1/types/response/repo.rs (renamed from librepology/src/v1/types/repo.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/status.rs (renamed from librepology/src/v1/types/status.rs) | 12 | ||||
-rw-r--r-- | librepology/src/v1/types/response/summary.rs (renamed from librepology/src/v1/types/summary.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/version.rs (renamed from librepology/src/v1/types/version.rs) | 2 | ||||
-rw-r--r-- | librepology/src/v1/types/response/www.rs (renamed from librepology/src/v1/types/www.rs) | 3 |
23 files changed, 56 insertions, 219 deletions
diff --git a/librepology/Cargo.toml b/librepology/Cargo.toml index fcbaa64..c82161c 100644 --- a/librepology/Cargo.toml +++ b/librepology/Cargo.toml @@ -16,14 +16,10 @@ license = "MPL-2.0" maintenance = { status = "actively-developed" } [dependencies] -serde = "1" -serde_derive = "1" +1eqwest = "0.11" +serde = { version = "1", features = ["derive"] } serde_json = "1" +thiserror = "1" url = "1.7" url_serde = "0.2" -thiserror = "1" -log = "0.4" -derive_more = "0.14" -derive-new = "0.5" -curl = "0.4" diff --git a/librepology/src/lib.rs b/librepology/src/lib.rs index 8ca04da..a3a6d96 100644 --- a/librepology/src/lib.rs +++ b/librepology/src/lib.rs @@ -1,17 +1 @@ -extern crate curl; -extern crate serde; -extern crate serde_json; -extern crate thiserror; -extern crate url; -extern crate url_serde; - -#[macro_use] -extern crate serde_derive; -#[macro_use] -extern crate log; -#[macro_use] -extern crate derive_more; -#[macro_use] -extern crate derive_new; - pub mod v1; diff --git a/librepology/src/v1/api.rs b/librepology/src/v1/api.rs deleted file mode 100644 index 1c8cd6e..0000000 --- a/librepology/src/v1/api.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::v1::error::Result; -use crate::v1::types::Package; -use crate::v1::types::Problem; - -/// The high-level functionality of the repology API is represented in this trait -/// -/// Each "functionality" is represented via one function. -/// -/// # Note -/// -/// This is implemented as a _trait_ rather than a _struct_ because this way we can reuse the -/// functionality for operating on a stream, for example on stdin as a source of data. -pub trait Api { - fn project<N: AsRef<str>>(&self, name: N) -> Result<Vec<Package>>; - - fn problems_for_repo<R: AsRef<str>>(&self, repo: R) -> Result<Vec<Problem>>; - - fn problems_for_maintainer<M: AsRef<str>>(&self, maintainer: M) -> Result<Vec<Problem>>; -} diff --git a/librepology/src/v1/buffer.rs b/librepology/src/v1/buffer.rs deleted file mode 100644 index e2eb67a..0000000 --- a/librepology/src/v1/buffer.rs +++ /dev/null @@ -1,38 +0,0 @@ -use std::io::Read; - -use crate::v1::api::Api; -use crate::v1::error::RepologyError as Error; -use crate::v1::error::Result; -use crate::v1::types::Package; -use crate::v1::types::Problem; - -#[derive(Debug)] -pub struct BufferApi { - buf: String, -} - -impl BufferApi { - pub fn read_from<R>(mut input: R) -> Result<BufferApi> - where - R: Read, - { - let mut buf = String::new(); - let read = input.read_to_string(&mut buf)?; - trace!("Read {} bytes from stdin", read); - Ok(BufferApi { buf }) - } -} - -impl Api for BufferApi { - fn project<N: AsRef<str>>(&self, _name: N) -> Result<Vec<Package>> { - serde_json::de::from_str(&self.buf).map_err(Error::from) - } - - fn problems_for_repo<R: AsRef<str>>(&self, _repo: R) -> Result<Vec<Problem>> { - serde_json::de::from_str(&self.buf).map_err(Error::from) - } - - fn problems_for_maintainer<M: AsRef<str>>(&self, _maintainer: M) -> Result<Vec<Problem>> { - serde_json::de::from_str(&self.buf).map_err(Error::from) - } -} diff --git a/librepology/src/v1/error.rs b/librepology/src/v1/error.rs index 79c556e..cbc66c7 100644 --- a/librepology/src/v1/error.rs +++ b/librepology/src/v1/error.rs @@ -2,9 +2,6 @@ pub type Result<T> = std::result::Result<T, RepologyError>; #[derive(thiserror::Error, Debug)] pub enum RepologyError { - #[error("Borrow mutable error")] - BorrowMutErr(#[from] std::cell::BorrowMutError), - #[error("UTF8 Error")] Utf8Error(#[from] std::string::FromUtf8Error), @@ -14,9 +11,6 @@ pub enum RepologyError { #[error("IO error")] IoError(#[from] std::io::Error), - #[error("curl error")] - CurlError(#[from] curl::Error), - #[error("unknown error")] Unknown, } diff --git a/librepology/src/v1/mod.rs b/librepology/src/v1/mod.rs index 781cc0f..5c74364 100644 --- a/librepology/src/v1/mod.rs +++ b/librepology/src/v1/mod.rs @@ -1,5 +1,2 @@ -pub mod api; -pub mod buffer; pub mod error; -pub mod restapi; pub mod types; diff --git a/librepology/src/v1/restapi.rs b/librepology/src/v1/restapi.rs deleted file mode 100644 index 1f68c41..0000000 --- a/librepology/src/v1/restapi.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::result::Result as RResult; - -use curl::easy::Easy2; - -use crate::v1::api::Api; -use crate::v1::error::RepologyError as Error; -use crate::v1::error::Result; -use crate::v1::types::{Package, Problem}; - -/// Private helper type for collecting data from the curl library -struct Collector(Vec<u8>); -impl curl::easy::Handler for Collector { - fn write(&mut self, data: &[u8]) -> RResult<usize, curl::easy::WriteError> { - self.0.extend_from_slice(data); - Ok(data.len()) - } -} - -/// Representational object for the REST Api of repology -pub struct RestApi { - /// Base url - repology: String, -} - -impl RestApi { - pub fn new(repology: String) -> Self { - Self { repology } - } - - /// Helper function for sending a request via the curl library - fn send_request<U: AsRef<str>>(&self, request: U) -> Result<String> { - let mut easy = Easy2::new(Collector(Vec::new())); - easy.get(true)?; - easy.url(request.as_ref())?; - easy.perform()?; - let content = easy.get_ref().0.clone(); // TODO: Ugh... - String::from_utf8(content).map_err(Error::from) - } -} - -impl Api for RestApi { - fn project<N: AsRef<str>>(&self, name: N) -> Result<Vec<Package>> { - let url = format!("{}api/v1/project/{}", self.repology, name.as_ref()); - trace!("Request: {}", url); - let response = self.send_request(url)?; - serde_json::from_str(&response).map_err(Error::from) - } - - fn problems_for_repo<R: AsRef<str>>(&self, repo: R) -> Result<Vec<Problem>> { - let url = format!( - "{}api/v1/repository/{}/problems", - self.repology, - repo.as_ref() - ); - trace!("Request: {}", url); - let response = self.send_request(url)?; - serde_json::from_str(&response).map_err(Error::from) - } - - fn problems_for_maintainer<M: AsRef<str>>(&self, maintainer: M) -> Result<Vec<Problem>> { - let url = format!( - "{}api/v1/maintainer/{}/problems", - self.repology, - maintainer.as_ref() - ); - trace!("Request: {}", url); - let response = self.send_request(url)?; - serde_json::from_str(&response).map_err(Error::from) - } -} diff --git a/librepology/src/v1/types/mod.rs b/librepology/src/v1/types/mod.rs index 434274b..e006218 100644 --- a/librepology/src/v1/types/mod.rs +++ b/librepology/src/v1/types/mod.rs @@ -1,35 +1,2 @@ -//! Module containing all _types_ of data for the API implementation -//! -//! Tne types have no functionality themselves but only represent objects which are returned by theĀ“ -//! repology API. -//! -//! This top-level module exports all types of the submodules publicly. -//! - -mod category; -mod download; -mod effname; -mod license; -mod maintainer; -mod name; -mod package; -mod problem; -mod repo; -mod status; -mod summary; -mod version; -mod www; - -pub use category::Category; -pub use download::Download; -pub use effname::EffName; -pub use license::License; -pub use maintainer::Maintainer; -pub use name::Name; -pub use package::Package; -pub use problem::Problem; -pub use repo::Repo; -pub use status::Status; -pub use summary::Summary; -pub use version::Version; -pub use www::Www; +pub mod request; +pub mod response; diff --git a/librepology/src/v1/types/request/mod.rs b/librepology/src/v1/types/request/mod.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/librepology/src/v1/types/request/mod.rs diff --git a/librepology/src/v1/types/category.rs b/librepology/src/v1/types/response/category.rs index ad118d0..12e837a 100644 --- a/librepology/src/v1/types/category.rs +++ b/librepology/src/v1/types/response/category.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // list of package categories -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Category(String); impl Deref for Category { diff --git a/librepology/src/v1/types/download.rs b/librepology/src/v1/types/response/download.rs index e4d64d1..7c28451 100644 --- a/librepology/src/v1/types/download.rs +++ b/librepology/src/v1/types/response/download.rs @@ -3,7 +3,7 @@ use std::ops::Deref; use url::Url; // list of package downloads -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Clone, Debug, serde::Deserialize)] pub struct Download(#[serde(with = "url_serde")] Url); impl Deref for Download { diff --git a/librepology/src/v1/types/effname.rs b/librepology/src/v1/types/response/effname.rs index d940b63..667c69f 100644 --- a/librepology/src/v1/types/effname.rs +++ b/librepology/src/v1/types/response/effname.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // list of package downloads -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct EffName(String); impl Deref for EffName { diff --git a/librepology/src/v1/types/license.rs b/librepology/src/v1/types/response/license.rs index 3c5fe4f..4df86d0 100644 --- a/librepology/src/v1/types/license.rs +++ b/librepology/src/v1/types/response/license.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // list of package licenses -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct License(String); impl Deref for License { diff --git a/librepology/src/v1/types/maintainer.rs b/librepology/src/v1/types/response/maintainer.rs index 4dbf506..a9d2b28 100644 --- a/librepology/src/v1/types/maintainer.rs +++ b/librepology/src/v1/types/response/maintainer.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // list of package maintainers -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Maintainer(String); impl Deref for Maintainer { diff --git a/librepology/src/v1/types/response/mod.rs b/librepology/src/v1/types/response/mod.rs new file mode 100644 index 0000000..a3902ad --- /dev/null +++ b/librepology/src/v1/types/response/mod.rs @@ -0,0 +1,35 @@ +//! Module containing all _types_ of data for the API implementation +//! +//! Tne types have no functionality themselves but only represent objects which are returned by theĀ“ +//! repology API. +//! +//! This top-level module exports all types of the submodules publicly. +//! + +mod category; +mod download; +mod effname; +mod license; +mod maintainer; +mod name; +mod package; +mod problem; +mod repo; +mod status; +mod summary; +mod version; +mod www; + +pub use crate::v1::types::response::category::Category; +pub use crate::v1::types::response::download::Download; +pub use crate::v1::types::response::effname::EffName; +pub use crate::v1::types::response::license::License; +pub use crate::v1::types::response::maintainer::Maintainer; +pub use crate::v1::types::response::name::Name; +pub use crate::v1::types::response::package::Package; +pub use crate::v1::types::response::problem::Problem; +pub use crate::v1::types::response::repo::Repo; +pub use crate::v1::types::response::status::Status; +pub use crate::v1::types::response::summary::Summary; +pub use crate::v1::types::response::version::Version; +pub use crate::v1::types::response::www::Www; diff --git a/librepology/src/v1/types/name.rs b/librepology/src/v1/types/response/name.rs index 0097f99..4a79c0f 100644 --- a/librepology/src/v1/types/name.rs +++ b/librepology/src/v1/types/response/name.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // package name as in repository (if different from version) -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Name(String); impl Deref for Name { diff --git a/librepology/src/v1/types/package.rs b/librepology/src/v1/types/response/package.rs index 1286b15..c1e2bab 100644 --- a/librepology/src/v1/types/package.rs +++ b/librepology/src/v1/types/response/package.rs @@ -1,6 +1,6 @@ -use crate::v1::types::*; +use crate::v1::types::response::*; -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize)] +#[derive(Eq, PartialEq, Clone, Debug, serde::Deserialize)] pub struct Package { /// name of repository for this package repo: Repo, diff --git a/librepology/src/v1/types/problem.rs b/librepology/src/v1/types/response/problem.rs index 60c7cc4..309fe3d 100644 --- a/librepology/src/v1/types/problem.rs +++ b/librepology/src/v1/types/response/problem.rs @@ -1,6 +1,6 @@ -use crate::v1::types::*; +use crate::v1::types::response::*; -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize)] +#[derive(Eq, PartialEq, Clone, Debug, serde::Deserialize)] pub struct Problem { #[serde(rename = "repo")] repo: Repo, diff --git a/librepology/src/v1/types/repo.rs b/librepology/src/v1/types/response/repo.rs index e5ddc87..5354676 100644 --- a/librepology/src/v1/types/repo.rs +++ b/librepology/src/v1/types/response/repo.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // name of repository for this package -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Repo(String); impl Deref for Repo { diff --git a/librepology/src/v1/types/status.rs b/librepology/src/v1/types/response/status.rs index 726359e..ff7c307 100644 --- a/librepology/src/v1/types/status.rs +++ b/librepology/src/v1/types/response/status.rs @@ -1,43 +1,33 @@ // package status -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, Display)] +#[derive(Eq, PartialEq, Clone, Debug, serde::Deserialize)] pub enum Status { #[serde(rename = "newest")] - #[display(fmt = "newest")] Newest, #[serde(rename = "devel")] - #[display(fmt = "devel")] Devel, #[serde(rename = "unique")] - #[display(fmt = "unique")] Unique, #[serde(rename = "outdated")] - #[display(fmt = "outdated")] Outdated, #[serde(rename = "legacy")] - #[display(fmt = "legacy")] Legacy, #[serde(rename = "rolling")] - #[display(fmt = "rolling")] Rolling, #[serde(rename = "noscheme")] - #[display(fmt = "noscheme")] Noscheme, #[serde(rename = "incorrect")] - #[display(fmt = "incorrect")] Incorrect, #[serde(rename = "untrusted")] - #[display(fmt = "untrusted")] Untrusted, #[serde(rename = "ignored")] - #[display(fmt = "ignored")] Ignored, } diff --git a/librepology/src/v1/types/summary.rs b/librepology/src/v1/types/response/summary.rs index b1fc874..00617ca 100644 --- a/librepology/src/v1/types/summary.rs +++ b/librepology/src/v1/types/response/summary.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // one-line description of the package -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Summary(String); impl Deref for Summary { diff --git a/librepology/src/v1/types/version.rs b/librepology/src/v1/types/response/version.rs index 2350316..fddeee5 100644 --- a/librepology/src/v1/types/version.rs +++ b/librepology/src/v1/types/response/version.rs @@ -1,7 +1,7 @@ use std::ops::Deref; // package version (sanitized) -#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Debug, serde::Deserialize)] pub struct Version(String); impl Deref for Version { diff --git a/librepology/src/v1/types/www.rs b/librepology/src/v1/types/response/www.rs index 04507a7..bf7fbe8 100644 --- a/librepology/src/v1/types/www.rs +++ b/librepology/src/v1/types/response/www.rs @@ -1,8 +1,9 @@ use std::ops::Deref; + use url::Url; // list of package webpages -#[derive(Eq, PartialEq, Clone, Debug, Serialize, Deserialize, new)] +#[derive(Eq, PartialEq, Clone, Debug, serde::Deserialize)] pub struct Www(#[serde(with = "url_serde")] Url); impl Deref for Www { |