Skip to content

Commit

Permalink
Bucket::new() returns now a Result instead of Option (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpelloni committed Oct 31, 2021
1 parent ce0f9e5 commit a5294a8
Showing 1 changed file with 41 additions and 6 deletions.
47 changes: 41 additions & 6 deletions src/bucket.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use std::error::Error as StdError;
use std::fmt::{self, Display};

use url::{ParseError, Url};

use crate::actions::{
Expand Down Expand Up @@ -41,31 +44,38 @@ use crate::Credentials;
/// assert_eq!(bucket.region(), "eu-west-1");
/// assert_eq!(bucket.object_url("duck.jpg").expect("url is valid").as_str(), "https://rusty-s3.s3.dualstack.eu-west-1.amazonaws.com/duck.jpg");
/// ```
#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub struct Bucket {
base_url: Url,
name: String,
region: String,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum BucketError {
UnsupportedScheme,
MissingHost,
}

impl Bucket {
/// Construct a new S3 bucket
pub fn new<S: Into<String>>(
endpoint: Url,
path_style: bool,
name: S,
region: S,
) -> Option<Self> {
let _ = endpoint.host_str()?;
) -> Result<Self, BucketError> {
endpoint.host_str().ok_or(BucketError::MissingHost)?;

match endpoint.scheme() {
"http" | "https" => {}
_ => return None,
_ => return Err(BucketError::UnsupportedScheme),
};

let name: String = name.into();
let base_url = base_url(endpoint, &name, path_style);

Some(Self {
Ok(Self {
base_url,
name,
region: region.into(),
Expand Down Expand Up @@ -260,6 +270,17 @@ impl Bucket {
}
}

impl Display for BucketError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsupportedScheme => f.write_str("unsupported Url scheme"),
Self::MissingHost => f.write_str("Url is missing the `host`"),
}
}
}

impl StdError for BucketError {}

#[cfg(test)]
mod tests {
use crate::actions::ObjectIdentifier;
Expand Down Expand Up @@ -303,10 +324,24 @@ mod tests {

#[test]
fn new_bad_scheme() {
let endpoint = "ftp://example.com/example".parse().unwrap();
let name = "rusty-s3";
let region = "eu-west-1";
assert_eq!(
Bucket::new(endpoint, true, name, region),
Err(BucketError::UnsupportedScheme)
);
}

#[test]
fn new_missing_host() {
let endpoint = "file:///home/something".parse().unwrap();
let name = "rusty-s3";
let region = "eu-west-1";
assert!(Bucket::new(endpoint, true, name, region).is_none());
assert_eq!(
Bucket::new(endpoint, true, name, region),
Err(BucketError::MissingHost)
);
}

#[test]
Expand Down

0 comments on commit a5294a8

Please sign in to comment.