| 1 | ## RON extensions |
| 2 | |
| 3 | RON has extensions that can be enabled by adding the following attribute at the top of your RON document: |
| 4 | |
| 5 | `#![enable(...)]` |
| 6 | |
| 7 | # unwrap_newtypes |
| 8 | |
| 9 | You can add this extension by adding the following attribute at the top of your RON document: |
| 10 | |
| 11 | `#![enable(unwrap_newtypes)]` |
| 12 | |
| 13 | This feature enables RON to automatically unwrap simple tuples. |
| 14 | |
| 15 | ```rust |
| 16 | struct NewType(u32); |
| 17 | struct Object { |
| 18 | pub new_type: NewType, |
| 19 | } |
| 20 | ``` |
| 21 | |
| 22 | Without `unwrap_newtypes`, because the value `5` can not be saved into `NewType(u32)`, your RON document would look like this: |
| 23 | |
| 24 | ```ron |
| 25 | ( |
| 26 | new_type: (5), |
| 27 | ) |
| 28 | ``` |
| 29 | |
| 30 | With the `unwrap_newtypes` extension, this coercion is done automatically. So `5` will be interpreted as `(5)`. |
| 31 | |
| 32 | ```ron |
| 33 | #![enable(unwrap_newtypes)] |
| 34 | ( |
| 35 | new_type: 5, |
| 36 | ) |
| 37 | ``` |
| 38 | |
| 39 | # implicit_some |
| 40 | |
| 41 | You can add this extension by adding the following attribute at the top of your RON document: |
| 42 | |
| 43 | `#![enable(implicit_some)]` |
| 44 | |
| 45 | This feature enables RON to automatically convert any value to `Some(value)` if the deserialized type requires it. |
| 46 | |
| 47 | ```rust |
| 48 | struct Object { |
| 49 | pub value: Option<u32>, |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | Without this feature, you would have to write this RON document. |
| 54 | |
| 55 | ```ron |
| 56 | ( |
| 57 | value: Some(5), |
| 58 | ) |
| 59 | ``` |
| 60 | |
| 61 | Enabling the feature would automatically infer `Some(x)` if `x` is given. In this case, RON automatically casts this `5` into a `Some(5)`. |
| 62 | |
| 63 | ```ron |
| 64 | ( |
| 65 | value: 5, |
| 66 | ) |
| 67 | ``` |
| 68 | |
| 69 | With this extension enabled, explicitly given `None` and `Some(..)` will be matched eagerly on `Option<Option<Option<u32>>>`, i.e. |
| 70 | * `5` -> `Some(Some(Some(5)))` |
| 71 | * `None` -> `None` |
| 72 | * `Some(5)` -> `Some(Some(Some(5)))` |
| 73 | * `Some(None)` -> `Some(None)` |
| 74 | * `Some(Some(5))` -> `Some(Some(Some(5)))` |
| 75 | * `Some(Some(None))` -> `Some(Some(None))` |
| 76 | * `Some(Some(Some(5)))` -> `Some(Some(Some(5)))` |
| 77 | |
| 78 | # unwrap_variant_newtypes |
| 79 | |
| 80 | You can add this extension by adding the following attribute at the top of your RON document: |
| 81 | |
| 82 | `#![enable(unwrap_variant_newtypes)]` |
| 83 | |
| 84 | This feature enables RON to automatically unwrap newtype enum variants. |
| 85 | |
| 86 | ```rust |
| 87 | #[derive(Deserialize)] |
| 88 | struct Inner { |
| 89 | pub a: u8, |
| 90 | pub b: bool, |
| 91 | } |
| 92 | #[derive(Deserialize)] |
| 93 | pub enum Enum { |
| 94 | A(Inner), |
| 95 | B, |
| 96 | } |
| 97 | ``` |
| 98 | |
| 99 | Without `unwrap_variant_newtypes`, your RON document would look like this: |
| 100 | |
| 101 | ```ron |
| 102 | ( |
| 103 | variant: A(Inner(a: 4, b: true)), |
| 104 | ) |
| 105 | ``` |
| 106 | |
| 107 | With the `unwrap_variant_newtypes` extension, the first structural layer inside a newtype variant will be unwrapped automatically: |
| 108 | |
| 109 | ```ron |
| 110 | #![enable(unwrap_newtypes)] |
| 111 | ( |
| 112 | variant: A(a: 4, b: true), |
| 113 | ) |
| 114 | ``` |
| 115 | |
| 116 | Note that when the `unwrap_variant_newtypes` extension is enabled, the first layer inside a newtype variant will **always** be unwrapped, i.e. it is no longer possible to write `A(Inner(a: 4, b: true))` or `A((a: 4, b: true))`. |
| 117 | |
| 118 | # explicit_struct_names |
| 119 | During serialization, this extension emits struct names. For instance, this would be emitted: |
| 120 | ```ron |
| 121 | Foo( |
| 122 | bar: Bar(42), |
| 123 | ) |
| 124 | ``` |
| 125 | |
| 126 | During deserialization, this extension requires that all structs have names attached to them. For example, the following deserializes perfectly fine: |
| 127 | ```ron |
| 128 | Foo( |
| 129 | bar: Bar(42), |
| 130 | ) |
| 131 | ``` |
| 132 | |
| 133 | However, with the `explicit_struct_names` extension enabled, the following will throw an `ExpectedStructName` error: |
| 134 | ```ron |
| 135 | ( |
| 136 | bar: Bar(42), |
| 137 | ) |
| 138 | ``` |
| 139 | |
| 140 | Similarly, the following will throw the same error: |
| 141 | ```ron |
| 142 | Foo( |
| 143 | bar: (42), |
| 144 | ) |
| 145 | ``` |
| 146 | |
| 147 | Note that if what you are parsing is spread across many files, you would likely use `Options::with_default_extension` to enable `Extensions::EXPLICIT_STRUCT_NAMES` before the parsing stage. This is because prepending `#![enable(explicit_struct_names)]` to the contents of every file you parse would violate DRY (Don't Repeat Yourself). |
| 148 | |
| 149 | Here is an example of how to enable `explicit_struct_names` using this method: |
| 150 | ```rust |
| 151 | use ron::extensions::Extensions; |
| 152 | use ron::options::Options; |
| 153 | |
| 154 | // Setup the options |
| 155 | let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES); |
| 156 | // Retrieve the contents of the file |
| 157 | let file_contents: &str = /* ... */; |
| 158 | // Parse the file's contents |
| 159 | let foo: Foo = options.from_str(file_contents)?; |
| 160 | ``` |