Skip to content

Commit

Permalink
add enum syn parsing part
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyukang committed Feb 26, 2024
1 parent 4160c36 commit 16f7e81
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
11 changes: 0 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,6 @@ gen-rpc-doc: submodule-init ## Generate rpc documentation
./target/debug/ckb-rpc-gen --json
./target/debug/ckb-rpc-gen rpc/README.md

.PHONY: old-gen-rpc-doc
old-gen-rpc-doc: ## Generate rpc documentation
rm -f ${CARGO_TARGET_DIR}/doc/ckb_rpc/module/trait.*.html
cargo doc --workspace
ln -nsf "${CARGO_TARGET_DIR}" "target"
if command -v python3 &> /dev/null; then \
python3 ./devtools/doc/rpc.py > rpc/old-README.md; \
else \
python ./devtools/doc/rpc.py > rpc/old-README.md; \
fi

.PHONY: gen-hashes
gen-hashes: ## Generate docs/hashes.toml
cargo run list-hashes -b > docs/hashes.toml
Expand Down
2 changes: 0 additions & 2 deletions devtools/doc/rpc-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ serde_json = "~1.0"
tera = "1"
syn = { version = "2.0.39", features = ["extra-traits", "full", "parsing", "visit"] }
walkdir = "2.1.4"


70 changes: 51 additions & 19 deletions devtools/doc/rpc-gen/src/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,33 @@ struct CommentFinder {
types: Vec<String>,
}

fn get_doc_from_attr(attr: &syn::Attribute) -> String {
if attr.path().is_ident("doc") {
if let Meta::NameValue(MetaNameValue {
value:
Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit),
..
}),
..
}) = &attr.meta
{
let lit = lit.value();
return lit;
}
}
"".to_string()
}

impl Visit<'_> for CommentFinder {
fn visit_attribute(&mut self, attr: &syn::Attribute) {
if let Some(type_name) = &self.current_type {
if attr.path().is_ident("doc") {
if let Meta::NameValue(MetaNameValue {
value:
Expr::Lit(syn::ExprLit {
lit: syn::Lit::Str(lit),
..
}),
..
}) = &attr.meta
{
let lit = lit.value();
let current_type = type_name.clone();
*self
.type_comments
.entry(current_type)
.or_insert("".to_string()) += &format!("\n{}", lit.trim_start());
}
}
let doc = get_doc_from_attr(attr);
let current_type = type_name.clone();
*self
.type_comments
.entry(current_type)
.or_insert("".to_string()) += &format!("\n{}", doc.trim_start());
}
}

Expand All @@ -59,6 +65,32 @@ impl Visit<'_> for CommentFinder {
self.current_type = None;
}
}

fn visit_item_enum(&mut self, i: &'_ syn::ItemEnum) {
let ident_name = i.ident.to_string();
if self.types.contains(&ident_name) {
if !i.attrs.is_empty() {
self.current_type = Some(ident_name);
for attr in &i.attrs {
self.visit_attribute(attr);
}
self.current_type = None;
}
let mut variants = vec![];
for v in &i.variants {
if !v.attrs.is_empty() {
let doc: Vec<String> = v.attrs.iter().map(get_doc_from_attr).collect();
let doc = doc.join("\n");
variants.push(format!(" - `{}` : {}", v.ident, doc));
}
}
let extra_doc = variants.join("\n");
*self
.type_comments
.entry(i.ident.to_string())
.or_insert("".to_string()) += &format!("An enum value from one of:\n{}", extra_doc);
}
}
}

fn visit_source_file(finder: &mut CommentFinder, file_path: &std::path::Path) {
Expand All @@ -74,7 +106,7 @@ pub(crate) fn visit_for_types() -> Vec<(String, String)> {
let mut finder = CommentFinder {
type_comments: Default::default(),
current_type: None,
types: vec!["JsonBytes", "IndexerRange"]
types: vec!["JsonBytes", "IndexerRange", "PoolTransactionReject"]
.iter()
.map(|&s| s.to_owned())
.collect(),
Expand Down
22 changes: 22 additions & 0 deletions rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ The crate `ckb-rpc`'s minimum supported rustc version is 1.71.1.
* [Type `OutPoint`](#type-outpoint)
* [Type `OutputsValidator`](#type-outputsvalidator)
* [Type `PeerSyncState`](#type-peersyncstate)
* [Type `PoolTransactionReject`](#type-pooltransactionreject)
* [Type `PoolTxDetailInfo`](#type-pooltxdetailinfo)
* [Type `ProposalShortId`](#type-proposalshortid)
* [Type `ProposalWindow`](#type-proposalwindow)
Expand Down Expand Up @@ -5869,6 +5870,8 @@ A array represent (half-open) range bounded inclusively below and exclusively ab
| ["0x0", "0x2"] | [0, 2) |
| ["0x0", "0x174876e801"] | [0, 100000000001) |



### Type `IndexerScriptType`
ScriptType `Lock` | `Type`

Expand Down Expand Up @@ -6174,6 +6177,25 @@ The chain synchronization state between the local node and a remote node.

**Deprecated**: this is an internal state and will be removed in a future release.

### Type `PoolTransactionReject`

TX reject message, `PoolTransactionReject` is a JSON object with following fields.
* `type`: the Reject type with following enum values
* `description`: `string` - Detailed description about why the transaction is rejected.

An enum value from one of:
- `LowFeeRate` : Transaction fee lower than config
- `ExceededMaximumAncestorsCount` : Transaction exceeded maximum ancestors count limit
- `ExceededTransactionSizeLimit` : Transaction exceeded maximum size limit
- `Full` : Transaction are replaced because the pool is full
- `Duplicated` : Transaction already exists in transaction_pool
- `Malformed` : Malformed transaction
- `DeclaredWrongCycles` : Declared wrong cycles
- `Resolve` : Resolve failed
- `Verification` : Verification failed
- `Expiry` : Transaction expired
- `RBFRejected` : RBF rejected

### Type `PoolTxDetailInfo`
A Tx details info in tx-pool.

Expand Down
4 changes: 3 additions & 1 deletion util/jsonrpc-types/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ impl From<CorePoolTxDetailInfo> for PoolTxDetailInfo {
}
}

/// TX reject message
/// TX reject message, `PoolTransactionReject` is a JSON object with following fields.
/// * `type`: the Reject type with following enum values
/// * `description`: `string` - Detailed description about why the transaction is rejected.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", content = "description")]
pub enum PoolTransactionReject {
Expand Down

0 comments on commit 16f7e81

Please sign in to comment.