Skip to content

Commit

Permalink
style(storage): format with love
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 3, 2023
1 parent e233791 commit 7c0eea3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 87 deletions.
109 changes: 24 additions & 85 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: "foo".to_string(),
limits: BucketLimits {
max_total_size: None,
max_objects: None,
max_object_size: None,
max_object_pins: None,
},
limits: BucketLimits::new(),
};
let info = mock_info("creator", &[]);

Expand Down Expand Up @@ -267,12 +262,7 @@ mod tests {
let info = mock_info("creator", &[]);
let msg = InstantiateMsg {
bucket: String::from("test"),
limits: BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: None,
max_object_pins: None,
},
limits: BucketLimits::new(),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();

Expand Down Expand Up @@ -325,7 +315,7 @@ mod tests {
data: Binary::from_base64(obj1.0.as_str()).unwrap(),
pin: obj1.1,
};
let res = execute(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();
let res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(
res.attributes,
vec![
Expand All @@ -350,77 +340,31 @@ mod tests {
#[test]
fn store_object_limits() {
let cases = vec![
(BucketLimits::new().set_max_objects(2u128.into()), None),
(BucketLimits::new().set_max_object_size(5u128.into()), None),
(BucketLimits::new().set_max_total_size(9u128.into()), None),
(BucketLimits::new().set_object_pins(1u128.into()), None),
(
BucketLimits {
max_objects: Some(2u128.into()),
max_object_size: None,
max_total_size: None,
max_object_pins: None,
},
None,
),
(
BucketLimits {
max_objects: None,
max_object_size: Some(5u128.into()),
max_total_size: None,
max_object_pins: None,
},
None,
),
(
BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: Some(9u128.into()),
max_object_pins: None,
},
None,
BucketLimits::new().set_max_objects(1u128.into()),
Some(ContractError::Bucket(BucketError::MaxObjectsLimitExceeded)),
),
(
BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: None,
max_object_pins: Some(1u128.into()),
},
None,
BucketLimits::new().set_max_object_size(4u128.into()),
Some(ContractError::Bucket(
BucketError::MaxObjectSizeLimitExceeded,
)),
),
(
BucketLimits {
max_objects: Some(1u128.into()),
max_object_size: None,
max_total_size: None,
max_object_pins: None,
},
Some(MaxObjectsLimitExceeded {}),
BucketLimits::new().set_max_total_size(8u128.into()),
Some(ContractError::Bucket(
BucketError::MaxTotalSizeLimitExceeded,
)),
),
(
BucketLimits {
max_objects: None,
max_object_size: Some(4u128.into()),
max_total_size: None,
max_object_pins: None,
},
Some(MaxObjectSizeLimitExceeded {}),
),
(
BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: Some(8u128.into()),
max_object_pins: None,
},
Some(MaxTotalSizeLimitExceeded {}),
),
(
BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: None,
max_object_pins: Some(0u128.into()),
},
Some(MaxObjectPinsLimitExceeded {}),
BucketLimits::new().set_object_pins(0u128.into()),
Some(ContractError::Bucket(
BucketError::MaxObjectPinsLimitExceeded,
)),
),
];

Expand Down Expand Up @@ -459,12 +403,7 @@ mod tests {

let msg = InstantiateMsg {
bucket: String::from("test"),
limits: BucketLimits {
max_objects: None,
max_object_size: None,
max_total_size: None,
max_object_pins: None,
},
limits: BucketLimits::new(),
};
instantiate(deps.as_mut(), mock_env(), info.clone(), msg).unwrap();

Expand Down Expand Up @@ -496,7 +435,7 @@ mod tests {
ObjectId::from("2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824")
);
assert_eq!(res.owner, info.sender);
assert_eq!(res.is_pinned, true);
assert!(res.is_pinned);
assert_eq!(res.size.u128(), 5u128);

let data = general_purpose::STANDARD.encode("okp4");
Expand All @@ -516,7 +455,7 @@ mod tests {
ObjectId::from("315d0d9ab12c5f8884100055f79de50b72db4bd2c9bfd3df049d89640fed1fa6")
);
assert_eq!(res.owner, info.sender);
assert_eq!(res.is_pinned, false);
assert!(!res.is_pinned);
assert_eq!(res.size.u128(), 4u128);
}
}
2 changes: 1 addition & 1 deletion contracts/cw-storage/src/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use sha2::{Sha256, Digest};
use sha2::{Digest, Sha256};

pub fn sha256_hash(data: &Vec<u8>) -> String {
base16ct::lower::encode_string(&Sha256::digest(data))
Expand Down
2 changes: 1 addition & 1 deletion contracts/cw-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod contract;
pub mod crypto;
mod error;
pub mod msg;
pub mod state;
pub mod crypto;

pub use crate::error::ContractError;
31 changes: 31 additions & 0 deletions contracts/cw-storage/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,37 @@ pub struct BucketLimits {
pub max_object_pins: Option<Uint128>,
}

impl BucketLimits {
pub const fn new() -> Self {
BucketLimits {
max_total_size: None,
max_objects: None,
max_object_size: None,
max_object_pins: None,
}
}

pub fn set_max_total_size(mut self, max_total_size: Uint128) -> Self {
self.max_total_size = Some(max_total_size);
self
}

pub fn set_max_objects(mut self, max_objects: Uint128) -> Self {
self.max_objects = Some(max_objects);
self
}

pub fn set_max_object_size(mut self, max_object_size: Uint128) -> Self {
self.max_object_size = Some(max_object_size);
self
}

pub fn set_object_pins(mut self, max_object_pins: Uint128) -> Self {
self.max_object_pins = Some(max_object_pins);
self
}
}

impl From<Limits> for BucketLimits {
fn from(limits: Limits) -> Self {
BucketLimits {
Expand Down
1 change: 1 addition & 0 deletions contracts/cw-storage/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ impl From<BucketLimits> for Limits {
}
}
}

pub const BUCKET: Item<Bucket> = Item::new("bucket");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
Expand Down

0 comments on commit 7c0eea3

Please sign in to comment.