[][src]Struct raystack::Grid

pub struct Grid { /* fields omitted */ }

A wrapper around a serde_json::Value which represents a Haystack Grid. Columns will always be sorted in alphabetical order.

Methods

impl Grid[src]

pub fn new(rows: Vec<Value>) -> Result<Self, ParseJsonGridError>[src]

Create a new Grid from rows. Each row must be a JSON Object.

Example

use raystack::Grid;
use serde_json::json;

let row = json!({"firstName": "Otis", "lastName": "Jackson Jr."});
let rows = vec![row];
let grid = Grid::new(rows).unwrap();
assert_eq!(grid.rows()[0]["firstName"], "Otis");

pub fn empty() -> Self[src]

Create an empty grid.

pub fn meta(&self) -> &Map<String, Value>[src]

Return a map which represents the metadata for the grid.

pub fn to_meta(&self) -> Map<String, Value>[src]

Return an owned map, which represents the metadata for the grid.

pub fn cols(&self) -> &Vec<Value>[src]

Return a vector of JSON values which represent the columns of the grid.

pub fn add_col<F>(&mut self, col_name: TagName, f: F) where
    F: Fn(&mut Map<String, Value>) -> Value
[src]

Add a new column, or overwrite an existing column by mapping each row to a new cell value.

pub fn to_cols(&self) -> Vec<Value>[src]

Return a vector of owned JSON values which represent the columns of the grid.

pub fn col_names(&self) -> Vec<TagName>[src]

Return a vector containing the column names in this grid.

pub fn col_name_strs(&self) -> Vec<&str>[src]

Return a vector containing the column names in this grid, as strings.

pub fn col_to_vec(&self, col_name: &str) -> Vec<Option<&Value>>[src]

Return a vector containing the values in the given column.

pub fn has_col_name(&self, name: &str) -> bool[src]

Returns true if the grid contains the given column name.

pub fn remove_col(&mut self, col_name: &str) -> bool[src]

Remove the column name from the grid if it is present, and return true if the column was removed.

pub fn remove_cols(&mut self, col_names: &[&str]) -> u32[src]

Remove the column names from the grid and return the number of columns that were removed. If a column name is not in the grid, nothing happens for that column name, and it does not increase the count of removed columns.

pub fn keep_cols(&mut self, cols_to_keep: &[&str])[src]

Keep the given column names and remove all other columns. If the column name is not present, nothing happens for that column name.

pub fn rename_col(&mut self, col_name: &TagName, new_col_name: &TagName) -> bool[src]

Rename a column in the grid. If the original column was contained in the grid, return true. If the original column did not exist in the grid, this function does not modify the grid, and returns false.

pub fn rows(&self) -> &Vec<Value>[src]

Return a vector of JSON values which represent the rows of the grid.

pub fn row_maps(&self) -> Vec<&Map<String, Value>>[src]

Return a vector of Maps which represent the rows of the grid.

pub fn to_rows(&self) -> Vec<Value>[src]

Return a vector of owned JSON values which represent the rows of the grid.

pub fn to_row_maps(&self) -> Vec<Map<String, Value>>[src]

Return a vector of owned JSON values which represent the rows of the grid.

pub fn sort_rows<F>(&mut self, compare: F) where
    F: FnMut(&Value, &Value) -> Ordering
[src]

Sort the rows with a comparator function. This sort is stable.

pub fn add_row(&mut self, row: Value) -> Result<(), ParseJsonGridError>[src]

Add a row to the grid. The row must be a JSON object.

pub fn add_rows(&mut self, rows: Vec<Value>) -> Result<(), ParseJsonGridError>[src]

Add rows to the grid. The rows to add must be a Vec containing only JSON objects.

pub fn size(&self) -> usize[src]

Return the number of rows in the grid.

pub fn is_empty(&self) -> bool[src]

Return true if the grid has no rows.

pub fn concat_grid(&mut self, grid: Grid)[src]

Concatenate the rows in the given grid to the current grid.

pub fn concat_grids(&mut self, grids: Vec<Grid>)[src]

For each given grid, concatenate its rows to the current grid.

pub fn concat_all(grids: Vec<Grid>) -> Grid[src]

Return a new grid which is formed by concatenating all the given grids together.

pub fn to_json_string(&self) -> String[src]

Return the string representation of the underlying JSON value.

pub fn to_json_string_pretty(&self) -> String[src]

Return a pretty formatted string representing the underlying JSON value.

pub fn is_error(&self) -> bool[src]

Returns true if the grid appears to be an error grid.

pub fn error_trace(&self) -> Option<String>[src]

Return the error trace if present.

pub fn to_csv_string_with_ordered_cols(
    &self,
    col_names: &[&str]
) -> Result<String, CsvError>
[src]

Return a string containing a CSV representation of the grid. The CSV string will have a header containing only the given column names, in the same order as they were provided. The header will include any given column names which are not present in the grid itself.

Nested structures such as Dicts (JSON objects) or Lists (JSON arrays) will not be expanded, and will be displayed as <StructureType>.

Example:

use raystack::Grid;
use serde_json::json;

let grid = Grid::new(vec![json!({"id": 1, "x": 2, "y": 3})]).unwrap();
let ordered_cols = vec!["y", "x", "colWithNoValues"];
let csv_string = grid
    .to_csv_string_with_ordered_cols(&ordered_cols)
    .unwrap();

assert_eq!(
    csv_string,
    "y,x,colWithNoValues\n3,2,\n".to_string()
);

pub fn to_csv_string(&self) -> Result<String, CsvError>[src]

Return a string containing a CSV representation of the grid.

Nested structures such as Dicts (JSON objects) or Lists (JSON arrays) will not be expanded, and will be displayed as <StructureType>.

Example:

use raystack::Grid;
use serde_json::json;

let grid = Grid::new(vec![json!({"id": 1, "x": 2, "y": 3})]).unwrap();
let csv_string = grid
    .to_csv_string()
    .unwrap();

assert_eq!(
    csv_string,
    "id,x,y\n1,2,3\n".to_string()
);

Trait Implementations

impl Clone for Grid[src]

impl PartialEq<Grid> for Grid[src]

impl Debug for Grid[src]

impl TryFrom<Value> for Grid[src]

type Error = ParseJsonGridError

The type returned in the event of a conversion error.

impl StructuralPartialEq for Grid[src]

Auto Trait Implementations

impl Send for Grid

impl Sync for Grid

impl Unpin for Grid

impl UnwindSafe for Grid

impl RefUnwindSafe for Grid

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]