Skip to content

Commit

Permalink
remove single whitespace prefix from comments and docs (#1385)
Browse files Browse the repository at this point in the history
* remove single whitespace prefix from comments and docs

* format code to please CI

* elided lifetime + tests

* adds comments to tests

* add docs for public function
  • Loading branch information
SkymanOne authored Sep 5, 2022
1 parent 5755802 commit 7ddf87f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
15 changes: 12 additions & 3 deletions crates/metadata/src/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

#![allow(clippy::new_ret_no_self)]

use crate::serde_hex;
use crate::{
serde_hex,
utils::trim_extra_whitespace,
};
#[cfg(not(feature = "std"))]
use alloc::{
format,
Expand Down Expand Up @@ -367,7 +370,10 @@ impl<S, P> ConstructorSpecBuilder<S, P> {
{
let mut this = self;
debug_assert!(this.spec.docs.is_empty());
this.spec.docs = docs.into_iter().map(str::trim).collect::<Vec<_>>();
this.spec.docs = docs
.into_iter()
.map(trim_extra_whitespace)
.collect::<Vec<_>>();
this
}
}
Expand Down Expand Up @@ -586,7 +592,10 @@ impl<S, M, P, R> MessageSpecBuilder<S, M, P, R> {
{
let mut this = self;
debug_assert!(this.spec.docs.is_empty());
this.spec.docs = docs.into_iter().collect::<Vec<_>>();
this.spec.docs = docs
.into_iter()
.map(trim_extra_whitespace)
.collect::<Vec<_>>();
this
}
}
Expand Down
47 changes: 47 additions & 0 deletions crates/metadata/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ fn spec_contract_json() {
)
}

/// Tests correct trimming of a simple comment with extra spaces
#[test]
fn trim_docs() {
// given
Expand Down Expand Up @@ -206,3 +207,49 @@ fn trim_docs() {
);
assert_eq!(deserialized.docs, compact_spec.docs);
}

/// Tests correct trimming of a complex comment with a code snippet
#[test]
fn trim_docs_with_code() {
// given
let label = "foo";
let cs = ConstructorSpec::from_label(label)
.selector(123_456_789u32.to_be_bytes())
.docs(vec![
" Example ",
" ```",
" fn test() {",
" \"Hello, World\"",
" }",
" ```",
])
.payable(Default::default())
.done();
let mut registry = Registry::new();
let compact_spec = cs.into_portable(&mut registry);

// when
let json = serde_json::to_value(&compact_spec).unwrap();
let deserialized: ConstructorSpec<PortableForm> =
serde_json::from_value(json.clone()).unwrap();

// then
assert_eq!(
json,
json!({
"label": "foo",
"payable": false,
"selector": "0x075bcd15",
"args": [],
"docs": [
"Example",
"```",
"fn test() {",
" \"Hello, World\"",
"}",
"```"
]
})
);
assert_eq!(deserialized.docs, compact_spec.docs);
}
10 changes: 10 additions & 0 deletions crates/metadata/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

use crate::serde_hex;
use std::str;

/// Serializes the given bytes as byte string.
pub fn serialize_as_byte_str<S>(bytes: &[u8], serializer: S) -> Result<S::Ok, S::Error>
Expand Down Expand Up @@ -56,3 +57,12 @@ where

deserializer.deserialize_str(Visitor)
}

/// Strips a single whitespace at the start and removes trailing spaces
pub fn trim_extra_whitespace(item: &str) -> &str {
if let Some(stripped) = item.strip_prefix(' ') {
stripped.trim_end()
} else {
item.trim_end()
}
}

0 comments on commit 7ddf87f

Please sign in to comment.