use alloc::{boxed::Box, vec::Vec};
use core::fmt;
use miden_formatting::{
hex::ToHex,
prettier::{Document, PrettyPrint, const_text, text},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{MastForestContributor, MastNodeExt};
use crate::{
Felt, Word,
mast::{MastForest, MastForestError, MastNodeId},
utils::LookupByIdx,
};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(all(feature = "arbitrary", test), miden_test_serde_macros::serde_test)]
pub struct ExternalNode {
digest: Word,
}
impl ExternalNode {
pub(super) fn to_display<'a>(&'a self, _mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
self.clone()
}
pub(super) fn to_pretty_print<'a>(
&'a self,
_mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
self.clone()
}
}
impl PrettyPrint for ExternalNode {
fn render(&self) -> Document {
const_text("external") + const_text(".") + text(self.digest.as_bytes().to_hex_with_prefix())
}
}
impl fmt::Display for ExternalNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use crate::prettier::PrettyPrint;
self.pretty_print(f)
}
}
impl MastNodeExt for ExternalNode {
fn digest(&self) -> Word {
self.digest
}
fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn fmt::Display + 'a> {
Box::new(ExternalNode::to_display(self, mast_forest))
}
fn to_pretty_print<'a>(&'a self, mast_forest: &'a MastForest) -> Box<dyn PrettyPrint + 'a> {
Box::new(ExternalNode::to_pretty_print(self, mast_forest))
}
fn has_children(&self) -> bool {
false
}
fn append_children_to(&self, _target: &mut Vec<MastNodeId>) {
}
fn for_each_child<F>(&self, _f: F)
where
F: FnMut(MastNodeId),
{
}
fn domain(&self) -> Felt {
panic!("Can't fetch domain for an `External` node.")
}
type Builder = ExternalNodeBuilder;
fn to_builder(self, _forest: &MastForest) -> Self::Builder {
ExternalNodeBuilder::new(self.digest)
}
}
#[cfg(all(feature = "arbitrary", test))]
impl proptest::prelude::Arbitrary for ExternalNode {
type Parameters = ();
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
use crate::Felt;
any::<[u64; 4]>()
.prop_map(|[a, b, c, d]| {
let word = Word::from([Felt::new_unchecked(a), Felt::new_unchecked(b), Felt::new_unchecked(c), Felt::new_unchecked(d)]);
ExternalNodeBuilder::new(word).build()
})
.no_shrink() .boxed()
}
type Strategy = proptest::prelude::BoxedStrategy<Self>;
}
#[derive(Debug)]
pub struct ExternalNodeBuilder {
digest: Word,
}
impl ExternalNodeBuilder {
pub fn new(digest: Word) -> Self {
Self { digest }
}
pub fn build(self) -> ExternalNode {
ExternalNode { digest: self.digest }
}
}
impl MastForestContributor for ExternalNodeBuilder {
fn add_to_forest(self, forest: &mut MastForest) -> Result<MastNodeId, MastForestError> {
let node_id = forest
.nodes
.push(ExternalNode { digest: self.digest }.into())
.map_err(|_| MastForestError::TooManyNodes)?;
Ok(node_id)
}
fn fingerprint_for_node(
&self,
_forest: &MastForest,
_hash_by_node_id: &impl LookupByIdx<MastNodeId, Word>,
) -> Result<Word, MastForestError> {
Ok(self.digest)
}
fn remap_children(self, _remapping: &impl LookupByIdx<MastNodeId, MastNodeId>) -> Self {
self
}
fn with_digest(mut self, digest: Word) -> Self {
self.digest = digest;
self
}
}
impl ExternalNodeBuilder {
pub(in crate::mast) fn add_to_forest_relaxed(
self,
forest: &mut MastForest,
) -> Result<MastNodeId, MastForestError> {
let node_id = forest
.nodes
.push(ExternalNode { digest: self.digest }.into())
.map_err(|_| MastForestError::TooManyNodes)?;
Ok(node_id)
}
}
#[cfg(any(test, feature = "arbitrary"))]
impl proptest::prelude::Arbitrary for ExternalNodeBuilder {
type Parameters = ExternalNodeBuilderParams;
type Strategy = proptest::strategy::BoxedStrategy<Self>;
fn arbitrary_with(params: Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
let _ = params;
any::<[u64; 4]>()
.prop_map(|[a, b, c, d]| {
Word::new([
Felt::new_unchecked(a),
Felt::new_unchecked(b),
Felt::new_unchecked(c),
Felt::new_unchecked(d),
])
})
.prop_map(Self::new)
.boxed()
}
}
#[cfg(any(test, feature = "arbitrary"))]
#[derive(Clone, Debug, Default)]
pub struct ExternalNodeBuilderParams {}