diff options
author | 2024-02-11 21:36:24 -0800 | |
---|---|---|
committer | 2024-02-11 21:36:24 -0800 | |
commit | 362efc5673c36f286ded953aefe95ea30a2a5400 (patch) | |
tree | b7c24c9d2182907fabb06621f76f0be99ccb2f00 | |
parent | 6086e450c72c1877287e156f3fe5f9194cc08e52 (diff) | |
parent | c4918fcf71a7a32b9973cefde63071b925355b1c (diff) | |
download | trakt-362efc5673c36f286ded953aefe95ea30a2a5400.tar.gz trakt-362efc5673c36f286ded953aefe95ea30a2a5400.tar.zst trakt-362efc5673c36f286ded953aefe95ea30a2a5400.zip |
Merge pull request #1 from ansg191/ansg191/ci
-rw-r--r-- | .github/workflows/pre-commit.yml | 22 | ||||
-rw-r--r-- | .github/workflows/rust.yml | 87 | ||||
-rw-r--r-- | .pre-commit-config.yaml | 19 | ||||
-rw-r--r-- | .rustfmt.toml | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | trakt/src/response.rs | 12 | ||||
-rw-r--r-- | trakt/src/smo.rs | 9 | ||||
-rw-r--r-- | trakt/src/url.rs | 22 | ||||
-rw-r--r-- | trakt/src/utils.rs | 2 | ||||
-rw-r--r-- | trakt_macros/src/lib.rs | 2 | ||||
-rw-r--r-- | trakt_macros/src/paginated.rs | 3 | ||||
-rw-r--r-- | trakt_macros/src/request.rs | 5 |
12 files changed, 146 insertions, 41 deletions
diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 0000000..89f951a --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,22 @@ +name: pre-commit + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + pre-commit: + name: Pre-commit + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + + - name: Install Rust + uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt, clippy + + - uses: pre-commit/action@v3.0.1 diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 5812a82..cb275b6 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -12,35 +12,64 @@ env: jobs: test: + name: Test + strategy: + matrix: + os: [ ubuntu-latest, macos-latest, windows-latest ] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v4 + + - name: Cache Cargo Dependencies + uses: actions/cache@v4 + with: + path: | + - ~/.cargo/bin + - ~/.cargo/registry/index/ + - ~/.cargo/registry/cache/ + - ~/.cargo/git/db/ + key: ${{ runner.OS }}-cargo-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.OS }}-cargo- + + - name: Cache build output dependencies + uses: actions/cache@v4 + with: + path: target + key: ${{ runner.OS }}-build-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.OS }}-build- + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + + - name: Run tests + run: cargo test + + fmt: + name: Rustfmt runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Install Rust + uses: dtolnay/rust-toolchain@nightly + with: + components: rustfmt + - name: Run rustfmt + run: cargo fmt --all -- --check + + clippy: + name: Clippy + runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - - name: Cache Cargo Dependencies - uses: actions/cache@v4 - with: - path: | - - ~/.cargo/bin - - ~/.cargo/registry/index/ - - ~/.cargo/registry/cache/ - - ~/.cargo/git/db/ - key: ${{ runner.OS }}-cargo-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.OS }}-cargo- - - - name: Cache build output dependencies - uses: actions/cache@v4 - with: - path: target - key: ${{ runner.OS }}-build-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.OS }}-build- - - - name: Install Rust - uses: dtolnay/rust-toolchain@master - with: - toolchain: stable - - - name: Run tests - run: cargo test + - uses: actions/checkout@v4 + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: clippy + + - name: Run clippy + run: cargo clippy --all-targets --all-features diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4eeda62 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-added-large-files + - id: check-case-conflict + - id: check-merge-conflict + - id: check-symlinks + - id: check-toml + - id: forbid-submodules + - id: mixed-line-ending + - repo: https://github.com/ansg191/pre-commit-rust + rev: v1.1 + hooks: + - id: fmt + - id: cargo-check + - id: clippy diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..455c820 --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +imports_granularity = "Crate" +group_imports = "StdExternalCrate" @@ -1,3 +1,3 @@ [workspace] members = ["trakt", "trakt_macros"] -resolver = "2"
\ No newline at end of file +resolver = "2" diff --git a/trakt/src/response.rs b/trakt/src/response.rs index 7c714af..ddb10f3 100644 --- a/trakt/src/response.rs +++ b/trakt/src/response.rs @@ -1,6 +1,18 @@ use crate::error::ApiError; pub trait Response: Sized { + /// Converts an HTTP response into a result of `Self`, where `Self` refers to the implementing type. + /// + /// # Arguments + /// + /// * `response` - The HTTP response to convert. + /// + /// # Errors + /// + /// Will error if the HTTP response is not a succeeding status code as determined by the + /// type or if the response body cannot be deserialized into the implementing type. + /// + /// See [`FromHttpError`] for more details. fn try_from_http_response<T: AsRef<[u8]>>( response: http::Response<T>, ) -> Result<Self, FromHttpError>; diff --git a/trakt/src/smo.rs b/trakt/src/smo.rs index cb25964..69d1fdf 100644 --- a/trakt/src/smo.rs +++ b/trakt/src/smo.rs @@ -98,6 +98,15 @@ impl TwoLetter { Self(bytes) } + /// Create a `TwoLetter` from bytes without checking if the bytes are valid UTF-8 + /// + /// # Arguments + /// + /// * `bytes`: Bytes to convert to a `TwoLetter` + /// + /// # Safety + /// + /// The bytes must be valid UTF-8. #[must_use] pub const unsafe fn from_bytes_unchecked(bytes: [u8; 2]) -> Self { Self(bytes) diff --git a/trakt/src/url.rs b/trakt/src/url.rs index 07d2a82..03008dc 100644 --- a/trakt/src/url.rs +++ b/trakt/src/url.rs @@ -712,9 +712,18 @@ mod tests { ] ); - assert_eq!(parse_endpoint("/shows/{{id}}").unwrap_err(), Error::InvalidEndpoint); - assert_eq!(parse_endpoint("/shows/{id}}").unwrap_err(), Error::InvalidEndpoint); - assert_eq!(parse_endpoint("/shows/{id").unwrap_err(), Error::InvalidEndpoint); + assert_eq!( + parse_endpoint("/shows/{{id}}").unwrap_err(), + Error::InvalidEndpoint + ); + assert_eq!( + parse_endpoint("/shows/{id}}").unwrap_err(), + Error::InvalidEndpoint + ); + assert_eq!( + parse_endpoint("/shows/{id").unwrap_err(), + Error::InvalidEndpoint + ); } #[test] @@ -732,9 +741,12 @@ mod tests { let base_url = "https://example.com"; let endpoint = "/shows/{id}"; let params = Params { id: 1 }; - let query = Query { page: 1, limit: None }; + let query = Query { + page: 1, + limit: None, + }; let url = construct_url(base_url, endpoint, ¶ms, &query).unwrap(); assert_eq!(url, "https://example.com/shows/1?page=1"); } -}
\ No newline at end of file +} diff --git a/trakt/src/utils.rs b/trakt/src/utils.rs index a355e8c..ff813f2 100644 --- a/trakt/src/utils.rs +++ b/trakt/src/utils.rs @@ -35,7 +35,7 @@ pub struct PaginationResponse<T> { } impl<T> PaginationResponse<T> { - pub fn from_headers(items: Vec<T>, map: &HeaderMap) -> Result<Self, DeserializeError> { + pub(crate) fn from_headers(items: Vec<T>, map: &HeaderMap) -> Result<Self, DeserializeError> { let current_page = parse_from_header(map, "X-Pagination-Page")?; let items_per_page = parse_from_header(map, "X-Pagination-Limit")?; let total_pages = parse_from_header(map, "X-Pagination-Page-Count")?; diff --git a/trakt_macros/src/lib.rs b/trakt_macros/src/lib.rs index cb3a49a..5dcb4e7 100644 --- a/trakt_macros/src/lib.rs +++ b/trakt_macros/src/lib.rs @@ -1,5 +1,5 @@ -mod request; mod paginated; +mod request; use proc_macro::TokenStream; diff --git a/trakt_macros/src/paginated.rs b/trakt_macros/src/paginated.rs index 79880ea..d9aa2fd 100644 --- a/trakt_macros/src/paginated.rs +++ b/trakt_macros/src/paginated.rs @@ -1,8 +1,7 @@ use proc_macro::TokenStream; use proc_macro2::Span; use quote::quote; -use syn::spanned::Spanned; -use syn::{parse_macro_input, DeriveInput}; +use syn::{parse_macro_input, spanned::Spanned, DeriveInput}; pub fn derive_paginated(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); diff --git a/trakt_macros/src/request.rs b/trakt_macros/src/request.rs index ad00b89..3fc7a81 100644 --- a/trakt_macros/src/request.rs +++ b/trakt_macros/src/request.rs @@ -1,8 +1,9 @@ use proc_macro::TokenStream; use proc_macro2::{Ident, Span}; use quote::{format_ident, quote}; -use syn::punctuated::Punctuated; -use syn::{parse_macro_input, DeriveInput, Field, Fields, LitStr, Token, Type}; +use syn::{ + parse_macro_input, punctuated::Punctuated, DeriveInput, Field, Fields, LitStr, Token, Type, +}; pub fn derive_request(input: TokenStream) -> TokenStream { let input = parse_macro_input!(input as DeriveInput); |