cli: add did function for args parsing

Adds a standard for parsing the Did type for the CLI commands.

It attempts to parse the Did type. If it fails then it checks if the
operator passed a PublicKey and suggests using `did:key` instead.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny 2023-02-16 12:38:37 +00:00 committed by Alexis Sellier
parent efe5ff6ddd
commit b57a4606f6
No known key found for this signature in database
1 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,8 @@ use std::ffi::OsString;
use std::str::FromStr;
use anyhow::anyhow;
use radicle::crypto;
use radicle::prelude::Did;
#[derive(thiserror::Error, Debug)]
pub enum Error {
@ -71,3 +73,15 @@ pub fn finish(unparsed: Vec<OsString>) -> anyhow::Result<()> {
}
Ok(())
}
pub fn did(val: OsString) -> anyhow::Result<Did> {
let val = val.to_string_lossy();
let Ok(peer) = Did::from_str(&val) else {
if crypto::PublicKey::from_str(&val).is_ok() {
return Err(anyhow!("expected DID, did you mean 'did:key:{val}'?"));
} else {
return Err(anyhow!("invalid DID '{}', expected 'did:key'", val));
}
};
Ok(peer)
}