Skip to content

Commit

Permalink
Cleanup, dispense with some needless borrows
Browse files Browse the repository at this point in the history
  • Loading branch information
durch committed Jan 22, 2018
1 parent 7d0b467 commit e518be2
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-s3"
version = "0.8.1"
version = "0.8.2"
authors = ["Drazen Urch", "Nick Stevens"]
description = "Tiny Rust library for working with Amazon S3."
repository = "https://github.com/durch/rust-s3"
Expand Down
6 changes: 3 additions & 3 deletions src/bin/simple_crud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::str;
use s3::bucket::Bucket;
use s3::credentials::Credentials;

const BUCKET: &'static str = "example-bucket";
const REGION: &'static str = "us-east-1";
const MESSAGE: &'static str = "I want to go to S3";
const BUCKET: &str = "example-bucket";
const REGION: &str = "us-east-1";
const MESSAGE: &str = "I want to go to S3";

fn load_credentials() -> Credentials {
let aws_access = env::var("AWS_KEY_ID").expect("Must specify AWS_ACCESS_KEY_ID");
Expand Down
5 changes: 2 additions & 3 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ pub enum Command<'a> {
impl<'a> Command<'a> {
pub fn http_verb(&self) -> &'static str {
match *self {
Command::Get => "GET",
Command::Get | Command::List { .. }=> "GET",
Command::Put { .. } => "PUT",
Command::Delete => "DELETE",
Command::List { .. } => "GET",
Command::Delete => "DELETE"
}
}
}
5 changes: 2 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,5 @@ pub mod serde_types;
pub mod signing;
pub mod deserializer;

const LONG_DATE: &'static str = "%Y%m%dT%H%M%SZ";
const EMPTY_PAYLOAD_SHA: &'static str = "e3b0c44298fc1c149afbf4c8996fb924\
27ae41e4649b934ca495991b7852b855";
const LONG_DATE: &str = "%Y%m%dT%H%M%SZ";
const EMPTY_PAYLOAD_SHA: &str = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
6 changes: 3 additions & 3 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'a> Request<'a> {
// generated, there's really no way this should fail.
let mut url = Url::parse(&url_str).expect("static URL parsing");

for (key, value) in self.bucket.extra_query.iter() {
for (key, value) in &self.bucket.extra_query {
url.query_pairs_mut().append_pair(key, value);
}

Expand Down Expand Up @@ -114,7 +114,7 @@ impl<'a> Request<'a> {
}

fn string_to_sign(&self, request: &str) -> String {
signing::string_to_sign(&self.datetime, self.bucket.region(), &request)
signing::string_to_sign(&self.datetime, self.bucket.region(), request)
}

fn signing_key(&self) -> Vec<u8> {
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'a> Request<'a> {

// Build and set a Curl List of headers
let mut list = List::new();
for (key, value) in try!(self.headers()).iter() {
for (key, value) in &self.headers()? {
let header = format!("{}: {}", key, value);
list.append(&header)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/serde_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Owner {
pub id: String,
}

/// An individual object in a ListBucketResult
/// An individual object in a `ListBucketResult`
#[derive(Deserialize, Debug, Clone)]
pub struct Object {
#[serde(rename = "LastModified")]
Expand Down Expand Up @@ -78,7 +78,7 @@ pub struct ListBucketResult {
pub common_prefixes: Option<Vec<CommonPrefix>>,
}

/// CommonPrefix is used to group keys
/// `CommonPrefix` is used to group keys
#[derive(Deserialize, Debug, Clone)]
pub struct CommonPrefix {
#[serde(rename = "Prefix")]
Expand Down
24 changes: 12 additions & 12 deletions src/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ use region::Region;
use request::Headers;
use sha2::{Digest, Sha256};

use serde_xml;
use serde_types::ListBucketResult;

const SHORT_DATE: &'static str = "%Y%m%d";
const LONG_DATETIME: &'static str = "%Y%m%dT%H%M%SZ";
const SHORT_DATE: &str = "%Y%m%d";
const LONG_DATETIME: &str = "%Y%m%dT%H%M%SZ";

/// Encode a URI following the specific requirements of the AWS service.
pub fn uri_encode(string: &str, encode_slash: bool) -> String {
Expand Down Expand Up @@ -72,10 +69,10 @@ pub fn signed_header_string(headers: &Headers) -> String {
pub fn canonical_request(method: &str, url: &Url, headers: &Headers, sha256: &str) -> String {
format!("{method}\n{uri}\n{query_string}\n{headers}\n\n{signed}\n{sha256}",
method = method,
uri = canonical_uri_string(&url),
query_string = canonical_query_string(&url),
uri = canonical_uri_string(url),
query_string = canonical_query_string(url),
headers = canonical_header_string(headers),
signed = signed_header_string(&headers),
signed = signed_header_string(headers),
sha256 = sha256)
}

Expand Down Expand Up @@ -107,12 +104,12 @@ pub fn signing_key(datetime: &DateTime<Utc>,
let secret = String::from("AWS4") + secret_key;
let mut date_hmac = Hmac::<Sha256>::new(secret.as_bytes());
date_hmac.input(datetime.format(SHORT_DATE).to_string().as_bytes());
let mut region_hmac = Hmac::<Sha256>::new(&date_hmac.result().code());
let mut region_hmac = Hmac::<Sha256>::new(date_hmac.result().code());
region_hmac.input(region.to_string().as_bytes());
let mut service_hmac = Hmac::<Sha256>::new(&region_hmac.result().code());
let mut service_hmac = Hmac::<Sha256>::new(region_hmac.result().code());
service_hmac.input(service.as_bytes());
let mut signing_hmac = Hmac::<Sha256>::new(&service_hmac.result().code());
signing_hmac.input("aws4_request".as_bytes());
let mut signing_hmac = Hmac::<Sha256>::new(service_hmac.result().code());
signing_hmac.input(b"aws4_request");
signing_hmac.result().code().into()
}

Expand Down Expand Up @@ -142,6 +139,9 @@ mod tests {
use request::Headers;
use super::*;

use serde_xml;
use serde_types::ListBucketResult;

#[test]
fn test_base_url_encode() {
// Make sure parsing doesn't remove extra slashes, as normalization
Expand Down

0 comments on commit e518be2

Please sign in to comment.