Skip to content

Commit

Permalink
feat(storage): remove whitespace from bucket name
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Feb 27, 2023
1 parent eb193db commit 403451b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
23 changes: 23 additions & 0 deletions contracts/cw-storage/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,27 @@ mod tests {

assert_eq!(err, ContractError::Bucket(BucketError::EmptyName));
}

#[test]
fn whitespace_initialization() {
let mut deps = mock_dependencies();

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

let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len());

let res = query(deps.as_ref(), mock_env(), QueryMsg::Bucket {}).unwrap();
let value: BucketResponse = from_binary(&res).unwrap();
assert_eq!("foobar", value.name);
}
}
2 changes: 2 additions & 0 deletions contracts/cw-storage/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Cursor = String;
#[cw_serde]
pub struct InstantiateMsg {
/// The name of the bucket.
/// The name could not be empty or contains whitespaces.
/// If name contains whitespace, they will be removed.
pub bucket: String,
/// The limits of the bucket.
pub limits: BucketLimits,
Expand Down
5 changes: 3 additions & 2 deletions contracts/cw-storage/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ pub struct Bucket {

impl Bucket {
pub fn new(name: String, limits: Limits) -> Result<Self, BucketError> {
if name.is_empty() {
let n: String = name.split_whitespace().collect();
if n.is_empty() {
return Err(EmptyName);
}

Ok(Self { name, limits })
Ok(Self { name: n, limits })
}
}

Expand Down

0 comments on commit 403451b

Please sign in to comment.