js_option-0.1.1/.cargo_vcs_info.json0000644000000001360000000000100130370ustar { "git": { "sha1": "aea4a281ea689e5ab17e8213310bfc11059d776c" }, "path_in_vcs": "" }js_option-0.1.1/Cargo.toml0000644000000020340000000000100110340ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "js_option" version = "0.1.1" include = [ "src/**/*", "LICENSE", "README.md", ] description = "An Option-like type with separate null and undefined variants" readme = "README.md" license = "MIT" repository = "https://github.com/ruma/js_option" resolver = "2" [dependencies.serde_crate] version = "1.0.125" optional = true package = "serde" [dev-dependencies.serde_crate] version = "1.0.125" features = ["derive"] package = "serde" [dev-dependencies.serde_json] version = "1.0.64" [features] default = ["serde"] serde = ["serde_crate"] js_option-0.1.1/Cargo.toml.orig000064400000000000000000000010560072674642500145500ustar 00000000000000[package] name = "js_option" version = "0.1.1" description = "An Option-like type with separate null and undefined variants" readme = "README.md" repository = "https://github.com/ruma/js_option" license = "MIT" edition = "2018" resolver = "2" include = ["src/**/*", "LICENSE", "README.md"] [features] default = ["serde"] serde = ["serde_crate"] [dependencies] serde_crate = { package = "serde", version = "1.0.125", optional = true } [dev-dependencies] serde_crate = { package = "serde", version = "1.0.125", features = ["derive"] } serde_json = "1.0.64" js_option-0.1.1/LICENSE000064400000000000000000000020370072674642500126660ustar 00000000000000Copyright (c) 2021 Lumeo, Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. js_option-0.1.1/README.md000064400000000000000000000020000072674642500131260ustar 00000000000000# js_option This crate provides a type `JsOption` that is very similar to the standard library's `Option` type except that it has three variants: * `Some(value)`: Like `Option::Some` * `Null`: Explicitly not some value * `Undefined`: Implicitly not some value This type can be useful when you want to deserialize JSON to a Rust struct and not loose information: A regular `Option` deserializes to `None` from both an explicit `null` or a missing field (this is due to special casing of `Option` in the `Deserialize` and `Serialize` derive macros, for other types a missing field will make deserialization fail unless there is a `#[serde(skip)]`, `#[serde(skip_deserializing)]` or `#[serde(default)]` attribute). ## Example: ``` # extern crate serde_crate as serde; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize)] struct MyStruct { #[serde(default, skip_serializing_if = "JsOption::is_undefined")] my_field: JsOption, } ``` ## License [MIT](https://opensource.org/licenses/MIT) js_option-0.1.1/src/lib.rs000064400000000000000000000131510072674642500135630ustar 00000000000000//! This crate provides a type `JsOption` that is very similar to the standard //! library's `Option` type except that it has three variants: //! //! * `Some(value)`: Like `Option::Some` //! * `Null`: Explicitly not some value //! * `Undefined`: Implicitly not some value //! //! This type can be useful when you want to deserialize JSON to a Rust struct //! and not loose information: A regular `Option` deserializes to `None` from //! both an explicit `null` or a missing field (this is due to special casing of //! `Option` in the `Deserialize` and `Serialize` derive macros, for other types //! a missing field will make deserialization fail unless there is a //! `#[serde(skip)]`, `#[serde(skip_deserializing)]` or `#[serde(default)]` //! attribute). //! //! # Example: //! //! ``` //! # extern crate serde_crate as serde; //! use js_option::JsOption; //! use serde::{Deserialize, Serialize}; //! //! #[derive(Serialize, Deserialize)] //! # #[serde(crate = "serde")] //! struct MyStruct { //! #[serde(default, skip_serializing_if = "JsOption::is_undefined")] //! my_field: JsOption, //! } //! ``` #![warn(missing_docs)] use std::ops::{Deref, DerefMut}; #[cfg(feature = "serde")] mod serde; /// An `Option`-like type with two data-less variants in addition to `Some`: /// `Null` and `Undefined`. #[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)] pub enum JsOption { /// Some value `T` Some(T), /// Explicit absence of a value Null, /// Implicit absence of a value Undefined, } impl JsOption { /// Construct a `JsOption` from a regular `Option`. /// /// `None` will be converted to to `Null`. pub fn from_option(opt: Option) -> Self { match opt { Some(val) => Self::Some(val), None => Self::Null, } } /// Construct a `JsOption` from a regular `Option`. /// /// `None` will be converted to `Undefined`. pub fn from_implicit_option(opt: Option) -> Self { match opt { Some(val) => Self::Some(val), None => Self::Undefined, } } /// Convert a `JsOption` to `Option`. pub fn into_option(self) -> Option { match self { Self::Some(val) => Some(val), _ => None, } } /// Convert a `JsOption` to `Option>`. /// /// `Null` is represented as `Some(None)` while `Undefined` is represented /// as `None`. pub fn into_nested_option(self) -> Option> { match self { Self::Some(val) => Some(Some(val)), Self::Null => Some(None), Self::Undefined => None, } } /// Returns `true` if the `JsOption` contains a value. pub const fn is_some(&self) -> bool { matches!(self, Self::Some(_)) } /// Returns `true` if the `JsOption` is `Null`. pub const fn is_null(&self) -> bool { matches!(self, Self::Null) } /// Returns `true` if the `JsOption` is `Undefined`. pub const fn is_undefined(&self) -> bool { matches!(self, Self::Undefined) } /// Returns the contained `Some` value, consuming the `self` value. /// /// # Panics /// /// Panics if the self value equals `Null` or `Undefined`. #[track_caller] pub fn unwrap(self) -> T { match self { Self::Some(val) => val, Self::Null => panic!("called `JsOption::unwrap()` on `Null`"), Self::Undefined => panic!("called `JsOption::unwrap()` on `Undefined`"), } } /// Returns the contained `Some` value or a provided default. pub fn unwrap_or(self, default: T) -> T { match self { Self::Some(val) => val, _ => default, } } /// Returns the contained `Some` value computes is from a closure. pub fn unwrap_or_else T>(self, f: F) -> T { match self { Self::Some(val) => val, _ => f(), } } /// Maps a `JsOption` to `JsOption` by applying a function to a /// contained value. pub fn map U>(self, f: F) -> JsOption { match self { Self::Some(val) => JsOption::Some(f(val)), Self::Null => JsOption::Null, Self::Undefined => JsOption::Undefined, } } /// Converts from `&Option` to `Option<&T>`. pub const fn as_ref(&self) -> JsOption<&T> { match self { Self::Some(x) => JsOption::Some(x), Self::Null => JsOption::Null, Self::Undefined => JsOption::Undefined, } } /// Converts from `&mut Option` to `Option<&mut T>`. pub fn as_mut(&mut self) -> JsOption<&mut T> { match self { Self::Some(x) => JsOption::Some(x), Self::Null => JsOption::Null, Self::Undefined => JsOption::Undefined, } } } impl JsOption { /// Returns the contained `Some` value or a default. pub fn unwrap_or_default(self) -> T { self.unwrap_or_else(Default::default) } } impl JsOption { /// Converts from `&JsOption` to `JsOption<&T::Target>`. pub fn as_deref(&self) -> JsOption<&::Target> { self.as_ref().map(|val| val.deref()) } } impl JsOption { /// Converts from `&mut JsOption` to `JsOption<&mut T::Target>`. pub fn as_deref_mut(&mut self) -> JsOption<&mut ::Target> { self.as_mut().map(|val| val.deref_mut()) } } impl Default for JsOption { /// Returns the default value, `JsOption::Undefined`. fn default() -> Self { Self::Undefined } } js_option-0.1.1/src/serde.rs000064400000000000000000000024310072674642500141160ustar 00000000000000// Undo rename from Cargo.toml extern crate serde_crate as serde; use serde::{ de::{Deserialize, Deserializer}, ser::{Error as _, Serialize, Serializer}, }; use crate::JsOption; impl<'de, T> Deserialize<'de> for JsOption where T: Deserialize<'de>, { /// Deserialize a `JsOption`. /// /// This implementation will never return `Undefined`. You need to use /// `#[serde(default)]` to get `Undefined` when the field is not present. fn deserialize(deserializer: D) -> Result where D: Deserializer<'de>, { Option::::deserialize(deserializer).map(Self::from_option) } } impl Serialize for JsOption where T: Serialize, { /// Serialize a `JsOption`. /// /// Serialization will fail for `JsOption::Undefined`. You need to use /// `#[skip_serializing_if = "JsOption::is_undefined"]` to stop the field /// from being serialized altogether. fn serialize(&self, serializer: S) -> Result where S: Serializer, { match self { Self::Some(val) => serializer.serialize_some(val), Self::Null => serializer.serialize_none(), Self::Undefined => Err(S::Error::custom("attempted to serialize `undefined`")), } } }