|
| 1 | +use acvm::AcirField; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use crate::PrintOutput; |
| 5 | + |
| 6 | +use super::{ |
| 7 | + layers::{self, Layer, Layering}, |
| 8 | + mocker::MockForeignCallExecutor, |
| 9 | + print::PrintForeignCallExecutor, |
| 10 | + ForeignCallExecutor, |
| 11 | +}; |
| 12 | + |
| 13 | +#[cfg(feature = "rpc")] |
| 14 | +use super::rpc::RPCForeignCallExecutor; |
| 15 | + |
| 16 | +/// A builder for [DefaultForeignCallLayers] where we can enable fields based on feature flags, |
| 17 | +/// which is easier than providing different overrides for a `new` method. |
| 18 | +#[derive(Default)] |
| 19 | +pub struct DefaultForeignCallBuilder<'a> { |
| 20 | + pub output: PrintOutput<'a>, |
| 21 | + #[cfg(feature = "rpc")] |
| 22 | + pub resolver_url: Option<String>, |
| 23 | + #[cfg(feature = "rpc")] |
| 24 | + pub root_path: Option<std::path::PathBuf>, |
| 25 | + #[cfg(feature = "rpc")] |
| 26 | + pub package_name: Option<String>, |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a> DefaultForeignCallBuilder<'a> { |
| 30 | + /// Override the output. |
| 31 | + pub fn with_output(mut self, output: PrintOutput<'a>) -> Self { |
| 32 | + self.output = output; |
| 33 | + self |
| 34 | + } |
| 35 | + |
| 36 | + /// Compose the executor layers with [layers::Empty] as the default handler. |
| 37 | + pub fn build<F>(self) -> DefaultForeignCallLayers<'a, layers::Empty, F> |
| 38 | + where |
| 39 | + F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a, |
| 40 | + { |
| 41 | + self.build_with_base(layers::Empty) |
| 42 | + } |
| 43 | + |
| 44 | + /// Compose the executor layers with `base` as the default handler. |
| 45 | + pub fn build_with_base<B, F>(self, base: B) -> DefaultForeignCallLayers<'a, B, F> |
| 46 | + where |
| 47 | + F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a, |
| 48 | + B: ForeignCallExecutor<F> + 'a, |
| 49 | + { |
| 50 | + let executor = { |
| 51 | + #[cfg(feature = "rpc")] |
| 52 | + { |
| 53 | + use rand::Rng; |
| 54 | + |
| 55 | + base.add_layer(self.resolver_url.map(|resolver_url| { |
| 56 | + let id = rand::thread_rng().gen(); |
| 57 | + RPCForeignCallExecutor::new( |
| 58 | + &resolver_url, |
| 59 | + id, |
| 60 | + self.root_path, |
| 61 | + self.package_name, |
| 62 | + ) |
| 63 | + })) |
| 64 | + } |
| 65 | + #[cfg(not(feature = "rpc"))] |
| 66 | + { |
| 67 | + base |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + executor |
| 72 | + .add_layer(MockForeignCallExecutor::default()) |
| 73 | + .add_layer(PrintForeignCallExecutor::new(self.output)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// Facilitate static typing of layers on a base layer, so inner layers can be accessed. |
| 78 | +#[cfg(feature = "rpc")] |
| 79 | +pub type DefaultForeignCallLayers<'a, B, F> = Layer< |
| 80 | + PrintForeignCallExecutor<'a>, |
| 81 | + Layer<MockForeignCallExecutor<F>, Layer<Option<RPCForeignCallExecutor>, B>>, |
| 82 | +>; |
| 83 | +#[cfg(not(feature = "rpc"))] |
| 84 | +pub type DefaultForeignCallLayers<'a, B, F> = |
| 85 | + Layer<PrintForeignCallExecutor<'a>, Layer<MockForeignCallExecutor<F>, B>>; |
| 86 | + |
| 87 | +/// Convenience constructor for code that used to create the executor this way. |
| 88 | +#[cfg(feature = "rpc")] |
| 89 | +pub struct DefaultForeignCallExecutor; |
| 90 | + |
| 91 | +/// Convenience constructors for the RPC case. Non-RPC versions are not provided |
| 92 | +/// because once a crate opts into this within the workspace, everyone gets it |
| 93 | +/// even if they don't want to. For the non-RPC case we can nudge people to |
| 94 | +/// use `DefaultForeignCallBuilder` which is easier to keep flexible. |
| 95 | +#[cfg(feature = "rpc")] |
| 96 | +impl DefaultForeignCallExecutor { |
| 97 | + #[allow(clippy::new_ret_no_self)] |
| 98 | + pub fn new<'a, F>( |
| 99 | + output: PrintOutput<'a>, |
| 100 | + resolver_url: Option<&str>, |
| 101 | + root_path: Option<std::path::PathBuf>, |
| 102 | + package_name: Option<String>, |
| 103 | + ) -> impl ForeignCallExecutor<F> + 'a |
| 104 | + where |
| 105 | + F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a, |
| 106 | + { |
| 107 | + DefaultForeignCallBuilder { |
| 108 | + output, |
| 109 | + resolver_url: resolver_url.map(|s| s.to_string()), |
| 110 | + root_path, |
| 111 | + package_name, |
| 112 | + } |
| 113 | + .build() |
| 114 | + } |
| 115 | +} |
0 commit comments