-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ Introduce
FromFile
trait and uniform the from_file
functi…
…on calls.
- Loading branch information
1 parent
a2d9411
commit 5f271c4
Showing
8 changed files
with
125 additions
and
71 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{io::Read, path::Path}; | ||
|
||
use async_trait::async_trait; | ||
use pgp::{ | ||
errors::Error as PgpError, | ||
packet::{Packet, PacketParser}, | ||
Deserializable, Signature, SignedPublicKey, SignedSecretKey, | ||
}; | ||
use tokio::{ | ||
fs::File, | ||
io::{AsyncRead, BufReader}, | ||
}; | ||
use tokio_util::io::SyncIoBridge; | ||
|
||
use crate::Result; | ||
|
||
#[async_trait] | ||
pub(crate) trait FromFile: Sized { | ||
fn try_from_reader(reader: impl Read + Send + Unpin) -> Result<Self>; | ||
fn try_from_async_reader(async_reader: impl AsyncRead + Send + Unpin) -> Result<Self> { | ||
Self::try_from_reader(SyncIoBridge::new(async_reader)) | ||
} | ||
async fn try_from_file(path: impl AsRef<Path> + Send) -> Result<Self> { | ||
let file = File::open(path).await?; | ||
Ok(Self::try_from_async_reader(BufReader::new(file))?) | ||
} | ||
} | ||
|
||
macro_rules! impl_from_file { | ||
($type:ty) => { | ||
impl FromFile for $type { | ||
fn try_from_reader(reader: impl Read + Send + Unpin) -> Result<Self> { | ||
Ok(Self::from_bytes(reader)?) | ||
} | ||
} | ||
}; | ||
} | ||
|
||
impl_from_file!(SignedSecretKey); | ||
impl_from_file!(SignedPublicKey); | ||
|
||
impl FromFile for Signature { | ||
fn try_from_reader(reader: impl Read + Send + Unpin) -> Result<Self> { | ||
let signature = PacketParser::new(reader) | ||
.find_map(|packet| { | ||
if let Ok(Packet::Signature(s)) = packet { | ||
Some(s) | ||
} else { | ||
None | ||
} | ||
}) | ||
.ok_or(PgpError::MissingPackets)?; | ||
Ok(signature) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,9 +71,8 @@ mod tests { | |
|
||
use crate::{key_pair::KeyPair, Result}; | ||
|
||
#[tokio::test] | ||
#[ignore = "Manual testing for file generation."] | ||
async fn test() -> Result<()> { | ||
#[test] | ||
fn test() -> Result<()> { | ||
let key_pair = KeyPair::generate("example", "[email protected]", String::new)?; | ||
let (secret_key, public_key) = (key_pair.secret_key(), key_pair.public_key()); | ||
println!("{}", secret_key.to_armored_string(None)?); | ||
|
@@ -82,9 +81,8 @@ mod tests { | |
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
#[ignore = "Manual testing for file parsing."] | ||
async fn extract_key_info() -> Result<()> { | ||
#[test] | ||
fn extract_key_info() -> Result<()> { | ||
let secret_key_str = KeyPair::generate("example", "[email protected]", String::new)? | ||
.secret_key() | ||
.to_armored_string(None)?; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters