notmongo 0.1.5

In-process, bad database with an API similar to MongoDB
Documentation
use bson;
use bson::Bson;

pub(crate) fn ensure_is_object<'a>(
    value: &'a Bson,
    error_source: &str,
) -> Result<&'a bson::Document, String> {
    match value {
        Bson::Document(value) => Ok(value),
        _ => Err(format!(
            "`{}` needs to be an object, not {}",
            error_source, value
        )),
    }
}

pub(crate) fn ensure_is_array<'a>(
    value: &'a Bson,
    error_source: &str,
) -> Result<&'a bson::Array, String> {
    match value {
        Bson::Array(value) => Ok(value),
        _ => Err(format!(
            "`{}` needs to be an array, not {}",
            error_source, value
        )),
    }
}

pub(crate) fn ensure_is_str<'a>(value: &'a Bson, error_source: &str) -> Result<&'a str, String> {
    match value {
        Bson::String(value) => Ok(value),
        _ => Err(format!(
            "`{}` needs to be a string, not {}",
            error_source, value
        )),
    }
}

pub(crate) fn get_key<'a>(
    value: &'a bson::Document,
    key: &str,
    error_source: &str,
) -> Result<&'a Bson, String> {
    match value.get(key) {
        Some(value) => Ok(value),
        None => Err(format!(
            "`{}` needs to contain a `{}` key.",
            error_source, key
        )),
    }
}