fn from_tree(top: &expression::Tree) -> Result<Descriptor<Pk>, Error> {
        match (top.name, top.args.len() as u32) {
            ("pk", 1) => {
                expression::terminal(&top.args[0], |pk| Pk::from_str(pk).map(Descriptor::Pk))
            }
            ("pkh", 1) => {
                expression::terminal(&top.args[0], |pk| Pk::from_str(pk).map(Descriptor::Pkh))
            }
            ("wpkh", 1) => {
                let wpkh = expression::terminal(&top.args[0], |pk| Pk::from_str(pk))?;
                if wpkh.is_uncompressed() {
                    Err(Error::ContextError(ScriptContextError::CompressedOnly))
                } else {
                    Ok(Descriptor::Wpkh(wpkh))
                }
            }
            ("sh", 1) => {
                let newtop = &top.args[0];
                match (newtop.name, newtop.args.len()) {
                    ("wsh", 1) => {
                        let sub = Miniscript::from_tree(&newtop.args[0])?;
                        if sub.ty.corr.base != miniscript::types::Base::B {
                            Err(Error::NonTopLevel(format!("{:?}", sub)))
                        } else {
                            Ok(Descriptor::ShWsh(sub))
                        }
                    }
                    ("wpkh", 1) => {
                        let wpkh = expression::terminal(&newtop.args[0], |pk| Pk::from_str(pk))?;
                        if wpkh.is_uncompressed() {
                            Err(Error::ContextError(ScriptContextError::CompressedOnly))
                        } else {
                            Ok(Descriptor::ShWpkh(wpkh))
                        }
                    }
                    _ => {
                        let sub = Miniscript::from_tree(&top.args[0])?;
                        if sub.ty.corr.base != miniscript::types::Base::B {
                            Err(Error::NonTopLevel(format!("{:?}", sub)))
                        } else {
                            Ok(Descriptor::Sh(sub))
                        }
                    }
                }
            }
            ("wsh", 1) => {
                let sub = Miniscript::from_tree(&top.args[0])?;
                if sub.ty.corr.base != miniscript::types::Base::B {
                    Err(Error::NonTopLevel(format!("{:?}", sub)))
                } else {
                    Ok(Descriptor::Wsh(sub))
                }
            }
            _ => {
                let sub = Miniscript::from_tree(&top)?;
                if sub.ty.corr.base != miniscript::types::Base::B {
                    Err(Error::NonTopLevel(format!("{:?}", sub)))
                } else {
                    Ok(Descriptor::Bare(sub))
                }
            } //TODO: If required add string parsing logic
        }
    }
/// Parse an expression tree into a descriptor
Parse an expression tree into a descriptor
Parse an expression tree into a descriptor
fn from_tree(top: &expression::Tree) -> Result<Descriptor<Pk>, Error> {
        match (top.name, top.args.len() as u32) {
            ("pk", 1) => {
                expression::terminal(&top.args[0], |pk| Pk::from_str(pk).map(Descriptor::Pk))
            }
            ("pkh", 1) => {
                expression::terminal(&top.args[0], |pk| Pk::from_str(pk).map(Descriptor::Pkh))
            }
            ("wpkh", 1) => {
                let wpkh = expression::terminal(&top.args[0], |pk| Pk::from_str(pk))?;
                if wpkh.is_uncompressed() {
                    Err(Error::ContextError(ScriptContextError::CompressedOnly))
                } else {
                    Ok(Descriptor::Wpkh(wpkh))
                }
            }
            ("sh", 1) => {
                let newtop = &top.args[0];
                match (newtop.name, newtop.args.len()) {
                    ("wsh", 1) => {
                        let sub = Miniscript::from_tree(&newtop.args[0])?;
                        if sub.ty.corr.base != miniscript::types::Base::B {
                            Err(Error::NonTopLevel(format!("{:?}", sub)))
                        } else {
                            Ok(Descriptor::ShWsh(sub))
                        }
                    }
                    ("wpkh", 1) => {
                        let wpkh = expression::terminal(&newtop.args[0], |pk| Pk::from_str(pk))?;
                        if wpkh.is_uncompressed() {
                            Err(Error::ContextError(ScriptContextError::CompressedOnly))
                        } else {
                            Ok(Descriptor::ShWpkh(wpkh))
                        }
                    }
                    _ => {
                        let sub = Miniscript::from_tree(&top.args[0])?;
                        if sub.ty.corr.base != miniscript::types::Base::B {
                            Err(Error::NonTopLevel(format!("{:?}", sub)))
                        } else {
                            Ok(Descriptor::Sh(sub))
                        }
                    }
                }
            }
            ("wsh", 1) => {
                let sub = Miniscript::from_tree(&top.args[0])?;
                if sub.ty.corr.base != miniscript::types::Base::B {
                    Err(Error::NonTopLevel(format!("{:?}", sub)))
                } else {
                    Ok(Descriptor::Wsh(sub))
                }
            }
            _ => {
                let sub = Miniscript::from_tree(&top)?;
                if sub.ty.corr.base != miniscript::types::Base::B {
                    Err(Error::NonTopLevel(format!("{:?}", sub)))
                } else {
                    Ok(Descriptor::Bare(sub))
                }
            } //TODO: If required add string parsing logic
        }
    }
