Grid

Struct Grid 

Source
pub struct Grid { /* private fields */ }
Expand description

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

Implementations§

Source§

impl Grid

Source

pub fn new(rows: Vec<Value>) -> Result<Grid, ParseJsonGridError>

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");
Source

pub fn empty() -> Grid

Create an empty grid.

Source

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

Return a map which represents the metadata for the grid.

Source

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

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

Source

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

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

Examples found in repository?
examples/quick_client.rs (line 14)
3fn main() -> Result<(), Box<dyn Error>> {
4    use raystack_blocking::{new_client, ValueExt};
5
6    let mut client = new_client("https://www.example.com/api/projName/", "username", "p4ssw0rd")?;
7
8    let sites_grid = client.eval("readAll(site)")?;
9
10    // Print the raw JSON:
11    println!("{}", sites_grid.to_json_string_pretty());
12
13    // Working with the Grid struct:
14    println!("All columns: {:?}", sites_grid.cols());
15    println!(
16        "first site id: {:?}",
17        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
18    );
19
20    Ok(())
21}
More examples
Hide additional examples
examples/client.rs (line 24)
7fn main() -> Result<(), Box<dyn Error>> {
8    use raystack_blocking::{SkySparkClient, ValueExt};
9    use url::Url;
10
11    let url = Url::parse("https://www.example.com/api/projName/")?;
12
13    // If you are going to create many `SkySparkClient`s,
14    // reuse the same `reqwest::Client` in each `SkySparkClient`
15    // by using the `SkySparkClient::new_with_client` function instead.
16    let mut client = SkySparkClient::new(url, "username", "p4ssw0rd")?;
17
18    let sites_grid = client.eval("readAll(site)")?;
19
20    // Print the raw JSON:
21    println!("{}", sites_grid.to_json_string_pretty());
22
23    // Working with the Grid struct:
24    println!("All columns: {:?}", sites_grid.cols());
25    println!(
26        "first site id: {:?}",
27        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
28    );
29
30    Ok(())
31}
Source

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

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

Source

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

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

Source

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

Return a vector containing the column names in this grid.

Source

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

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

Source

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

Return a vector containing the values in the given column.

Source

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

Returns true if the grid contains the given column name.

Source

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

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

Source

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

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.

Source

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

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

Source

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

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.

Source

pub fn map_col<F>(&mut self, col_name: &TagName, f: F)
where F: Fn(&Value) -> Value,

Modify the grid by applying the mapping function to each value in the specified column.

Source

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

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

Examples found in repository?
examples/quick_client.rs (line 17)
3fn main() -> Result<(), Box<dyn Error>> {
4    use raystack_blocking::{new_client, ValueExt};
5
6    let mut client = new_client("https://www.example.com/api/projName/", "username", "p4ssw0rd")?;
7
8    let sites_grid = client.eval("readAll(site)")?;
9
10    // Print the raw JSON:
11    println!("{}", sites_grid.to_json_string_pretty());
12
13    // Working with the Grid struct:
14    println!("All columns: {:?}", sites_grid.cols());
15    println!(
16        "first site id: {:?}",
17        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
18    );
19
20    Ok(())
21}
More examples
Hide additional examples
examples/client.rs (line 27)
7fn main() -> Result<(), Box<dyn Error>> {
8    use raystack_blocking::{SkySparkClient, ValueExt};
9    use url::Url;
10
11    let url = Url::parse("https://www.example.com/api/projName/")?;
12
13    // If you are going to create many `SkySparkClient`s,
14    // reuse the same `reqwest::Client` in each `SkySparkClient`
15    // by using the `SkySparkClient::new_with_client` function instead.
16    let mut client = SkySparkClient::new(url, "username", "p4ssw0rd")?;
17
18    let sites_grid = client.eval("readAll(site)")?;
19
20    // Print the raw JSON:
21    println!("{}", sites_grid.to_json_string_pretty());
22
23    // Working with the Grid struct:
24    println!("All columns: {:?}", sites_grid.cols());
25    println!(
26        "first site id: {:?}",
27        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
28    );
29
30    Ok(())
31}
Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

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

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

Source

pub fn size(&self) -> usize

Return the number of rows in the grid.

Source

pub fn is_empty(&self) -> bool

Return true if the grid has no rows.

Source

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

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

Source

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

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

Source

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

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

Source

pub fn to_json_string(&self) -> String

Return the string representation of the underlying JSON value.

Source

pub fn to_json_string_pretty(&self) -> String

Return a pretty formatted string representing the underlying JSON value.

Examples found in repository?
examples/quick_client.rs (line 11)
3fn main() -> Result<(), Box<dyn Error>> {
4    use raystack_blocking::{new_client, ValueExt};
5
6    let mut client = new_client("https://www.example.com/api/projName/", "username", "p4ssw0rd")?;
7
8    let sites_grid = client.eval("readAll(site)")?;
9
10    // Print the raw JSON:
11    println!("{}", sites_grid.to_json_string_pretty());
12
13    // Working with the Grid struct:
14    println!("All columns: {:?}", sites_grid.cols());
15    println!(
16        "first site id: {:?}",
17        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
18    );
19
20    Ok(())
21}
More examples
Hide additional examples
examples/client.rs (line 21)
7fn main() -> Result<(), Box<dyn Error>> {
8    use raystack_blocking::{SkySparkClient, ValueExt};
9    use url::Url;
10
11    let url = Url::parse("https://www.example.com/api/projName/")?;
12
13    // If you are going to create many `SkySparkClient`s,
14    // reuse the same `reqwest::Client` in each `SkySparkClient`
15    // by using the `SkySparkClient::new_with_client` function instead.
16    let mut client = SkySparkClient::new(url, "username", "p4ssw0rd")?;
17
18    let sites_grid = client.eval("readAll(site)")?;
19
20    // Print the raw JSON:
21    println!("{}", sites_grid.to_json_string_pretty());
22
23    // Working with the Grid struct:
24    println!("All columns: {:?}", sites_grid.cols());
25    println!(
26        "first site id: {:?}",
27        sites_grid.rows()[0]["id"].as_hs_ref().unwrap()
28    );
29
30    Ok(())
31}
Source

pub fn is_error(&self) -> bool

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

Source

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

Return the error trace if present.

Trait Implementations§

Source§

impl Clone for Grid

Source§

fn clone(&self) -> Grid

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Grid

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Grid

Source§

fn eq(&self, other: &Grid) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<Value> for Grid

Source§

type Error = ParseJsonGridError

The type returned in the event of a conversion error.
Source§

fn try_from(value: Value) -> Result<Grid, <Grid as TryFrom<Value>>::Error>

Performs the conversion.
Source§

impl StructuralPartialEq for Grid

Auto Trait Implementations§

§

impl Freeze for Grid

§

impl RefUnwindSafe for Grid

§

impl Send for Grid

§

impl Sync for Grid

§

impl Unpin for Grid

§

impl UnwindSafe for Grid

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more