diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eccf03..ccb71f7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +# 0.3.0 - update to Rust 2021 edition + switch to using the tracing crate + +* 0b9c91d - chore: switch to using the tracing crate (Ronald Holshausen, Wed Jun 14 10:19:49 2023 +1000) +* e5b7dcf - chore: Bump version and update to Rust 2021 edition (Ronald Holshausen, Wed Jun 14 10:05:08 2023 +1000) +* 6cafea1 - bump version to 0.2.3 (Ronald Holshausen, Mon Jan 4 11:20:40 2021 +1100) + # 0.2.2 - Update crates to latest * 3d3cfb1 - chore: upgrade crates to latest (including hyper to 0.14) (Ronald Holshausen, Mon Jan 4 11:16:04 2021 +1100) diff --git a/Cargo.toml b/Cargo.toml index cc145cc..6f46d86 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "webmachine-rust" -version = "0.2.3" +version = "0.3.0" authors = ["Ronald Holshausen "] description = "Port of the Ruby Webmachine library to Rust" documentation = "http://www.pact.io/reference/rust/webmachine-rust-0.1.0/webmachine_rust/" @@ -9,20 +9,20 @@ repository = "https://github.com/uglyog/webmachine-rust" readme = "README.md" keywords = ["webmachine"] license = "MIT" -edition = "2018" +edition = "2021" [dependencies] -log = "0.4.8" -maplit = "1.0.1" -itertools = "0.10.0" +chrono = "0.4.26" +futures = "0.3.28" +hex = "0.4.3" +http = "0.2.9" +hyper = { version = "0.14.26", features = ["full"] } +itertools = "0.10.5" lazy_static = "1.4.0" -chrono = "0.4.15" -serde = "1.0.98" -serde_json = "1.0.40" -http = "0.2.1" -hex = "0.4.2" -hyper = { version = "0.14", features = ["full"] } -futures = "0.3.5" +maplit = "1.0.2" +serde = "1.0.163" +serde_json = "1.0.96" +tracing = "0.1.37" [dev-dependencies] expectest = "0.12.0" diff --git a/src/context.rs b/src/context.rs index e98c3b1..fb0e3a1 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,11 +1,14 @@ //! The `context` module encapsulates the context of the environment that the webmachine is //! executing in. Basically wraps the request and response. -use std::collections::{HashMap, BTreeMap}; -use crate::headers::HeaderValue; +use std::collections::{BTreeMap, HashMap}; + use chrono::{DateTime, FixedOffset}; +use maplit::hashmap; use itertools::Itertools; +use crate::headers::HeaderValue; + /// Request that the state machine is executing against #[derive(Debug, Clone, PartialEq)] pub struct WebmachineRequest { @@ -263,10 +266,12 @@ impl Default for WebmachineContext { #[cfg(test)] mod tests { - use super::*; - use crate::headers::*; use expectest::prelude::*; + use crate::headers::*; + + use super::*; + #[test] fn request_does_not_have_header_test() { let request = WebmachineRequest { diff --git a/src/headers.rs b/src/headers.rs index 35a39f6..6fedd71 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -1,9 +1,10 @@ //! The `headers` deals with parsing and formatting request and response headers use std::collections::HashMap; -use std::str::Chars; -use std::iter::Peekable; use std::hash::{Hash, Hasher}; +use std::iter::Peekable; +use std::str::Chars; + use itertools::Itertools; const SEPERATORS: [char; 10] = ['(', ')', '<', '>', '@', ',', ';', '=', '{', '}']; @@ -231,10 +232,12 @@ macro_rules! h { #[cfg(test)] mod tests { - use super::*; - use expectest::prelude::*; + use expectest::prelude::*; + use maplit::hashmap; - #[test] + use super::*; + + #[test] fn parse_header_value_test() { expect!(HeaderValue::parse_string("")).to(be_equal_to("".to_string())); expect!(HeaderValue::parse_string("A B")).to(be_equal_to("A B".to_string())); diff --git a/src/lib.rs b/src/lib.rs index d7b212a..6874712 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,11 +43,6 @@ The WebmachineDispatcher implementes the Hyper Service trait, so you can pass it Note: This example uses the maplit crate to provide the `btreemap` macro and the log crate for the logging macros. ```no_run - # #[macro_use] extern crate log; - # #[macro_use] extern crate maplit; - # extern crate hyper; - # extern crate webmachine_rust; - # extern crate serde_json; use hyper::server::Server; use webmachine_rust::*; use webmachine_rust::context::*; @@ -57,6 +52,8 @@ Note: This example uses the maplit crate to provide the `btreemap` macro and the use std::net::SocketAddr; use hyper::service::make_service_fn; use std::convert::Infallible; + use maplit::btreemap; + use tracing::error; # fn main() {} // setup the dispatcher, which maps paths to resources. The requirement of make_service_fn is @@ -109,29 +106,27 @@ For an example of a project using this crate, have a look at the [Pact Mock Serv #![warn(missing_docs)] -#[macro_use] extern crate log; -#[macro_use] extern crate maplit; -extern crate itertools; -#[macro_use] extern crate lazy_static; -extern crate chrono; -extern crate http; - use std::collections::{BTreeMap, HashMap}; -use std::sync::Mutex; +use std::future::Future; +use std::ops::Deref; +use std::pin::Pin; use std::sync::Arc; -use itertools::Itertools; +use std::sync::Mutex; +use std::task::{Context, Poll}; + use chrono::{DateTime, FixedOffset, Utc}; -use context::{WebmachineContext, WebmachineResponse, WebmachineRequest}; -use headers::HeaderValue; +use futures::TryStreamExt; use http::{Request, Response}; -use hyper::service::Service; -use std::task::{Context, Poll}; -use std::pin::Pin; -use std::future::Future; use http::request::Parts; -use futures::TryStreamExt; use hyper::Body; -use std::ops::Deref; +use hyper::service::Service; +use itertools::Itertools; +use lazy_static::lazy_static; +use maplit::hashmap; +use tracing::{debug, error, trace}; + +use context::{WebmachineContext, WebmachineRequest, WebmachineResponse}; +use headers::HeaderValue; #[macro_use] pub mod headers; pub mod context; diff --git a/src/tests.rs b/src/tests.rs index 03d660c..f2242c9 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,17 +1,20 @@ +use std::collections::HashMap; + +use chrono::*; +use expectest::prelude::*; +use maplit::btreemap; + use super::*; -use super::sanitise_path; use super::{ execute_state_machine, - update_paths_for_resource, - parse_header_values, finalise_response, join_paths, + parse_header_values, + update_paths_for_resource, }; use super::context::*; use super::headers::*; -use expectest::prelude::*; -use std::collections::HashMap; -use chrono::*; +use super::sanitise_path; fn resource(path: &str) -> WebmachineRequest { WebmachineRequest { @@ -713,7 +716,8 @@ fn execute_state_machine_returns_412_if_the_resource_etag_does_not_match_if_matc #[test] fn execute_state_machine_returns_412_if_the_resource_last_modified_gt_unmodified_since() { - let datetime = Local::now().with_timezone(&FixedOffset::east(10 * 3600)); + let offset = FixedOffset::east_opt(10 * 3600).expect("FixedOffset::east out of bounds"); + let datetime = Local::now().with_timezone(&offset); let header_datetime = datetime.clone() - Duration::minutes(5); let mut context = WebmachineContext { request: WebmachineRequest { @@ -726,7 +730,10 @@ fn execute_state_machine_returns_412_if_the_resource_last_modified_gt_unmodified }; let resource = WebmachineResource { resource_exists: callback(&|_, _| true), - last_modified: callback(&|_, _| Some(Local::now().with_timezone(&FixedOffset::east(10 * 3600)))), + last_modified: callback(&|_, _| { + let fixed_offset = FixedOffset::east_opt(10 * 3600).expect("FixedOffset::east out of bounds"); + Some(Local::now().with_timezone(&fixed_offset)) + }), ..WebmachineResource::default() }; @@ -821,7 +828,8 @@ fn execute_state_machine_returns_304_if_resource_etag_in_if_non_match_and_is_a_h #[test] fn execute_state_machine_returns_304_if_the_resource_last_modified_gt_modified_since() { - let datetime = Local::now().with_timezone(&FixedOffset::east(10 * 3600)) - Duration::minutes(15); + let offset = FixedOffset::east_opt(10 * 3600).expect("FixedOffset::east out of bounds"); + let datetime = Local::now().with_timezone(&offset) - Duration::minutes(15); let header_datetime = datetime + Duration::minutes(5); let mut context = WebmachineContext { request: WebmachineRequest { @@ -834,7 +842,10 @@ fn execute_state_machine_returns_304_if_the_resource_last_modified_gt_modified_s }; let resource = WebmachineResource { resource_exists: callback(&|_, _| true), - last_modified: callback(&|_, _| Some(Local::now().with_timezone(&FixedOffset::east(10 * 3600)) - Duration::minutes(15))), + last_modified: callback(&|_, _| { + let offset = FixedOffset::east_opt(10 * 3600).expect("FixedOffset::east out of bounds"); + Some(Local::now().with_timezone(&offset) - Duration::minutes(15)) + }), ..WebmachineResource::default() }; execute_state_machine(&mut context, &resource);