|
| 1 | +// Copyright 2020 Contributors to the Parsec project. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Lists the supported opcodes for a given provider. |
| 5 | +
|
| 6 | +use crate::cli::ParsecToolApp; |
| 7 | +use crate::error::ParsecToolError; |
| 8 | +use crate::subcommands::common::ProviderOpts; |
| 9 | +use crate::subcommands::ParsecToolSubcommand; |
| 10 | +use parsec_client::core::interface::requests::ProviderID; |
| 11 | +use parsec_client::core::operation_client::OperationClient; |
| 12 | +use parsec_interface::operations::list_opcodes; |
| 13 | +use parsec_interface::operations::{NativeOperation, NativeResult}; |
| 14 | +use std::convert::TryFrom; |
| 15 | +use structopt::StructOpt; |
| 16 | + |
| 17 | +/// Lists the supported opcodes for a given provider. |
| 18 | +#[derive(Copy, Clone, Debug, StructOpt)] |
| 19 | +#[structopt(name = "list_opcodes")] |
| 20 | +pub struct ListOpcodesSubcommand { |
| 21 | + #[structopt(flatten)] |
| 22 | + provider_opts: ProviderOpts, |
| 23 | +} |
| 24 | + |
| 25 | +impl TryFrom<ListOpcodesSubcommand> for NativeOperation { |
| 26 | + type Error = ParsecToolError; |
| 27 | + |
| 28 | + fn try_from(list_opcodes_subcommand: ListOpcodesSubcommand) -> Result<Self, Self::Error> { |
| 29 | + // Trivially converted to a `NativeOperation`. |
| 30 | + Ok(NativeOperation::ListOpcodes(list_opcodes::Operation { |
| 31 | + provider_id: ProviderID::try_from(list_opcodes_subcommand.provider_opts.provider)?, |
| 32 | + })) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl ParsecToolSubcommand for ListOpcodesSubcommand { |
| 37 | + /// Lists the supported opcodes for a given provider. |
| 38 | + fn run(&self, matches: &ParsecToolApp) -> Result<(), ParsecToolError> { |
| 39 | + let client = OperationClient::new(); |
| 40 | + let native_result = client.process_operation( |
| 41 | + NativeOperation::try_from(*self)?, |
| 42 | + // We still use the core provider beacuse listing opcodes is a core operation. Note the |
| 43 | + // distinction between the provider we're _using_ and the provider we're querying. |
| 44 | + ProviderID::Core, |
| 45 | + &matches.authentication_data(), |
| 46 | + )?; |
| 47 | + |
| 48 | + if let NativeResult::ListOpcodes(result) = native_result { |
| 49 | + info!( |
| 50 | + "Available opcodes for provider {:?}:", |
| 51 | + ProviderID::try_from(self.provider_opts.provider)? |
| 52 | + ); |
| 53 | + for provider_opcode in result.opcodes { |
| 54 | + eprint_colored!(Blue, "*"); |
| 55 | + eprintln!(" {:?}", provider_opcode); |
| 56 | + } |
| 57 | + Ok(()) |
| 58 | + } else { |
| 59 | + Err(ParsecToolError::UnexpectedNativeResult(native_result)) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments