fix: parse constructor as function (#1479)

* fix: parse constructor as function

* parse constructor separatly
This commit is contained in:
Matthias Seitz 2022-07-14 21:15:15 +02:00 committed by GitHub
parent cca3fcec93
commit ba00f549dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 46 additions and 1 deletions

View File

@ -1,4 +1,4 @@
use ethabi::{Event, EventParam, Function, Param, ParamType, StateMutability}; use ethabi::{Constructor, Event, EventParam, Function, Param, ParamType, StateMutability};
use std::{fmt, iter::Peekable, str::CharIndices}; use std::{fmt, iter::Peekable, str::CharIndices};
use unicode_xid::UnicodeXID; use unicode_xid::UnicodeXID;
@ -320,6 +320,18 @@ impl<'input> HumanReadableParser<'input> {
Self::new(input).take_function() Self::new(input).take_function()
} }
/// Parses a [Constructor] from a human readable form
///
/// # Example
///
/// ```
/// use ethers_core::abi::HumanReadableParser;
/// let mut constructor = HumanReadableParser::parse_constructor("constructor(address author, string oldValue, string newValue)").unwrap();
/// ```
pub fn parse_constructor(input: &'input str) -> Result<Constructor, LexerError> {
Self::new(input).take_constructor()
}
/// Parses an [Event] from a human readable form /// Parses an [Event] from a human readable form
/// ///
/// # Example /// # Example
@ -332,6 +344,15 @@ impl<'input> HumanReadableParser<'input> {
Self::new(input).take_event() Self::new(input).take_event()
} }
/// Returns the next `Constructor` and consumes the underlying tokens
pub fn take_constructor(&mut self) -> Result<Constructor, LexerError> {
self.take_next_exact(Token::Constructor)?;
self.take_open_parenthesis()?;
let inputs = self.take_function_params()?;
self.take_close_parenthesis()?;
Ok(Constructor { inputs })
}
/// Returns the next `Function` and consumes the underlying tokens
pub fn take_function(&mut self) -> Result<Function, LexerError> { pub fn take_function(&mut self) -> Result<Function, LexerError> {
let name = self.take_identifier(Token::Function)?; let name = self.take_identifier(Token::Function)?;
@ -812,6 +833,30 @@ pub enum DataLocation {
mod tests { mod tests {
use super::*; use super::*;
#[test]
fn parse_constructor() {
let f = Constructor {
inputs: vec![
Param { name: "author".to_string(), kind: ParamType::Address, internal_type: None },
Param {
name: "oldValue".to_string(),
kind: ParamType::String,
internal_type: None,
},
Param {
name: "newValue".to_string(),
kind: ParamType::String,
internal_type: None,
},
],
};
let parsed = HumanReadableParser::parse_constructor(
"constructor(address author, string oldValue, string newValue)",
)
.unwrap();
assert_eq!(f, parsed);
}
#[test] #[test]
fn test_parse_function() { fn test_parse_function() {
#[allow(deprecated)] #[allow(deprecated)]