|
1 | 1 | //! The BER mode.
|
2 | 2 | //!
|
3 |
| -//! This is a private module. It’s public items are re-exported by the parent. |
| 3 | +//! |
4 | 4 |
|
5 |
| -use crate::decode; |
6 |
| -use crate::decode::DecodeError; |
7 | 5 |
|
| 6 | +/// Basic Encoding Rules. |
| 7 | +/// |
| 8 | +/// These are the most flexible rules, allowing alternative encodings for |
| 9 | +/// some types as well as indefinite length values. |
| 10 | +// |
| 11 | +// XXX We derive all the things for now so we can derive them on types that |
| 12 | +// are generic over the mode but will replace the derives later. |
| 13 | +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 14 | +pub struct Ber; |
8 | 15 |
|
9 |
| -//------------ Mode ---------------------------------------------------------- |
10 |
| - |
11 |
| -/// The BER Mode. |
| 16 | +/// Canonical Encoding Rules. |
12 | 17 | ///
|
13 |
| -/// X.680 defines not one but three sets of related encoding rules. All three |
14 |
| -/// follow the same basic ideas but implement them in slightly different |
15 |
| -/// ways. |
| 18 | +/// These rules always employ indefinite length encoding for constructed |
| 19 | +/// values and the shortest possible form for primitive values. There |
| 20 | +/// are additional restrictions for certain types. |
| 21 | +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 22 | +pub struct Cer; |
| 23 | + |
| 24 | +/// Distinguished Encoding Rules. |
16 | 25 | ///
|
17 |
| -/// This type represents these rules. The [`decode`] method provides a way to |
18 |
| -/// decode a source using the specific decoding mode. You can also change |
19 |
| -/// the decoding mode later on through the `set_mode` methods of [`Primitive`] |
20 |
| -/// and [`Constructed`]. |
| 26 | +/// These rules always employ definite length values and require the |
| 27 | +/// shortest possible encoding. Additional rules apply to some types. |
| 28 | +#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
| 29 | +pub struct Der; |
| 30 | + |
| 31 | +/// One of the restricted rules CER or DER. |
21 | 32 | ///
|
22 |
| -/// [`decode´]: #method.decode |
23 |
| -/// [`Primitive`]: decode/struct.Primitive.html |
24 |
| -/// [`Constructed`]: decode/struct.Constructed.html |
25 |
| -#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
26 |
| -pub enum Mode { |
27 |
| - /// Basic Encoding Rules. |
28 |
| - /// |
29 |
| - /// These are the most flexible rules, allowing alternative encodings for |
30 |
| - /// some types as well as indefinite length values. |
31 |
| - #[default] |
32 |
| - Ber, |
| 33 | +/// Some restrictions to the basic rules are shared between CER and DER. |
| 34 | +/// This trait allows implementations to do both. |
| 35 | +pub trait Restricted { } |
33 | 36 |
|
34 |
| - /// Canonical Encoding Rules. |
35 |
| - /// |
36 |
| - /// These rules always employ indefinite length encoding for constructed |
37 |
| - /// values and the shortest possible form for primitive values. There |
38 |
| - /// are additional restrictions for certain types. |
39 |
| - Cer, |
| 37 | +impl Restricted for Cer { } |
| 38 | +impl Restricted for Der { } |
40 | 39 |
|
41 |
| - /// Distinguished Encoding Rules. |
42 |
| - /// |
43 |
| - /// These rules always employ definite length values and require the |
44 |
| - /// shortest possible encoding. Additional rules apply to some types. |
45 |
| - Der, |
46 |
| -} |
| 40 | +/// One of the modes. |
| 41 | +pub trait Mode { |
| 42 | + /// Is this mode CER or DER? |
| 43 | + const IS_RESTRICTED: bool; |
47 | 44 |
|
48 |
| -impl Mode { |
49 |
| - /// Decode a source using a specific mode. |
50 |
| - /// |
51 |
| - /// The method will attempt to decode `source` using the rules represented |
52 |
| - /// by this value. The closure `op` will be given the content of the |
53 |
| - /// source as a sequence of values. The closure does not need to process |
54 |
| - /// all values in the source. |
55 |
| - pub fn decode<S, F, T>( |
56 |
| - self, source: S, op: F, |
57 |
| - ) -> Result<T, DecodeError<<S::Source as decode::Source>::Error>> |
58 |
| - where |
59 |
| - S: decode::IntoSource, |
60 |
| - F: FnOnce( |
61 |
| - &mut decode::Constructed<S::Source> |
62 |
| - ) -> Result<T, DecodeError<<S::Source as decode::Source>::Error>>, |
63 |
| - { |
64 |
| - decode::Constructed::decode(source, self, op) |
65 |
| - } |
| 45 | + /// Does this mode allow definite-length constructed values? |
| 46 | + const ALLOW_DEFINITE_CONSTRUCTED: bool; |
66 | 47 |
|
67 |
| - /// Returns whether the mode is `Mode::Ber`. |
68 |
| - pub fn is_ber(self) -> bool { |
69 |
| - matches!(self, Mode::Ber) |
70 |
| - } |
| 48 | + /// Does this mode allow indefinite length constructed values? |
| 49 | + const ALLOW_INDEFINITE_CONSTRUCTED: bool; |
| 50 | +} |
| 51 | + |
| 52 | +impl Mode for Ber { |
| 53 | + const IS_RESTRICTED: bool = false; |
| 54 | + const ALLOW_DEFINITE_CONSTRUCTED: bool = true; |
| 55 | + const ALLOW_INDEFINITE_CONSTRUCTED: bool = true; |
| 56 | +} |
71 | 57 |
|
72 |
| - /// Returns whether the mode is `Mode::Cer`. |
73 |
| - pub fn is_cer(self) -> bool { |
74 |
| - matches!(self, Mode::Cer) |
75 |
| - } |
| 58 | +impl Mode for Cer { |
| 59 | + const IS_RESTRICTED: bool = true; |
| 60 | + const ALLOW_DEFINITE_CONSTRUCTED: bool = false; |
| 61 | + const ALLOW_INDEFINITE_CONSTRUCTED: bool = true; |
| 62 | +} |
76 | 63 |
|
77 |
| - /// Returns whether the mode is `Mode::Der`. |
78 |
| - pub fn is_der(self) -> bool { |
79 |
| - matches!(self, Mode::Der) |
80 |
| - } |
| 64 | +impl Mode for Der { |
| 65 | + const IS_RESTRICTED: bool = true; |
| 66 | + const ALLOW_DEFINITE_CONSTRUCTED: bool = true; |
| 67 | + const ALLOW_INDEFINITE_CONSTRUCTED: bool = false; |
81 | 68 | }
|
82 | 69 |
|
0 commit comments