1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// saltpig -- the Rustic, ergonomic, and extensible CBOR implementation
// Copyright (C) 2018 Moonbolt{K}
//
// saltpig is free software: you may redistribute and/or modify it under
// version 3.0 or later of the GNU GPL (<LICENSE-GPL> or
// <https://www.gnu.org/licenses/gpl-3.0-standalone.html>), or the BSD 3-Clause
// License (<LICENSE-BSD> or <https://opensource.org/licenses/BSD-3-Clause>), at
// your option.
//
// saltpig is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See either of its licenses for more details.

use ::std::convert::TryFrom;

use ::error::{Error, TypeError};
use ::extensions::Extension;
use ::value::{Major, Value};

/// An extension type for embedded encoded CBOR items (tag value 24; [RFC 7049,
/// section 2.4.4.1][standard]).
///
/// (Note that this is really just a newtype of `Vec<u8>`, and no checking at
/// all is done to see if the wrapped bytes even remotely resemble an encoded
/// CBOR item.)
///
/// [standard]: https://tools.ietf.org/html/rfc7049#section-2.4.4.1
#[derive(Debug, Clone)]
pub struct EncodedCbor(Vec<u8>);

impl Extension for EncodedCbor {
    const TAG: u64 = 24;
}

impl EncodedCbor {
    /// Constructs an [`EncodedCbor`] from the raw bytes it wraps.
    ///
    /// As mentioned in the type-level docs, no checking is done to see if the
    /// bytes are actually valid CBOR data.
    pub fn new(bytes: Vec<u8>) -> EncodedCbor {
        EncodedCbor(bytes)
    }

    /// Attempts to parse an [`EncodedCbor`] value out of a [`Value`] (returning
    /// Err if the value turns out to be something other than an `EncodedCbor`).
    ///
    /// [`EncodedCbor`]: struct.EncodedCbor.html
    /// [`Value`]: ../../value/enum.Value.html
    pub fn extract(value: Value) -> Result<EncodedCbor, TypeError> {
        if let Value::Tagged(tag, box_) = value {
            if tag != Self::TAG {
                Err(TypeError::WrongTag(tag, Self::TAG))?
            }
            if let Value::Bytes(bytes) = *box_ {
                Ok(EncodedCbor(bytes))
            } else {
                Err(TypeError::WrongMajor(box_.major(), Major::Bytes, false))
            }
        } else {
            Err(TypeError::WrongMajor(value.major(), Major::Tagged, false))
        }
    }

    /// Constructs an [`EncodedCbor`] by encoding a [`Value`].
    ///
    /// [`Value`]: ../../value/enum.Value.html
    pub fn encode_value(value: Value) -> EncodedCbor {
        EncodedCbor(value.to_bytes())
    }

    /// Unwraps this [`EncodedCbor`], returning the underlying bytes.
    pub fn into_inner(self) -> Vec<u8> {
        let EncodedCbor(bytes) = self;
        bytes
    }

    /// Renders `self` into a generic CBOR value.
    ///
    /// Returns a [`Value`] representing `self`'s encoded bytes with a tag value
    /// of 24.
    ///
    /// [`Value`]: ../../value/enum.Value.html
    pub fn embed(self) -> Value {
        let EncodedCbor(bytes) = self;
        Value::Tagged(Self::TAG, Box::new(Value::Bytes(bytes)))
    }

    /// Attempts to decode the encoded CBOR item.
    ///
    /// This is a convenience method that simply calls [`Value::from_bytes()`]
    /// on the underlying bytes stored in `self`.
    ///
    /// [`Value::from_bytes()`]: ../../value/enum.Value.html#method.from_bytes
    pub fn decode(&self) -> Result<Value, Error> {
        let EncodedCbor(bytes) = self;
        Value::from_bytes(bytes)
    }
}