fn from_tree ( top : & expression :: Tree ) -> Result < Descriptor < Pk > , Error > { match ( top . name , top . args . len ( ) as u32 ) { ( " " , 1 ) => { expression :: terminal ( & top . args [ 0 ] , | pk | Pk :: from_str ( pk ) . map ( Descriptor :: Pk ) ) } ( " " , 1 ) => { expression :: terminal ( & top . args [ 0 ] , | pk | Pk :: from_str ( pk ) . map ( Descriptor :: Pkh ) ) } ( " " , 1 ) => { let wpkh = expression :: terminal ( & top . args [ 0 ] , | pk | Pk :: from_str ( pk ) ) ? ; if wpkh . is_uncompressed ( ) { Err ( Error :: ContextError ( ScriptContextError :: CompressedOnly ) ) } else { Ok ( Descriptor :: Wpkh ( wpkh ) ) } } ( " " , 1 ) => { let newtop = & top . args [ 0 ] ; match ( newtop . name , newtop . args . len ( ) ) { ( " " , 1 ) => { let sub = Miniscript :: from_tree ( & newtop . args [ 0 ] ) ? ; if sub . ty . corr . base != miniscript :: types :: Base :: B { Err ( Error :: NonTopLevel ( format ! ( " " sub ) ) ) } else { Ok ( Descriptor :: ShWsh ( sub ) ) } } ( " " , 1 ) => { let wpkh = expression :: terminal ( & newtop . args [ 0 ] , | pk | Pk :: from_str ( pk ) ) ? ; if wpkh . is_uncompressed ( ) { Err ( Error :: ContextError ( ScriptContextError :: CompressedOnly ) ) } else { Ok ( Descriptor :: ShWpkh ( wpkh ) ) } } _ => { let sub = Miniscript :: from_tree ( & top . args [ 0 ] ) ? ; if sub . ty . corr . base != miniscript :: types :: Base :: B { Err ( Error :: NonTopLevel ( format ! ( " " sub ) ) ) } else { Ok ( Descriptor :: Sh ( sub ) ) } } } } ( " " , 1 ) => { let sub = Miniscript :: from_tree ( & top . args [ 0 ] ) ? ; if sub . ty . corr . base != miniscript :: types :: Base :: B { Err ( Error :: NonTopLevel ( format ! ( " " sub ) ) ) } else { Ok ( Descriptor :: Wsh ( sub ) ) } } _ => { let sub = Miniscript :: from_tree ( & top ) ? ; if sub . ty . corr . base != miniscript :: types :: Base :: B { Err ( Error :: NonTopLevel ( format ! ( " " sub ) ) ) } else { Ok ( Descriptor :: Bare ( sub ) ) } } //TODO: If required add string parsing logic } }
Parse an expression tree into a descriptor
Parse an expression tree into a descriptor
top
&expression::Tree
top
&expression::Tree