Skip to content

Commit

Permalink
remove extra spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Oct 12, 2023
1 parent 9b1e588 commit e698638
Show file tree
Hide file tree
Showing 2 changed files with 3,590 additions and 3,472 deletions.
81 changes: 67 additions & 14 deletions devtools/doc/rpc-gen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ impl RpcModule {
"".to_string()
};
let signatures = format!("* `{}({})`\n{}\n{}", name, args, arg_lines, ret_ty);
let desc = method["description"]
let mut desc = method["description"]
.as_str()
.unwrap()
.replace("##", "######");
desc = strip_prefix_space(&desc);
res.push_str(&format!(
"#### Method `{}`\n{}\n\n{}\n",
name, signatures, desc,
Expand All @@ -96,10 +97,7 @@ impl RpcDocGenerator {
.unwrap()
.trim_end_matches("_rpc")
.to_string();
let module_description = map["info"]["description"]
.as_str()
.unwrap_or_default()
.to_string();
let module_description = get_description(&map["info"]["description"]);
let module_methods = map["methods"].as_array().unwrap();
let types = map["components"]["schemas"].as_object().unwrap();
all_types.push(types);
Expand Down Expand Up @@ -208,10 +206,40 @@ impl RpcDocGenerator {
}

fn capitlize(s: &str) -> String {
let mut res = String::new();
res.push_str(&s[0..1].to_uppercase());
res.push_str(&s[1..]);
res
s[0..1].to_uppercase().to_string() + &s[1..]
}

fn strip_prefix_space(content: &str) -> String {
let minimal_strip_count = content
.lines()
.map(|l| {
if l.trim().is_empty() {
usize::MAX
} else {
l.chars().take_while(|c| c.is_whitespace()).count()
}
})
.min()
.unwrap_or(0);
if minimal_strip_count > 0 {
content
.lines()
.map(|l| {
if l.len() > minimal_strip_count {
l[minimal_strip_count..].to_string()
} else {
"".to_string()
}
})
.collect::<Vec<_>>()
.join("\n")
} else {
content.to_string()
}
}

fn get_description(value: &Value) -> String {
strip_prefix_space(value.as_str().unwrap())
}

fn gen_type_desc(desc: &str) -> String {
Expand All @@ -234,7 +262,11 @@ fn gen_type_desc(desc: &str) -> String {
} else {
l.to_string()
};
format!(" {}", l)
if l.is_empty() {
l
} else {
format!(" {}", l)
}
})
.collect::<Vec<_>>()
.join("\n");
Expand Down Expand Up @@ -262,7 +294,8 @@ fn gen_type_fields(ty: &Value) -> String {
})
.collect::<Vec<_>>()
.join("\n");
format!("#### Fields:\n{}", res)
let res = strip_prefix_space(&res);
format!("\n#### Fields:\n{}", res)
} else {
"".to_string()
}
Expand Down Expand Up @@ -318,12 +351,12 @@ fn gen_type(ty: &Value) -> String {
fn gen_errors_content(res: &mut String) {
let schema = schema_for!(RPCError);
let value = serde_json::to_value(schema).unwrap();
let summary = value["description"].as_str().unwrap();
let summary = get_description(&value["description"]);
res.push_str("## RPC Errors\n");
res.push_str(summary);
res.push_str(&summary);

for error in value["oneOf"].as_array().unwrap().iter() {
let desc = error["description"].as_str().unwrap();
let desc = get_description(&error["description"]);
let enum_ty = error["enum"].as_array().unwrap()[0].as_str().unwrap();
let doc = format!("\n### ERROR `{}`\n{}\n", enum_ty, desc);
res.push_str(&doc);
Expand All @@ -346,6 +379,7 @@ fn gen_subscription_rpc_doc(res: &mut String) {
})
.collect::<Vec<_>>()
.join("\n");
let summary = strip_prefix_space(&summary);

// read the continues comments between `S: Stream` and `fn subscribe`
let sub_desc = pubsub_module_source
Expand All @@ -359,7 +393,26 @@ fn gen_subscription_rpc_doc(res: &mut String) {
})
.collect::<Vec<_>>()
.join("\n");
let sub_desc = strip_prefix_space(&sub_desc);

res.push_str(format!("{}\n\n", summary).as_str());
res.push_str(format!("{}\n", sub_desc).as_str());
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_strip_prefix_space() {
assert_eq!(strip_prefix_space(" "), "".to_string());
assert_eq!(
strip_prefix_space(" \n abc\n cdf"),
"\nabc\ncdf".to_string()
);
assert_eq!(
strip_prefix_space("abc\n cde\n cdf"),
"abc\n cde\n cdf".to_string()
);
}
}
Loading

0 comments on commit e698638

Please sign in to comment.