/// Renders `self` into a generic CBOR value.
///
/// Equivalent to [`EncodedCbor::embed()`].
///
/// [`EncodedCbor::embed()`]: struct.EncodedCbor.html#method.embed
impl From<EncodedCbor> for Value {
    fn from(encoded_cbor: EncodedCbor) -> Value {
        encoded_cbor.embed()
    }
}

/// Attempts to parse an [`EncodedCbor`] value out of a [`Value`].
///
/// Equivalent to [`EncodedCbor::extract()`].
///
/// [`Value`]: ../../value/enum.Value.html
/// [`EncodedCbor`]: struct.EncodedCbor.html
/// [`EncodedCbor::extract()`]: struct.EncodedCbor.html#method.extract
impl TryFrom<Value> for EncodedCbor {
    type Error = TypeError;

    fn try_from(value: Value) -> Result<EncodedCbor, TypeError> {
        EncodedCbor::extract(value)
    }
}

/// Constructs an [`EncodedCbor`] from the raw bytes it wraps.
///
/// Equivalent to [`EncodedCbor::new()`].
///
/// [`EncodedCbor`]: struct.EncodedCbor.html
/// [`EncodedCbor::new()`]: struct.EncodedCbor.html#method.new
impl From<Vec<u8>> for EncodedCbor {
    fn from(bytes: Vec<u8>) -> EncodedCbor {
        EncodedCbor::new(bytes)
    }
}

/// Unwraps this [`EncodedCbor`], returning the underlying bytes.
///
/// Equivalent to [`EncodedCbor::into_inner()`]
///
/// [`EncodedCbor`]: struct.EncodedCbor.html
/// [`EncodedCbor::into_inner()`]: struct.EncodedCbor.html#method.into_inner
impl From<EncodedCbor> for Vec<u8> {
    fn from(encoded_cbor: EncodedCbor) -> Vec<u8> {
        encoded_cbor.into_inner()
    }
}

/// An extension type for self-describing CBOR items (tag value 55799; [RFC
/// 7049, section 2.4.5][standard]).
///
/// [standard]: https://tools.ietf.org/html/rfc7049#section-2.4.5
#[derive(Debug, Clone)]
pub struct Ident(pub Value);

impl Extension for Ident {
    const TAG: u64 = 55799;
}

impl Ident {
    /// Attempts to parse an [`Ident`] value out of a [`Value`] (returning Err
    /// if the value turns out to be something other than an `Ident`).
    ///
    /// [`Ident`]: struct.Ident.html
    /// [`Value`]: ../../value/enum.Value.html
    pub fn extract(value: Value) -> Result<Ident, TypeError> {
        if let Value::Tagged(tag, box_) = value {
            if tag == Self::TAG {
                Ok(Ident(*box_))
            } else {
                Err(TypeError::WrongTag(tag, Self::TAG))
            }
        } else {
            Err(TypeError::WrongMajor(value.major(), Major::Tagged, false))
        }
    }

    /// Renders `self` into a generic CBOR value.
    ///
    /// Returns a [`Value`] representing `self`'s encoded bytes with a tag value
    /// of 55799.
    ///
    /// [`Value`]: ../../value/enum.Value.html
    pub fn embed(self) -> Value {
        let Ident(value) = self;
        Value::Tagged(Self::TAG, Box::new(value))
    }
}

/// Renders `self` into a generic CBOR value.
///
/// Equivalent to [`Ident::embed()`].
///
/// [`Ident::embed()`]: struct.Ident.html#method.embed
impl From<Ident> for Value {
    fn from(value: Ident) -> Value {
        value.embed()
    }
}

/// Attempts to parse an [`Ident`] value out of a [`Value`].
///
/// Equivalent to [`Ident::extract()`].
///
/// [`Value`]: ../../value/enum.Value.html
/// [`Ident`]: struct.Ident.html
/// [`Ident::extract()`]: struct.Ident.html#method.extract
impl TryFrom<Value> for Ident {
    type Error = TypeError;

    fn try_from(value: Value) -> Result<Ident, TypeError> {
        Self::extract(value)
    }
}