Skip to content

Commit

Permalink
bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
alekspickle committed Jan 16, 2024
1 parent f0150c7 commit d0ca28e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "serde_default_utils"
authors = ["Oleks Pickle <oleks.pickle@gmail.com>"]
version = "0.2.0"
version = "0.2.1"
edition = "2021"
categories = ["encoding", "no-std", "no-std::no-alloc"]
description = "A set of simple helper functions to cut corners with serde_default"
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# serde_default_utils

[![v](https://img.shields.io/badge/v-0.2.0-blueviolet)]()
[![v](https://img.shields.io/badge/v-0.2.1-blueviolet)]()

## Overview
This is a simple set of functions to make your life easier while working with defaults in serde.
Expand All @@ -16,10 +16,12 @@ helps to generate another const generic function for any const generic type.
use serde_default_utils::*;
use serde::{Deserialize, Serialize};

const JSON: &str = r#"{"yes_or_no":false,"max":60,"delta":-77,"delimeter":"☀"}"#;
const JSON: &str = r#"{"yes_or_no":false,"max":60,"delta":-77,"delimeter":"☀","motto":"You matter"}"#;
const EMPTY_JSON: &str = r#"{}"#;
const MAX: u32 = 7;

serde_default!(motto, "You matter");

#[derive(Serialize, Deserialize, Default)]
struct Config {
#[serde(default = "default_bool::<true>")]
Expand All @@ -31,21 +33,23 @@ helps to generate another const generic function for any const generic type.
max: u32,
#[serde(default = "default_char::<'☀'>")]
delimeter: char,
#[serde(default = "default_motto")]
motto: &'static str,
}

fn main() {
// existing json fields are not changed
let config: Config = serde_json::from_str(JSON).unwrap();
let s = serde_json::to_string(&config).unwrap();
assert_eq!(r#"{"yes_or_no":false,"delta":-77,"max":60,"delimeter":"☀"}"#, &s);
assert_eq!(r#"{"yes_or_no":false,"delta":-77,"max":60,"delimeter":"☀","motto":"You matter"}"#, &s);
// if the field is not present - it is substituted with defaults
let config: Config = serde_json::from_str(EMPTY_JSON).unwrap();
let s = serde_json::to_string(&config).unwrap();
assert_eq!(r#"{"yes_or_no":true,"delta":-3,"max":7,"delimeter":"☀"}"#, &s);
assert_eq!(r#"{"yes_or_no":true,"delta":-3,"max":7,"delimeter":"☀","motto":"You matter"}"#, &s);
// the default impl is just calling underlying type defaults unless you have a custom impl Default
let config = Config::default();
let s = serde_json::to_string(&config).unwrap();
assert_eq!(r#"{"yes_or_no":false,"delta":0,"max":0,"delimeter":"\u0000"}"#, &s);
assert_eq!(r#"{"yes_or_no":false,"delta":0,"max":0,"delimeter":"\u0000","motto":""}"#, &s);
}

```
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! [![v](https://img.shields.io/badge/v-0.2.0-blueviolet)]()
//! [![v](https://img.shields.io/badge/v-0.2.1-blueviolet)]()
//!
//! # Overview
//! This is a simple set of functions to make your life easier while working with defaults in serde.
Expand Down

0 comments on commit d0ca28e

Please sign in to comment.