fn decode_policy_inner<Pk>(
            d: &mut impl io::Read,
            mut depth: u8,
        ) -> Result<Policy<Pk>, Error>
        where
            Pk: MiniscriptKey + StrictDecode,
            <Pk as MiniscriptKey>::Hash: StrictDecode,
        {
            if depth > MINISCRIPT_DEPTH_LIMIT {
                return Err(Error::ExceedMaxItems(
                    MINISCRIPT_DEPTH_LIMIT as usize,
                ));
            }
            depth += 1;

            let byte = d.read_u8()?;
            Ok(match byte {
                MS_TRUE => Policy::Trivial,
                MS_FALSE => Policy::Unsatisfiable,
                MS_KEY => Policy::Key(Pk::strict_decode(d)?),
                MS_AFTER => Policy::After(u32::strict_decode(d)?),
                MS_OLDER => Policy::Older(u32::strict_decode(d)?),
                MS_SHA256 => Policy::Sha256(sha256::Hash::strict_decode(d)?),
                MS_HASH256 => Policy::Hash256(sha256d::Hash::strict_decode(d)?),
                MS_RIPEMD160 => {
                    Policy::Ripemd160(ripemd160::Hash::strict_decode(d)?)
                }
                MS_HASH160 => Policy::Hash160(hash160::Hash::strict_decode(d)?),
                MS_AND_B => {
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push(decode_policy_inner(d, depth)?);
                    }
                    Policy::And(vec)
                }
                MS_OR_B => {
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push((
                            d.read_u16()? as usize,
                            decode_policy_inner(d, depth)?,
                        ));
                    }
                    Policy::Or(vec)
                }
                MS_THRESH => {
                    let thresh = d.read_u16()? as usize;
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push(decode_policy_inner(d, depth)?);
                    }
                    Policy::Threshold(thresh, vec)
                }

                MS_KEY_HASH | MS_ALT | MS_SWAP | MS_CHECK | MS_DUP_IF
                | MS_VERIFY | MS_NON_ZERO | MS_ZERO_NE | MS_AND_V
                | MS_AND_OR | MS_OR_D | MS_OR_C | MS_OR_I | MS_MULTI => {
                    return Err(Error::DataIntegrityError(format!(
                        "byte {:#04X} is a valid miniscript instruction, but \
                         does  not belong to a set of concrete policy \
                         instructions. Try to decode data using different \
                         miniscript type",
                        byte
                    )))
                }

                wrong => {
                    return Err(Error::DataIntegrityError(format!(
                        "byte {:#04X} does not correspond to any of \
                         miniscript concrete policy instructions",
                        wrong
                    )))
                }
            })
        }
// We need this first because of the need to control maximum number of
// nested miniscript code, and than because of the rust compiler
// limitations in working with recursive generic functions
We need this first because of the need to control maximum number of
nested miniscript code, and than because of the rust compiler
limitations in working with recursive generic functions
We need this first because of the need to control maximum number of nested miniscript code and than because of the rust compiler limitations in working with recursive generic functions
fn decode_policy_inner<Pk>(
            d: &mut impl io::Read,
            mut depth: u8,
        ) -> Result<Policy<Pk>, Error>
        where
            Pk: MiniscriptKey + StrictDecode,
            <Pk as MiniscriptKey>::Hash: StrictDecode,
        {
            if depth > MINISCRIPT_DEPTH_LIMIT {
                return Err(Error::ExceedMaxItems(
                    MINISCRIPT_DEPTH_LIMIT as usize,
                ));
            }
            depth += 1;
            let byte = d.read_u8()?;
            Ok(match byte {
                MS_TRUE => Policy::Trivial,
                MS_FALSE => Policy::Unsatisfiable,
                MS_KEY => Policy::Key(Pk::strict_decode(d)?),
                MS_AFTER => Policy::After(u32::strict_decode(d)?),
                MS_OLDER => Policy::Older(u32::strict_decode(d)?),
                MS_SHA256 => Policy::Sha256(sha256::Hash::strict_decode(d)?),
                MS_HASH256 => Policy::Hash256(sha256d::Hash::strict_decode(d)?),
                MS_RIPEMD160 => {
                    Policy::Ripemd160(ripemd160::Hash::strict_decode(d)?)
                }
                MS_HASH160 => Policy::Hash160(hash160::Hash::strict_decode(d)?),
                MS_AND_B => {
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push(decode_policy_inner(d, depth)?);
                    }
                    Policy::And(vec)
                }
                MS_OR_B => {
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push((
                            d.read_u16()? as usize,
                            decode_policy_inner(d, depth)?,
                        ));
                    }
                    Policy::Or(vec)
                }
                MS_THRESH => {
                    let thresh = d.read_u16()? as usize;
                    let len = d.read_u16()?;
                    let mut vec = Vec::with_capacity(len as usize);
                    for _ in 0..len {
                        vec.push(decode_policy_inner(d, depth)?);
                    }
                    Policy::Threshold(thresh, vec)
                }
                MS_KEY_HASH | MS_ALT | MS_SWAP | MS_CHECK | MS_DUP_IF
                | MS_VERIFY | MS_NON_ZERO | MS_ZERO_NE | MS_AND_V
                | MS_AND_OR | MS_OR_D | MS_OR_C | MS_OR_I | MS_MULTI => {
                    return Err(Error::DataIntegrityError(format!(
                        "byte {:#04X} is a valid miniscript instruction, but \
                         does  not belong to a set of concrete policy \
                         instructions. Try to decode data using different \
                         miniscript type",
                        byte
                    )))
                }
                wrong => {
                    return Err(Error::DataIntegrityError(format!(
                        "byte {:#04X} does not correspond to any of \
                         miniscript concrete policy instructions",
                        wrong
                    )))
                }
            })
        }
