ratio-graph 0.23.3

Ratio's graph manipulation library.
Documentation
//! # Serde utilities
//!
//! ## License
//!
//! This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
//! If a copy of the MPL was not distributed with this file,
//! You can obtain one at <https://mozilla.org/MPL/2.0/>.
//!
//! **Code examples both in the docstrings and rendered documentation are free to use.**

use std::collections::{BTreeMap, BTreeSet};

/// Whether a value equals its type's default.
pub fn is_default<T: Default + PartialEq>(t: &T) -> bool {
    t == &T::default()
}

/// Whether a vec is empty.
pub fn is_empty_vec<T>(t: &[T]) -> bool {
    t.is_empty()
}

/// Whether a set is empty.
pub fn is_empty_set<T>(t: &BTreeSet<T>) -> bool {
    t.is_empty()
}

/// Whether a map is empty.
pub fn is_empty_map<K, V>(t: &BTreeMap<K, V>) -> bool {
    t.is_empty()
}

/// Helper for serializing maps with consistent ordering.
pub fn sort_alphabetically<T: serde::Serialize, S: serde::Serializer>(
    value: &T,
    serializer: S,
) -> Result<S::Ok, S::Error> {
    let value = serde_json::to_value(value).map_err(serde::ser::Error::custom)?;
    serde::Serialize::serialize(&value, serializer)
}