summaryrefslogtreecommitdiffstats
path: root/openpgp/src
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2018-10-26 15:29:33 +0200
committerJustus Winter <justus@sequoia-pgp.org>2018-10-26 15:30:51 +0200
commit5355c4cb5664f3aa3b027544e058db85232456f2 (patch)
treeaf01869a1be7b995621e58a516adefd9f214c719 /openpgp/src
parent6cac049b99c98e001404b57ef4049773618c73ed (diff)
openpgp: Add constructors to TSK, implement Deref{,Mut} for TSK.
Diffstat (limited to 'openpgp/src')
-rw-r--r--openpgp/src/tsk.rs43
1 files changed, 40 insertions, 3 deletions
diff --git a/openpgp/src/tsk.rs b/openpgp/src/tsk.rs
index 33098fcf..820b35cb 100644
--- a/openpgp/src/tsk.rs
+++ b/openpgp/src/tsk.rs
@@ -1,3 +1,8 @@
+use std::borrow::Cow;
+use std::io;
+use std::ops::{Deref, DerefMut};
+use std::path::Path;
+
use {
Result,
TPK,
@@ -7,9 +12,7 @@ use serialize::{
Serialize,
SerializeKey,
};
-
-use std::io;
-use std::borrow::Cow;
+use parse::PacketParserResult;
/// A transferable secret key (TSK).
///
@@ -22,7 +25,41 @@ pub struct TSK {
key: TPK,
}
+impl Deref for TSK {
+ type Target = TPK;
+
+ fn deref(&self) -> &Self::Target {
+ &self.key
+ }
+}
+
+impl DerefMut for TSK {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.key
+ }
+}
+
impl TSK {
+ /// Initializes a `TSK` from a `PacketParser`.
+ pub fn from_packet_parser<'a>(ppr: PacketParserResult<'a>) -> Result<Self> {
+ TPK::from_packet_parser(ppr).map(|tpk| Self::from_tpk(tpk))
+ }
+
+ /// Initializes a `TSK` from a `Read`er.
+ pub fn from_reader<'a, R: 'a + io::Read>(reader: R) -> Result<Self> {
+ TPK::from_reader(reader).map(|tpk| Self::from_tpk(tpk))
+ }
+
+ /// Initializes a `TSK` from a `File`.
+ pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
+ TPK::from_file(path).map(|tpk| Self::from_tpk(tpk))
+ }
+
+ /// Initializes a `TSK` from a byte string.
+ pub fn from_bytes<'a>(data: &'a [u8]) -> Result<Self> {
+ TPK::from_bytes(data).map(|tpk| Self::from_tpk(tpk))
+ }
+
pub(crate) fn from_tpk(tpk: TPK) -> TSK {
TSK{ key: tpk }
}