Skip to content

Commit

Permalink
Merge pull request #1 from rholshausen/master
Browse files Browse the repository at this point in the history
update to Rust 2021 edition + switch to using the tracing crate
  • Loading branch information
uglyog authored Jun 14, 2023
2 parents 6cafea1 + 71954b4 commit 5432879
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 53 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
24 changes: 12 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webmachine-rust"
version = "0.2.3"
version = "0.3.0"
authors = ["Ronald Holshausen <uglyog@gmail.com>"]
description = "Port of the Ruby Webmachine library to Rust"
documentation = "http://www.pact.io/reference/rust/webmachine-rust-0.1.0/webmachine_rust/"
Expand All @@ -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"
13 changes: 9 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 8 additions & 5 deletions src/headers.rs
Original file line number Diff line number Diff line change
@@ -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] = ['(', ')', '<', '>', '@', ',', ';', '=', '{', '}'];
Expand Down Expand Up @@ -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()));
Expand Down
39 changes: 17 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 21 additions & 10 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
};

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand Down

0 comments on commit 5432879

Please sign in to comment.