fn decode_policy_inner < Pk > ( d : & mut impl io :: Read , mut depth : u8 , ) -> Result < Policy < Pk > , Error > where Pk : MiniscriptKey + StrictDecode , < Pk as MiniscriptKey > :: Hash : StrictDecode , { if depth > MINISCRIPT_DEPTH_LIMIT { return Err ( Error :: ExceedMaxItems ( MINISCRIPT_DEPTH_LIMIT as usize , ) ) ; } depth += 1 ; let byte = d . read_u8 ( ) ? ; Ok ( match byte { MS_TRUE => Policy :: Trivial , MS_FALSE => Policy :: Unsatisfiable , MS_KEY => Policy :: Key ( Pk :: strict_decode ( d ) ? ) , MS_AFTER => Policy :: After ( u32 :: strict_decode ( d ) ? ) , MS_OLDER => Policy :: Older ( u32 :: strict_decode ( d ) ? ) , MS_SHA256 => Policy :: Sha256 ( sha256 :: Hash :: strict_decode ( d ) ? ) , MS_HASH256 => Policy :: Hash256 ( sha256d :: Hash :: strict_decode ( d ) ? ) , MS_RIPEMD160 => { Policy :: Ripemd160 ( ripemd160 :: Hash :: strict_decode ( d ) ? ) } MS_HASH160 => Policy :: Hash160 ( hash160 :: Hash :: strict_decode ( d ) ? ) , MS_AND_B => { let len = d . read_u16 ( ) ? ; let mut vec = Vec :: with_capacity ( len as usize ) ; for _ in 0 .. len { vec . push ( decode_policy_inner ( d , depth ) ? ) ; } Policy :: And ( vec ) } MS_OR_B => { let len = d . read_u16 ( ) ? ; let mut vec = Vec :: with_capacity ( len as usize ) ; for _ in 0 .. len { vec . push ( ( d . read_u16 ( ) ? as usize , decode_policy_inner ( d , depth ) ? , ) ) ; } Policy :: Or ( vec ) } MS_THRESH => { let thresh = d . read_u16 ( ) ? as usize ; let len = d . read_u16 ( ) ? ; let mut vec = Vec :: with_capacity ( len as usize ) ; for _ in 0 .. len { vec . push ( decode_policy_inner ( d , depth ) ? ) ; } Policy :: Threshold ( thresh , vec ) } MS_KEY_HASH | MS_ALT | MS_SWAP | MS_CHECK | MS_DUP_IF | MS_VERIFY | MS_NON_ZERO | MS_ZERO_NE | MS_AND_V | MS_AND_OR | MS_OR_D | MS_OR_C | MS_OR_I | MS_MULTI => { return Err ( Error :: DataIntegrityError ( format ! ( " \
 \
 \
 " byte ) ) ) } wrong => { return Err ( Error :: DataIntegrityError ( format ! ( " \
 " wrong ) ) ) } } ) }
We need this first because of the need to control maximum number of
nested miniscript code, and than because of the rust compiler
limitations in working with recursive generic functions
We need this first because of the need to control maximum number of nested miniscript code and than because of the rust compiler limitations in working with recursive generic functions
d
&mut impl io::Read
depth
u8
d
&mut impl io::Read
depth
u8