use super::*;
impl<N: Network, Instruction: InstructionTrait<N>> Parser for ClosureCore<N, Instruction> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
let (string, _) = Sanitizer::parse(string)?;
let (string, _) = tag(Self::type_name())(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, name) = Identifier::<N>::parse(string)?;
let (string, _) = Sanitizer::parse_whitespaces(string)?;
let (string, _) = tag(":")(string)?;
let (string, inputs) = many0(Input::parse)(string)?;
let (string, instructions) = many1(Instruction::parse)(string)?;
let (string, outputs) = many0(Output::parse)(string)?;
map_res(take(0usize), move |_| {
let mut closure = Self::new(name);
inputs.iter().cloned().try_for_each(|input| closure.add_input(input))?;
instructions.iter().cloned().try_for_each(|instruction| closure.add_instruction(instruction))?;
outputs.iter().cloned().try_for_each(|output| closure.add_output(output))?;
Ok::<_, Error>(closure)
})(string)
}
}
impl<N: Network, Instruction: InstructionTrait<N>> FromStr for ClosureCore<N, Instruction> {
type Err = Error;
fn from_str(string: &str) -> Result<Self> {
match Self::parse(string) {
Ok((remainder, object)) => {
ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
Ok(object)
}
Err(error) => bail!("Failed to parse string. {error}"),
}
}
}
impl<N: Network, Instruction: InstructionTrait<N>> Debug for ClosureCore<N, Instruction> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network, Instruction: InstructionTrait<N>> Display for ClosureCore<N, Instruction> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} {}:", Self::type_name(), self.name)?;
self.inputs.iter().try_for_each(|input| write!(f, "\n {input}"))?;
self.instructions.iter().try_for_each(|instruction| write!(f, "\n {instruction}"))?;
self.outputs.iter().try_for_each(|output| write!(f, "\n {output}"))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Closure;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
#[test]
fn test_closure_parse() {
let closure = Closure::<CurrentNetwork>::parse(
r"
closure foo:
input r0 as field;
input r1 as field;
add r0 r1 into r2;
output r2 as field;",
)
.unwrap()
.1;
assert_eq!("foo", closure.name().to_string());
assert_eq!(2, closure.inputs().len());
assert_eq!(1, closure.instructions().len());
assert_eq!(1, closure.outputs().len());
}
#[test]
fn test_closure_parse_cast() {
let closure = Closure::<CurrentNetwork>::parse(
r"
closure foo:
input r0 as token.record;
cast r0.owner r0.token_amount into r1 as data;
output r1 as data;",
)
.unwrap()
.1;
assert_eq!("foo", closure.name().to_string());
assert_eq!(1, closure.inputs().len());
assert_eq!(1, closure.instructions().len());
assert_eq!(1, closure.outputs().len());
}
#[test]
fn test_closure_display() {
let expected = r"closure foo:
input r0 as field;
input r1 as field;
add r0 r1 into r2;
output r2 as field;";
let closure = Closure::<CurrentNetwork>::parse(expected).unwrap().1;
assert_eq!(expected, format!("{closure}"),);
}
#[test]
fn test_closure_parse_output_function() {
let result = Closure::<CurrentNetwork>::parse(
r"
closure foo:
input r0 as token.record;
cast r0.owner r0.token_amount into r1 as token.record;
output r1 as token.record;",
);
assert!(result.is_err());
}
}