#[cfg(test)]
use crate::sns::commands::{
options::common::parse_sns_matches,
spec::{
sns_proposal_cache_list_command, sns_proposal_cache_status_command,
sns_proposal_info_command, sns_proposal_list_command, sns_proposal_refresh_command,
},
};
use crate::{
cli::{
clap::{required_string, required_typed, string_option, typed_option},
common::{OutputFormat, output_format},
},
sns::commands::{
SnsCommandError,
options::lookup::SnsLookupOptions,
spec::{
SNS_PROPOSALS_LOCAL_SORT_VALUE_NAME, SnsProposalEligibilityArg, SnsProposalStatusArg,
SnsProposalTopicArg, SnsProposalsSortArg,
},
},
};
use clap::ArgMatches;
use ic_query::sns::{SnsProposalSortDirection, SnsProposalsSort};
#[cfg(test)]
use std::ffi::OsString;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::sns::commands) struct SnsProposalsOptions {
pub(in crate::sns::commands) lookup: SnsLookupOptions,
pub(in crate::sns::commands) limit: u32,
pub(in crate::sns::commands) before_proposal_id: Option<u64>,
pub(in crate::sns::commands) status: SnsProposalStatusArg,
pub(in crate::sns::commands) topic: SnsProposalTopicArg,
pub(in crate::sns::commands) eligibility: SnsProposalEligibilityArg,
pub(in crate::sns::commands) proposer_neuron_id: Option<String>,
pub(in crate::sns::commands) query: Option<String>,
pub(in crate::sns::commands) sort: SnsProposalsSortArg,
pub(in crate::sns::commands) sort_direction: SnsProposalSortDirection,
pub(in crate::sns::commands) verbose: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::sns::commands) struct SnsProposalOptions {
pub(in crate::sns::commands) lookup: SnsLookupOptions,
pub(in crate::sns::commands) proposal_id: u64,
pub(in crate::sns::commands) verbose: bool,
pub(in crate::sns::commands) show_ballots: bool,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::sns::commands) struct SnsProposalsCacheListOptions {
pub(in crate::sns::commands) network: String,
pub(in crate::sns::commands) format: OutputFormat,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::sns::commands) struct SnsProposalsCacheStatusOptions {
pub(in crate::sns::commands) input: String,
pub(in crate::sns::commands) network: String,
pub(in crate::sns::commands) format: OutputFormat,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::sns::commands) struct SnsProposalsRefreshOptions {
pub(in crate::sns::commands) lookup: SnsLookupOptions,
pub(in crate::sns::commands) page_size: u32,
pub(in crate::sns::commands) max_pages: Option<u32>,
}
impl SnsProposalsOptions {
pub(in crate::sns::commands) fn from_matches(
matches: &ArgMatches,
network: &str,
) -> Result<Self, SnsCommandError> {
let status = required_typed(matches, "status");
let sort = required_typed(matches, "sort");
let sort_direction = proposal_sort_direction(matches, sort)?;
Ok(Self {
lookup: SnsLookupOptions::from_matches(matches, network),
limit: required_typed(matches, "limit"),
before_proposal_id: typed_option::<u64>(matches, "before"),
status,
topic: required_typed(matches, "topic"),
eligibility: required_typed(matches, "eligible"),
proposer_neuron_id: string_option(matches, "proposer"),
query: string_option(matches, "query"),
sort,
sort_direction,
verbose: matches.get_flag("verbose"),
})
}
#[cfg(test)]
pub(in crate::sns::commands) fn parse<I>(args: I) -> Result<Self, SnsCommandError>
where
I: IntoIterator<Item = OsString>,
{
let matches = parse_sns_matches(sns_proposal_list_command(), args)?;
Self::from_matches(&matches, ic_query::subnet_catalog::MAINNET_NETWORK)
}
}
fn proposal_sort_direction(
matches: &ArgMatches,
sort: SnsProposalsSortArg,
) -> Result<SnsProposalSortDirection, SnsCommandError> {
if matches.get_flag("asc") {
return explicit_proposal_sort_direction(sort, SnsProposalSortDirection::Asc, "--asc");
}
if matches.get_flag("desc") {
return explicit_proposal_sort_direction(sort, SnsProposalSortDirection::Desc, "--desc");
}
Ok(SnsProposalsSort::from(sort).default_direction())
}
fn explicit_proposal_sort_direction(
sort: SnsProposalsSortArg,
direction: SnsProposalSortDirection,
flag: &'static str,
) -> Result<SnsProposalSortDirection, SnsCommandError> {
if !SnsProposalsSort::from(sort).uses_local_direction() {
return Err(SnsCommandError::Usage(format!(
"{flag} requires --sort {SNS_PROPOSALS_LOCAL_SORT_VALUE_NAME}"
)));
}
Ok(direction)
}
impl SnsProposalOptions {
pub(in crate::sns::commands) fn from_matches(matches: &ArgMatches, network: &str) -> Self {
Self {
lookup: SnsLookupOptions::from_matches(matches, network),
proposal_id: required_typed(matches, "proposal-id"),
verbose: matches.get_flag("verbose"),
show_ballots: matches.get_flag("ballots"),
}
}
#[cfg(test)]
pub(in crate::sns::commands) fn parse<I>(args: I) -> Result<Self, SnsCommandError>
where
I: IntoIterator<Item = OsString>,
{
let matches = parse_sns_matches(sns_proposal_info_command(), args)?;
Ok(Self::from_matches(
&matches,
ic_query::subnet_catalog::MAINNET_NETWORK,
))
}
}
impl SnsProposalsCacheListOptions {
pub(in crate::sns::commands) fn from_matches(matches: &ArgMatches, network: &str) -> Self {
Self {
network: network.to_string(),
format: output_format(matches),
}
}
#[cfg(test)]
pub(in crate::sns::commands) fn parse<I>(args: I) -> Result<Self, SnsCommandError>
where
I: IntoIterator<Item = OsString>,
{
let matches = parse_sns_matches(sns_proposal_cache_list_command(), args)?;
Ok(Self::from_matches(
&matches,
ic_query::subnet_catalog::MAINNET_NETWORK,
))
}
}
impl SnsProposalsCacheStatusOptions {
pub(in crate::sns::commands) fn from_matches(matches: &ArgMatches, network: &str) -> Self {
Self {
input: required_string(matches, "input"),
network: network.to_string(),
format: output_format(matches),
}
}
#[cfg(test)]
pub(in crate::sns::commands) fn parse<I>(args: I) -> Result<Self, SnsCommandError>
where
I: IntoIterator<Item = OsString>,
{
let matches = parse_sns_matches(sns_proposal_cache_status_command(), args)?;
Ok(Self::from_matches(
&matches,
ic_query::subnet_catalog::MAINNET_NETWORK,
))
}
}
impl SnsProposalsRefreshOptions {
pub(in crate::sns::commands) fn from_matches(matches: &ArgMatches, network: &str) -> Self {
Self {
lookup: SnsLookupOptions::from_matches(matches, network),
page_size: required_typed(matches, "page-size"),
max_pages: typed_option::<u32>(matches, "max-pages"),
}
}
#[cfg(test)]
pub(in crate::sns::commands) fn parse<I>(args: I) -> Result<Self, SnsCommandError>
where
I: IntoIterator<Item = OsString>,
{
let matches = parse_sns_matches(sns_proposal_refresh_command(), args)?;
Ok(Self::from_matches(
&matches,
ic_query::subnet_catalog::MAINNET_NETWORK,
))
}
}