use alloc::{boxed::Box, vec::Vec};
use core::fmt;
use miden_crypto::{Felt, Word};
use miden_formatting::{
hex::ToHex,
prettier::{Document, PrettyPrint, const_text, nl, text},
};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::{MastNodeErrorContext, MastNodeExt};
use crate::mast::{DecoratedOpLink, DecoratorId, MastForest, MastNodeId, Remapping};
#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ExternalNode {
digest: Word,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Vec::is_empty"))]
before_enter: Vec<DecoratorId>,
#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Vec::is_empty"))]
after_exit: Vec<DecoratorId>,
}
impl ExternalNode {
pub fn new(procedure_hash: Word) -> Self {
Self {
digest: procedure_hash,
before_enter: Vec::new(),
after_exit: Vec::new(),
}
}
}
impl MastNodeErrorContext for ExternalNode {
fn decorators(&self) -> impl Iterator<Item = DecoratedOpLink> {
self.before_enter.iter().chain(&self.after_exit).copied().enumerate()
}
}
impl ExternalNode {
pub(super) fn to_display<'a>(&'a self, mast_forest: &'a MastForest) -> impl fmt::Display + 'a {
ExternalNodePrettyPrint { node: self, mast_forest }
}
pub(super) fn to_pretty_print<'a>(
&'a self,
mast_forest: &'a MastForest,
) -> impl PrettyPrint + 'a {
ExternalNodePrettyPrint { node: self, mast_forest }
}
}
struct ExternalNodePrettyPrint<'a> {
node: &'a ExternalNode,
mast_forest: &'a MastForest,
}
impl ExternalNodePrettyPrint<'_> {
fn concatenate_decorators(
&self,
decorator_ids: &[DecoratorId],
prepend: Document,
append: Document,
) -> Document {
let decorators = decorator_ids
.iter()
.map(|&decorator_id| self.mast_forest[decorator_id].render())
.reduce(|acc, doc| acc + const_text(" ") + doc)
.unwrap_or_default();
if decorators.is_empty() {
decorators
} else {
prepend + decorators + append
}
}
fn single_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, const_text(" "))
}
fn single_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), const_text(" "), Document::Empty)
}
fn multi_line_pre_decorators(&self) -> Document {
self.concatenate_decorators(self.node.before_enter(), Document::Empty, nl())
}
fn multi_line_post_decorators(&self) -> Document {
self.concatenate_decorators(self.node.after_exit(), nl(), Document::Empty)
}
}
impl crate::prettier::PrettyPrint for ExternalNodePrettyPrint<'_> {
fn render(&self) -> crate::prettier::Document {
let external = const_text("external")
+ const_text(".")
+ text(self.node.digest.as_bytes().to_hex_with_prefix());
let single_line = self.single_line_pre_decorators()
+ external.clone()
+ self.single_line_post_decorators();
let multi_line =
self.multi_line_pre_decorators() + external + self.multi_line_post_decorators();
single_line | multi_line
}
}
impl fmt::Display for ExternalNodePrettyPrint<'_> {
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 before_enter(&self) -> &[DecoratorId] {
&self.before_enter
}
fn after_exit(&self) -> &[DecoratorId] {
&self.after_exit
}
fn append_before_enter(&mut self, decorator_ids: &[DecoratorId]) {
self.before_enter.extend_from_slice(decorator_ids);
}
fn append_after_exit(&mut self, decorator_ids: &[DecoratorId]) {
self.after_exit.extend_from_slice(decorator_ids);
}
fn remove_decorators(&mut self) {
self.before_enter.truncate(0);
self.after_exit.truncate(0);
}
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 remap_children(&self, _remapping: &Remapping) -> Self {
self.clone()
}
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.")
}
}