summaryrefslogtreecommitdiffstats
path: root/openpgp/src
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-04-20 17:42:46 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-04-20 18:43:42 +0200
commit15723e28cc6e18b0ca7a51212bbcd72fb0e40c72 (patch)
tree12142a08345d1138313b348dc924d9ba531b649b /openpgp/src
parent3c9aa9d2fc1a4c3a15f7efb763ca8b9eeaddf69d (diff)
openpgp: Make armor::Writer's inner writer non-optional.
Diffstat (limited to 'openpgp/src')
-rw-r--r--openpgp/src/armor.rs97
1 files changed, 42 insertions, 55 deletions
diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs
index 6433dae5..34b068bc 100644
--- a/openpgp/src/armor.rs
+++ b/openpgp/src/armor.rs
@@ -140,7 +140,7 @@ impl Kind {
/// A filter that applies ASCII Armor to the data written to it.
pub struct Writer<W: Write> {
- sink: Option<W>,
+ sink: W,
kind: Kind,
stash: Vec<u8>,
column: usize,
@@ -178,7 +178,7 @@ impl<W: Write> Writer<W> {
/// ```
pub fn new(inner: W, kind: Kind, headers: &[(&str, &str)]) -> Result<Self> {
let mut w = Writer {
- sink: Some(inner),
+ sink: inner,
kind,
stash: Vec::<u8>::with_capacity(2),
column: 0,
@@ -202,15 +202,10 @@ impl<W: Write> Writer<W> {
Ok(w)
}
- fn e_finalized() -> Error {
- Error::new(ErrorKind::BrokenPipe, "Writer is finalized.")
- }
-
fn finalize_headers(&mut self) -> Result<()> {
if ! self.dirty {
self.dirty = true;
- self.sink.as_mut().ok_or_else(Self::e_finalized)?
- .write_all(&self.header)?;
+ self.sink.write_all(&self.header)?;
// Release memory.
crate::vec_truncate(&mut self.header, 0);
self.header.shrink_to_fit();
@@ -226,14 +221,10 @@ impl<W: Write> Writer<W> {
pub fn finalize(mut self) -> Result<W> {
if ! self.dirty {
// No data was written to us, don't emit anything.
- return Ok(self.sink.take().ok_or_else(Self::e_finalized)?);
+ return Ok(self.sink);
}
self.finalize_armor()?;
- if let Some(sink) = self.sink.take() {
- Ok(sink)
- } else {
- Err(Self::e_finalized())
- }
+ Ok(self.sink)
}
/// Writes the footer.
@@ -243,55 +234,51 @@ impl<W: Write> Writer<W> {
return Ok(());
}
self.finalize_headers()?;
- if let Some(sink) = self.sink.as_mut() {
- // Write any stashed bytes and pad.
- if self.stash.len() > 0 {
- sink.write_all(base64::encode_config(
- &self.stash, base64::STANDARD).as_bytes())?;
- self.column += 4;
- }
- // Inserts a line break if necessary.
- //
- // Unfortunately, we cannot use
- //self.linebreak()?;
- //
- // Therefore, we inline it here. This is a bit sad.
- assert!(self.column <= LINE_LENGTH);
- if self.column == LINE_LENGTH {
- write!(sink, "{}", LINE_ENDING)?;
- self.column = 0;
- }
+ // Write any stashed bytes and pad.
+ if self.stash.len() > 0 {
+ self.sink.write_all(base64::encode_config(
+ &self.stash, base64::STANDARD).as_bytes())?;
+ self.column += 4;
+ }
- if self.column > 0 {
- write!(sink, "{}", LINE_ENDING)?;
- }
+ // Inserts a line break if necessary.
+ //
+ // Unfortunately, we cannot use
+ //self.linebreak()?;
+ //
+ // Therefore, we inline it here. This is a bit sad.
+ assert!(self.column <= LINE_LENGTH);
+ if self.column == LINE_LENGTH {
+ write!(self.sink, "{}", LINE_ENDING)?;
+ self.column = 0;
+ }
- let crc = self.crc.finalize();
- let bytes: [u8; 3] = [
- (crc >> 16) as u8,
- (crc >> 8) as u8,
- (crc >> 0) as u8,
- ];
+ if self.column > 0 {
+ write!(self.sink, "{}", LINE_ENDING)?;
+ }
- // CRC and footer.
- write!(sink, "={}{}{}{}",
- base64::encode_config(&bytes, base64::STANDARD_NO_PAD),
- LINE_ENDING, self.kind.end(), LINE_ENDING)?;
+ let crc = self.crc.finalize();
+ let bytes: [u8; 3] = [
+ (crc >> 16) as u8,
+ (crc >> 8) as u8,
+ (crc >> 0) as u8,
+ ];
- self.dirty = false;
- Ok(())
- } else {
- Err(Self::e_finalized())
- }
+ // CRC and footer.
+ write!(self.sink, "={}{}{}{}",
+ base64::encode_config(&bytes, base64::STANDARD_NO_PAD),
+ LINE_ENDING, self.kind.end(), LINE_ENDING)?;
+
+ self.dirty = false;
+ Ok(())
}
/// Inserts a line break if necessary.
fn linebreak(&mut self) -> Result<()> {
assert!(self.column <= LINE_LENGTH);
if self.column == LINE_LENGTH {
- write!(self.sink.as_mut().ok_or_else(Self::e_finalized)?,
- "{}", LINE_ENDING)?;
+ write!(self.sink, "{}", LINE_ENDING)?;
self.column = 0;
}
Ok(())
@@ -328,7 +315,7 @@ impl<W: Write> Write for Writer<W> {
// If this fails for some reason, and the caller retries
// the write, we might end up with a stash of size 3.
- self.sink.as_mut().ok_or_else(Self::e_finalized)?
+ self.sink
.write_all(base64::encode_config(
&self.stash, base64::STANDARD_NO_PAD).as_bytes())?;
self.column += 4;
@@ -354,7 +341,7 @@ impl<W: Write> Write for Writer<W> {
let mut enc = encoded.as_bytes();
while enc.len() > 0 {
let n = cmp::min(LINE_LENGTH - self.column, enc.len());
- self.sink.as_mut().ok_or_else(Self::e_finalized)?
+ self.sink
.write_all(&enc[..n])?;
enc = &enc[n..];
self.column += n;
@@ -366,7 +353,7 @@ impl<W: Write> Write for Writer<W> {
}
fn flush(&mut self) -> Result<()> {
- self.sink.as_mut().ok_or_else(Self::e_finalized)?.flush()
+ self.sink.flush()
}
}