-
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.
Machinery to allow distinguishing from missing field and null field
Fixes #50
- Loading branch information
1 parent
a60b31a
commit 373edf2
Showing
15 changed files
with
299 additions
and
101 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,7 @@ segments: | |
### Valid values | ||
Any valid path. | ||
Any valid non-empty path. | ||
## `kind` | ||
|
||
|
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,73 @@ | ||
/* SPDX-FileCopyrightText: © 2024 decompals */ | ||
/* SPDX-License-Identifier: MIT */ | ||
|
||
use serde::{Deserialize, Deserializer}; | ||
|
||
use crate::SlinkyError; | ||
|
||
// https://stackoverflow.com/a/44332837/6292472 | ||
|
||
#[derive(Debug, PartialEq, Default)] | ||
pub(crate) enum AbsentNullable<T> { | ||
#[default] | ||
Absent, | ||
Null, | ||
Value(T), | ||
} | ||
|
||
impl<T> From<Option<T>> for AbsentNullable<T> { | ||
fn from(opt: Option<T>) -> AbsentNullable<T> { | ||
match opt { | ||
Some(v) => AbsentNullable::Value(v), | ||
None => AbsentNullable::Null, | ||
} | ||
} | ||
} | ||
|
||
impl<'de, T> Deserialize<'de> for AbsentNullable<T> | ||
where | ||
T: Deserialize<'de>, | ||
{ | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
Option::deserialize(deserializer).map(Into::into) | ||
} | ||
} | ||
|
||
impl<T> AbsentNullable<T> { | ||
pub fn get_non_null<F>(self, name: &str, default: F) -> Result<T, SlinkyError> | ||
where | ||
F: FnOnce() -> T, | ||
{ | ||
match self { | ||
AbsentNullable::Absent => Ok(default()), | ||
AbsentNullable::Null => Err(SlinkyError::NullValueOnNonNull { | ||
name: name.to_string(), | ||
}), | ||
AbsentNullable::Value(v) => Ok(v), | ||
} | ||
} | ||
|
||
pub fn get_non_null_no_default(self, name: &str) -> Result<Option<T>, SlinkyError> { | ||
match self { | ||
AbsentNullable::Absent => Ok(None), | ||
AbsentNullable::Null => Err(SlinkyError::NullValueOnNonNull { | ||
name: name.to_string(), | ||
}), | ||
AbsentNullable::Value(v) => Ok(Some(v)), | ||
} | ||
} | ||
|
||
pub fn get_optional_nullable<F>(self, _name: &str, default: F) -> Result<Option<T>, SlinkyError> | ||
where | ||
F: FnOnce() -> Option<T>, | ||
{ | ||
match self { | ||
AbsentNullable::Absent => Ok(default()), | ||
AbsentNullable::Null => Ok(None), | ||
AbsentNullable::Value(v) => Ok(Some(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.