Skip to content

Commit 12d009e

Browse files
[clap-v3-utils] Add functions to directly parse from SignerSource (#1062)
* add `try_get_keypair` and `try_get_keypairs` * add `try_get_signer` and `try_get_signers` * add `try_get_pubkey` and `try_get_pubkeys` * add `try_resolve`
1 parent be80b92 commit 12d009e

File tree

1 file changed

+108
-1
lines changed

1 file changed

+108
-1
lines changed

clap-v3-utils/src/input_parsers/signer.rs

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use {
22
crate::keypair::{
3-
keypair_from_seed_phrase, pubkey_from_path, resolve_signer_from_path, signer_from_path,
3+
keypair_from_seed_phrase, keypair_from_source, pubkey_from_path, pubkey_from_source,
4+
resolve_signer_from_path, resolve_signer_from_source, signer_from_path, signer_from_source,
45
ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG,
56
},
67
clap::{builder::ValueParser, ArgMatches},
@@ -89,6 +90,112 @@ impl SignerSource {
8990
}
9091
}
9192

93+
pub fn try_get_keypair(
94+
matches: &ArgMatches,
95+
name: &str,
96+
) -> Result<Option<Keypair>, Box<dyn error::Error>> {
97+
let source = matches.try_get_one::<Self>(name)?;
98+
if let Some(source) = source {
99+
keypair_from_source(matches, source, name, true).map(Some)
100+
} else {
101+
Ok(None)
102+
}
103+
}
104+
105+
pub fn try_get_keypairs(
106+
matches: &ArgMatches,
107+
name: &str,
108+
) -> Result<Option<Vec<Keypair>>, Box<dyn error::Error>> {
109+
let sources = matches.try_get_many::<Self>(name)?;
110+
if let Some(sources) = sources {
111+
let keypairs = sources
112+
.filter_map(|source| keypair_from_source(matches, source, name, true).ok())
113+
.collect();
114+
Ok(Some(keypairs))
115+
} else {
116+
Ok(None)
117+
}
118+
}
119+
120+
#[allow(clippy::type_complexity)]
121+
pub fn try_get_signer(
122+
matches: &ArgMatches,
123+
name: &str,
124+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
125+
) -> Result<Option<(Box<dyn Signer>, Pubkey)>, Box<dyn error::Error>> {
126+
let source = matches.try_get_one::<Self>(name)?;
127+
if let Some(source) = source {
128+
let signer = signer_from_source(matches, source, name, wallet_manager)?;
129+
let signer_pubkey = signer.pubkey();
130+
Ok(Some((signer, signer_pubkey)))
131+
} else {
132+
Ok(None)
133+
}
134+
}
135+
136+
#[allow(clippy::type_complexity)]
137+
pub fn try_get_signers(
138+
matches: &ArgMatches,
139+
name: &str,
140+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
141+
) -> Result<Option<Vec<(Box<dyn Signer>, Pubkey)>>, Box<dyn error::Error>> {
142+
let sources = matches.try_get_many::<Self>(name)?;
143+
if let Some(sources) = sources {
144+
let signers = sources
145+
.filter_map(|source| {
146+
let signer = signer_from_source(matches, source, name, wallet_manager).ok()?;
147+
let signer_pubkey = signer.pubkey();
148+
Some((signer, signer_pubkey))
149+
})
150+
.collect();
151+
Ok(Some(signers))
152+
} else {
153+
Ok(None)
154+
}
155+
}
156+
157+
pub fn try_get_pubkey(
158+
matches: &ArgMatches,
159+
name: &str,
160+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
161+
) -> Result<Option<Pubkey>, Box<dyn error::Error>> {
162+
let source = matches.try_get_one::<Self>(name)?;
163+
if let Some(source) = source {
164+
pubkey_from_source(matches, source, name, wallet_manager).map(Some)
165+
} else {
166+
Ok(None)
167+
}
168+
}
169+
170+
pub fn try_get_pubkeys(
171+
matches: &ArgMatches,
172+
name: &str,
173+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
174+
) -> Result<Option<Vec<Pubkey>>, Box<dyn std::error::Error>> {
175+
let sources = matches.try_get_many::<Self>(name)?;
176+
if let Some(sources) = sources {
177+
let pubkeys = sources
178+
.filter_map(|source| pubkey_from_source(matches, source, name, wallet_manager).ok())
179+
.collect();
180+
Ok(Some(pubkeys))
181+
} else {
182+
Ok(None)
183+
}
184+
}
185+
186+
pub fn try_resolve(
187+
matches: &ArgMatches,
188+
name: &str,
189+
wallet_manager: &mut Option<Rc<RemoteWalletManager>>,
190+
) -> Result<Option<String>, Box<dyn std::error::Error>> {
191+
let source = matches.try_get_one::<Self>(name)?;
192+
if let Some(source) = source {
193+
resolve_signer_from_source(matches, source, name, wallet_manager)
194+
} else {
195+
Ok(None)
196+
}
197+
}
198+
92199
pub fn parse<S: AsRef<str>>(source: S) -> Result<Self, SignerSourceError> {
93200
let source = source.as_ref();
94201
let source = {

0 commit comments

Comments
 (0)