perspective 2.0.0

A data visualization and analytics component, especially well-suited for large and/or streaming datasets.
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2018, the Perspective Authors.
//
// This file is part of the Perspective library, distributed under the terms
// of the Apache License 2.0.  The full license can be found in the LICENSE
// file.

use crate::utils::ApiResult;

pub trait AsBlob {
    /// Standardized conversions from common `wasm_bindgen` types to
    /// `js_sys::Blob`, which is commonly necessary to provide data to a
    /// download or clipboard action.
    fn as_blob(&self) -> ApiResult<web_sys::Blob>;
}

impl AsBlob for js_sys::ArrayBuffer {
    fn as_blob(&self) -> ApiResult<web_sys::Blob> {
        let array = std::iter::once(js_sys::Uint8Array::new(self)).collect::<js_sys::Array>();
        Ok(web_sys::Blob::new_with_u8_array_sequence(&array)?)
    }
}

impl AsBlob for js_sys::JsString {
    fn as_blob(&self) -> ApiResult<web_sys::Blob> {
        let array = std::iter::once(self).collect::<js_sys::Array>();
        let mut options = web_sys::BlobPropertyBag::new();
        options.type_("text/plain");
        Ok(web_sys::Blob::new_with_str_sequence_and_options(
            &array, &options,
        )?)
    }
}

impl AsBlob for js_sys::Object {
    fn as_blob(&self) -> ApiResult<web_sys::Blob> {
        let array = std::iter::once(js_sys::JSON::stringify(self)?).collect::<js_sys::Array>();
        let mut options = web_sys::BlobPropertyBag::new();
        options.type_("text/plain");
        Ok(web_sys::Blob::new_with_str_sequence_and_options(
            &array, &options,
        )?)
    }
}