use crate::error::{Error, Result};
use async_trait::async_trait;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use std::str::FromStr;
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct ProofRequestResponse {
pub state: String,
pub presentation_request: Value,
pub updated_at: String,
pub connection_id: String,
pub thread_id: String,
pub presentation_request_dict: Value,
pub role: String,
pub auto_present: bool,
pub presentation_exchange_id: String,
pub created_at: String,
pub initiator: String,
}
#[derive(Debug, Clone)]
pub struct Predicate(pub String, pub String, pub String);
impl FromStr for Predicate {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let mut res = s.split(',');
let name = res
.next()
.ok_or_else(|| Error::UnableToParseOutValue(s.to_owned()))?;
let operator = res
.next()
.ok_or_else(|| Error::UnableToParseOutValue(s.to_owned()))?;
let value = res
.next()
.ok_or_else(|| Error::UnableToParseOutValue(s.to_owned()))?;
validate_operator(operator).map_err(|_| Error::InvalidOperator(operator.to_owned()))?;
Ok(Self(name.to_owned(), operator.to_owned(), value.to_owned()))
}
}
fn validate_operator(op: &str) -> Result<()> {
if vec![">=", "<=", "=", ">", "<"]
.iter()
.map(|o| String::from(*o))
.any(|o| o == *op)
{
return Ok(());
};
Err(Error::InvalidOperator(op.to_owned()).into())
}
pub struct ProofRequestOptions {
pub connection_id: String,
pub name: String,
pub attributes: Vec<String>,
pub predicates: Vec<(String, String, i32)>,
}
#[async_trait]
pub trait ProofModule {
async fn send_request(&self, options: ProofRequestOptions) -> Result<ProofRequestResponse>;
}