Skip to content

Commit 5a4862f

Browse files
committed
Factor out extensions and use in PrettyConfig to configure implicit_some
1 parent 3103df3 commit 5a4862f

File tree

6 files changed

+45
-34
lines changed

6 files changed

+45
-34
lines changed

src/de/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ use std::{borrow::Cow, io, str};
77

88
use self::id::IdDeserializer;
99
use self::tag::TagDeserializer;
10-
use crate::parse::{AnyNum, Bytes, Extensions, ParsedStr};
10+
use crate::extensions::Extensions;
11+
use crate::parse::{AnyNum, Bytes, ParsedStr};
1112

1213
mod error;
1314
mod id;

src/extensions.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use bitflags::bitflags;
2+
use serde::{Deserialize, Serialize};
3+
4+
bitflags! {
5+
#[derive(Serialize, Deserialize)]
6+
pub struct Extensions: usize {
7+
const UNWRAP_NEWTYPES = 0x1;
8+
const IMPLICIT_SOME = 0x2;
9+
}
10+
}
11+
12+
impl Extensions {
13+
/// Creates an extension flag from an ident.
14+
pub fn from_ident(ident: &[u8]) -> Option<Extensions> {
15+
match ident {
16+
b"unwrap_newtypes" => Some(Extensions::UNWRAP_NEWTYPES),
17+
b"implicit_some" => Some(Extensions::IMPLICIT_SOME),
18+
_ => None,
19+
}
20+
}
21+
}

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Serializing / Deserializing is as simple as calling `to_string` / `from_str`.
6060
pub mod de;
6161
pub mod ser;
6262
pub mod value;
63+
pub mod extensions;
6364
pub use crate::value::Value;
6465

6566
mod parse;

src/parse.rs

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use bitflags::bitflags;
21
use std::{
32
char::from_u32 as char_from_u32,
43
fmt::{Display, Formatter, Result as FmtResult},
@@ -7,6 +6,7 @@ use std::{
76
};
87

98
use crate::de::{Error, ParseError, Result};
9+
use crate::extensions::Extensions;
1010

1111
const DIGITS: &[u8] = b"0123456789ABCDEFabcdef_";
1212
const FLOAT_CHARS: &[u8] = b"0123456789.+-eE";
@@ -773,23 +773,6 @@ impl<'a> Bytes<'a> {
773773
}
774774
}
775775

776-
bitflags! {
777-
pub struct Extensions: usize {
778-
const UNWRAP_NEWTYPES = 0x1;
779-
const IMPLICIT_SOME = 0x2;
780-
}
781-
}
782-
783-
impl Extensions {
784-
/// Creates an extension flag from an ident.
785-
pub fn from_ident(ident: &[u8]) -> Option<Extensions> {
786-
match ident {
787-
b"unwrap_newtypes" => Some(Extensions::UNWRAP_NEWTYPES),
788-
b"implicit_some" => Some(Extensions::IMPLICIT_SOME),
789-
_ => None,
790-
}
791-
}
792-
}
793776

794777
pub trait Num {
795778
fn from_u8(x: u8) -> Self;

src/ser/mod.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{
44
fmt::{Display, Formatter, Result as FmtResult, Write},
55
};
66

7+
use crate::extensions::Extensions;
8+
79
mod value;
810

911
/// Serializes `value` and returns it as string.
@@ -96,8 +98,8 @@ pub struct PrettyConfig {
9698
/// Enumerate array items in comments
9799
#[serde(default = "default_enumerate_arrays")]
98100
pub enumerate_arrays: bool,
99-
/// Enable implicit_some extension
100-
pub implicit_some: bool,
101+
/// Enable extensions. Only configures 'implicit_some' for now.
102+
pub extensions: Extensions,
101103
/// Private field to ensure adding a field is non-breaking.
102104
#[serde(skip)]
103105
_future_proof: (),
@@ -159,11 +161,11 @@ impl PrettyConfig {
159161
self
160162
}
161163

162-
/// Configures whether the implicit_some extension will be enabled
164+
/// Configures extensions
163165
///
164-
/// Default: `false`
165-
pub fn with_implicit_some(mut self, implicit_some: bool) -> Self {
166-
self.implicit_some = implicit_some;
166+
/// Default: Extensions::empty()
167+
pub fn with_extensions(mut self, extensions: Extensions) -> Self {
168+
self.extensions = extensions;
167169

168170
self
169171
}
@@ -194,8 +196,8 @@ fn default_enumerate_arrays() -> bool {
194196
false
195197
}
196198

197-
fn default_implicit_some() -> bool {
198-
false
199+
fn default_extensions() -> Extensions {
200+
Extensions::empty()
199201
}
200202

201203
impl Default for PrettyConfig {
@@ -206,7 +208,7 @@ impl Default for PrettyConfig {
206208
indentor: default_indentor(),
207209
separate_tuple_members: default_separate_tuple_members(),
208210
enumerate_arrays: default_enumerate_arrays(),
209-
implicit_some: default_implicit_some(),
211+
extensions: default_extensions(),
210212
_future_proof: (),
211213
}
212214
}
@@ -229,7 +231,7 @@ impl Serializer {
229231
/// Most of the time you can just use `to_string` or `to_string_pretty`.
230232
pub fn new(config: Option<PrettyConfig>, struct_names: bool) -> Self {
231233
let initial_output = if let Some(conf) = &config {
232-
if conf.implicit_some {
234+
if conf.extensions.contains(Extensions::IMPLICIT_SOME) {
233235
"#![enable(implicit_some)]".to_string() + &conf.new_line
234236
} else {
235237
String::new()
@@ -271,10 +273,10 @@ impl Serializer {
271273
.map_or(false, |&(ref config, _)| config.separate_tuple_members)
272274
}
273275

274-
fn implicit_some(&self) -> bool {
276+
fn extensions(&self) -> Extensions {
275277
self.pretty
276278
.as_ref()
277-
.map_or(false, |&(ref config, _)| config.implicit_some)
279+
.map_or(Extensions::empty(), |&(ref config, _)| config.extensions)
278280
}
279281

280282
fn start_indent(&mut self) {
@@ -414,11 +416,12 @@ impl<'a> ser::Serializer for &'a mut Serializer {
414416
where
415417
T: ?Sized + Serialize,
416418
{
417-
if !self.implicit_some() {
419+
let implicit_some = self.extensions().contains(Extensions::IMPLICIT_SOME);
420+
if !implicit_some {
418421
self.output += "Some(";
419422
}
420423
value.serialize(&mut *self)?;
421-
if !self.implicit_some() {
424+
if !implicit_some {
422425
self.output += ")";
423426
}
424427

tests/roundtrip.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use serde::{Deserialize, Serialize};
22
use std::collections::HashMap;
33

4+
use ron::extensions::Extensions;
5+
46
#[derive(Debug, PartialEq, Deserialize, Serialize)]
57
struct UnitStruct;
68

@@ -68,7 +70,7 @@ fn roundtrip_pretty() {
6870

6971
let pretty = ron::ser::PrettyConfig::new()
7072
.with_enumerate_arrays(true)
71-
.with_implicit_some(true);
73+
.with_extensions(Extensions::IMPLICIT_SOME);
7274
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();
7375

7476
println!("Serialized: {}", serial);

0 commit comments

Comments
 (0)