1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use crate::{Envelope, EnvelopeError};

use super::envelope::EnvelopeCase;

/// Support for wrapping and unwrapping envelopes.
impl Envelope {
    /// Return a new envelope which wraps the current envelope.
    pub fn wrap_envelope(&self) -> Self {
        Self::new_wrapped(self.clone())
    }

    /// Unwraps and returns the inner envelope.
    ///
    /// Returns an error if this is not a wrapped envelope.
    pub fn unwrap_envelope(&self) -> Result<Self, EnvelopeError> {
        match self.subject().case() {
            EnvelopeCase::Wrapped { envelope, .. } => Ok(envelope.clone()),
            _ => Err(EnvelopeError::NotWrapped),
        }
    }
}