-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
project: upgrade serde_yaml to 0.9, introduce singleton_yaml module
Originally in 0.8, the yaml module serialized enum values the same way as it's done in JSON: to a dict with a single field. 0.9 changed the backend, and also introduced the tagged serialization of enums. Unfortunately this is not compatible with the original serialization, and also breaks the json<->yaml bijection. There's also a limtiation in serde regarding tagged and untagged enums: dtolnay/serde-yaml#318 So we are staying on the singleton serialization, and I made a small wrapper module for convenience.
- Loading branch information
1 parent
f36f038
commit dddd67f
Showing
12 changed files
with
212 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (C) 2022, Alex Badics | ||
// | ||
// This file is part of Hun-Law. | ||
// | ||
// Hun-law is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, version 3 of the License. | ||
// | ||
// Hun-law 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 the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Hun-law. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use serde::{de::DeserializeOwned, Deserialize, Serialize}; | ||
use serde_yaml::{with::singleton_map_recursive, Deserializer, Result, Serializer}; | ||
|
||
#[inline] | ||
pub fn to_writer<W, T>(writer: W, value: &T) -> Result<()> | ||
where | ||
W: std::io::Write, | ||
T: ?Sized + Serialize, | ||
{ | ||
let mut ser = Serializer::new(writer); | ||
singleton_map_recursive::serialize(&value, &mut ser) | ||
} | ||
|
||
#[inline] | ||
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>> | ||
where | ||
T: ?Sized + Serialize, | ||
{ | ||
let mut writer = Vec::with_capacity(128); | ||
to_writer(&mut writer, value)?; | ||
Ok(writer) | ||
} | ||
|
||
#[inline] | ||
pub fn to_string<T>(value: &T) -> Result<String> | ||
where | ||
T: ?Sized + Serialize, | ||
{ | ||
let vec = to_vec(value)?; | ||
let string = unsafe { | ||
// We do not emit invalid UTF-8. | ||
String::from_utf8_unchecked(vec) | ||
}; | ||
Ok(string) | ||
} | ||
|
||
pub fn from_str<'de, T>(s: &'de str) -> Result<T> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
singleton_map_recursive::deserialize(Deserializer::from_str(s)) | ||
} | ||
|
||
/// Deserialize an instance of type `T` from an IO stream of YAML. | ||
/// | ||
/// This conversion can fail if the structure of the Value does not match the | ||
/// structure expected by `T`, for example if `T` is a struct type but the Value | ||
/// contains something other than a YAML map. It can also fail if the structure | ||
/// is correct but `T`'s implementation of `Deserialize` decides that something | ||
/// is wrong with the data, for example required struct fields are missing from | ||
/// the YAML map or some number is too big to fit in the expected primitive | ||
/// type. | ||
pub fn from_reader<R, T>(rdr: R) -> Result<T> | ||
where | ||
R: std::io::Read, | ||
T: DeserializeOwned, | ||
{ | ||
singleton_map_recursive::deserialize(Deserializer::from_reader(rdr)) | ||
} | ||
|
||
/// Deserialize an instance of type `T` from bytes of YAML text. | ||
/// | ||
/// This conversion can fail if the structure of the Value does not match the | ||
/// structure expected by `T`, for example if `T` is a struct type but the Value | ||
/// contains something other than a YAML map. It can also fail if the structure | ||
/// is correct but `T`'s implementation of `Deserialize` decides that something | ||
/// is wrong with the data, for example required struct fields are missing from | ||
/// the YAML map or some number is too big to fit in the expected primitive | ||
/// type. | ||
pub fn from_slice<'de, T>(v: &'de [u8]) -> Result<T> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
singleton_map_recursive::deserialize(Deserializer::from_slice(v)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.