From 0e582cc2d5ca97e010e5cb6b743b990ebaf12f2e Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 9 Oct 2022 21:28:51 +0200 Subject: [PATCH 1/3] Fix compilation with default features disabled http is a dependency of reqwest and provides just HTTP types. --- Cargo.toml | 1 + src/providers.rs | 2 +- src/request.rs | 10 +++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d6732fc..96595801 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ documentation = "https://docs.rs/urlshortener" travis-ci = { repository = "vityafx/urlshortener-rs" } [dependencies] +http = "0.2" url = "2" [dependencies.reqwest] diff --git a/src/providers.rs b/src/providers.rs index 672fbaa5..f8e56ab8 100644 --- a/src/providers.rs +++ b/src/providers.rs @@ -1,7 +1,7 @@ //! Library service providers implementation. use crate::request as req; -use reqwest::header::HeaderMap; +use http::header::HeaderMap; use url::form_urlencoded; /// A user agent for faking weird services. diff --git a/src/request.rs b/src/request.rs index 803d70a8..c158ded0 100644 --- a/src/request.rs +++ b/src/request.rs @@ -1,10 +1,10 @@ +use http::header; #[cfg(feature = "client")] -use reqwest::{ - blocking::{Client, Response}, - header::{self, HeaderMap}, -}; +use reqwest::blocking::{Client, Response}; +#[cfg(feature = "client")] const CONTENT_JSON: &str = "application/json"; +#[cfg(feature = "client")] const CONTENT_FORM_URL_ENCODED: &str = "application/x-www-form-urlencoded"; /// An HTTP method abstraction @@ -41,7 +41,7 @@ pub struct Request { /// The user agent. pub user_agent: Option, /// Request headers. - pub headers: Option, + pub headers: Option, /// The HTTP method. pub method: Method, } From ed2cf9bed2f40e6239f0de2defc66cde213d2445 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 9 Oct 2022 21:32:23 +0200 Subject: [PATCH 2/3] Fix 'cargo test' with default features disabled --- Cargo.toml | 19 ++++++++++++++ src/lib.rs | 73 +++++++++++++++++++++++++++++------------------------- 2 files changed, 58 insertions(+), 34 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 96595801..5b7f6a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,22 @@ optional = true [features] default = ["client"] client = ["reqwest"] + +[[example]] +name = "via_all_providers" +required-features = ["client"] + +[[example]] +name = "via_auth_provider" +required-features = ["client"] + +[[example]] +name = "via_kutt_custom_host_provider" +required-features = ["client"] + +[[example]] +name = "via_single_provider" +required-features = ["client"] + +[package.metadata.docs.rs] +all-features = true diff --git a/src/lib.rs b/src/lib.rs index 4b04ba5e..1cd91fa4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,41 +1,46 @@ +#![deny(missing_docs)] +#![deny(warnings)] //! # urlshortener //! //! An easy library for retrieving short urls. //! -//! ## Usage -//! -//! Creating a short URL via a specified provider is very simple: -//! -//! ```rust,no_run -//! use urlshortener::{providers::Provider, client::UrlShortener}; -//! -//! let us = UrlShortener::new().unwrap(); -//! let short_url = us.generate("https://my-long-url.com", &Provider::IsGd); -//! assert!(short_url.is_ok()); -//! ``` -//! -//! Or attempting all URL shorteners until one is successfully generated: -//! -//! ```rust,no_run -//! use urlshortener::client::UrlShortener; -//! -//! let us = UrlShortener::new().unwrap(); -//! let short_url = us.try_generate("https://my-long-url.com", None); -//! assert!(short_url.is_ok()); -//! ``` -//! In order to use service with authentication use the appropriate provider directly: -//! -//! ```rust,no_run -//! use urlshortener::{ client::UrlShortener, providers::Provider }; -//! -//! let us = UrlShortener::new().unwrap(); -//! let key = "MY_API_KEY"; -//! let short_url = us.generate("https://my-long-url.com", &Provider::GooGl { api_key: -//! key.to_owned() }); -//! assert!(short_url.is_ok()); -//! ``` -#![deny(missing_docs)] -#![deny(warnings)] +#[cfg_attr( + feature = "client", + doc = r##" +## Usage + +Creating a short URL via a specified provider is very simple: + +```rust,no_run +use urlshortener::{providers::Provider, client::UrlShortener}; + +let us = UrlShortener::new().unwrap(); +let short_url = us.generate("https://my-long-url.com", &Provider::IsGd); +assert!(short_url.is_ok()); +``` + +Or attempting all URL shorteners until one is successfully generated: + +```rust,no_run +use urlshortener::client::UrlShortener; + +let us = UrlShortener::new().unwrap(); +let short_url = us.try_generate("https://my-long-url.com", None); +assert!(short_url.is_ok()); +``` +In order to use service with authentication use the appropriate provider directly: + +```rust,no_run +use urlshortener::{ client::UrlShortener, providers::Provider }; + +let us = UrlShortener::new().unwrap(); +let key = "MY_API_KEY"; +let short_url = us.generate("https://my-long-url.com", &Provider::GooGl { api_key: +key.to_owned() }); +assert!(short_url.is_ok()); +``` +"## +)] /// A urlshortener http client for performing requests. #[cfg(feature = "client")] From 4a74cd9d188c231d2747816f12afffe5c4e881ca Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 9 Oct 2022 21:40:40 +0200 Subject: [PATCH 3/3] CI: Build and test with default features disabled --- .github/workflows/ci.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8f990c41..561cc939 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,6 +20,10 @@ jobs: - macOS - Windows + cargo-opts: + - --all-features + - --no-default-features + include: - name: beta toolchain: beta @@ -52,13 +56,9 @@ jobs: target key: ${{ runner.os }}-test-${{ steps.tc.outputs.rustc_hash }}-${{ hashFiles('**/Cargo.toml') }} - - name: Build all features - if: matrix.features == '' - run: cargo build --all-features + - run: cargo build ${{ matrix.cargo-opts }} - - name: Test all features - if: matrix.features == '' - run: cargo test --all-features + - run: cargo test ${{ matrix.cargo-opts }} clippy: name: Run clippy