use log::debug;
use reqsign_core::{Context, ProvideCredential, Result};
use crate::credential::Credential;
use super::parse::parse_credential_bytes;
#[derive(Debug, Clone)]
pub struct FileCredentialProvider {
path: String,
scope: Option<String>,
}
impl FileCredentialProvider {
pub fn new(path: impl Into<String>) -> Self {
Self {
path: path.into(),
scope: None,
}
}
pub fn with_scope(mut self, scope: impl Into<String>) -> Self {
self.scope = Some(scope.into());
self
}
}
impl ProvideCredential for FileCredentialProvider {
type Credential = Credential;
async fn provide_credential(&self, ctx: &Context) -> Result<Option<Self::Credential>> {
debug!("loading credential from file path: {}", self.path);
let content = ctx.file_read(&self.path).await?;
parse_credential_bytes(ctx, &content, self.scope.clone()).await
}
}