Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove single whitespace prefix from comments and docs #1385

Merged
merged 5 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Collaborator

@ascjones ascjones Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot, could you update the trim_docs test: https://github.com/paritytech/ink/blob/47b1fa3291dd7551002e780ace31df4d80e8539d/crates/metadata/src/tests.rs#L180 (or add a new one) to cover this, and also to test the updated trimming logic?

.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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a short comment to this public function

if let Some(stripped) = item.strip_prefix(' ') {
stripped.trim_end()
} else {
item.trim_end()
}
}