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
impl Grid
Sourcepub fn new(rows: Vec<Value>) -> Result<Grid, ParseJsonGridError>
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");Sourcepub fn meta(&self) -> &Map<String, Value>
pub fn meta(&self) -> &Map<String, Value>
Return a map which represents the metadata for the grid.
Sourcepub fn to_meta(&self) -> Map<String, Value>
pub fn to_meta(&self) -> Map<String, Value>
Return an owned map, which represents the metadata for the grid.
Sourcepub fn cols(&self) -> &Vec<Value>
pub fn cols(&self) -> &Vec<Value>
Return a vector of JSON values which represent the columns of the grid.
Examples found in repository?
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
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}Sourcepub fn add_col<F>(&mut self, col_name: TagName, f: F)
pub fn add_col<F>(&mut self, col_name: TagName, f: F)
Add a new column, or overwrite an existing column by mapping each row to a new cell value.
Sourcepub fn to_cols(&self) -> Vec<Value>
pub fn to_cols(&self) -> Vec<Value>
Return a vector of owned JSON values which represent the columns of the grid.
Sourcepub fn col_names(&self) -> Vec<TagName>
pub fn col_names(&self) -> Vec<TagName>
Return a vector containing the column names in this grid.
Sourcepub fn col_name_strs(&self) -> Vec<&str>
pub fn col_name_strs(&self) -> Vec<&str>
Return a vector containing the column names in this grid, as strings.
Sourcepub fn col_to_vec(&self, col_name: &str) -> Vec<Option<&Value>>
pub fn col_to_vec(&self, col_name: &str) -> Vec<Option<&Value>>
Return a vector containing the values in the given column.
Sourcepub fn has_col_name(&self, name: &str) -> bool
pub fn has_col_name(&self, name: &str) -> bool
Returns true if the grid contains the given column name.
Sourcepub fn remove_col(&mut self, col_name: &str) -> bool
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.
Sourcepub fn remove_cols(&mut self, col_names: &[&str]) -> u32
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.
Sourcepub fn keep_cols(&mut self, cols_to_keep: &[&str])
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.
Sourcepub fn rename_col(&mut self, col_name: &TagName, new_col_name: &TagName) -> bool
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.
Sourcepub fn map_col<F>(&mut self, col_name: &TagName, f: F)
pub fn map_col<F>(&mut self, col_name: &TagName, f: F)
Modify the grid by applying the mapping function to each value in the specified column.
Sourcepub fn rows(&self) -> &Vec<Value>
pub fn rows(&self) -> &Vec<Value>
Return a vector of JSON values which represent the rows of the grid.
Examples found in repository?
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
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}Sourcepub fn row_maps(&self) -> Vec<&Map<String, Value>>
pub fn row_maps(&self) -> Vec<&Map<String, Value>>
Return a vector of Maps which represent the rows of the grid.
Sourcepub fn to_rows(&self) -> Vec<Value>
pub fn to_rows(&self) -> Vec<Value>
Return a vector of owned JSON values which represent the rows of the grid.
Sourcepub fn to_row_maps(&self) -> Vec<Map<String, Value>>
pub fn to_row_maps(&self) -> Vec<Map<String, Value>>
Return a vector of owned JSON values which represent the rows of the grid.
Sourcepub fn sort_rows<F>(&mut self, compare: F)
pub fn sort_rows<F>(&mut self, compare: F)
Sort the rows with a comparator function. This sort is stable.
Sourcepub fn add_row(&mut self, row: Value) -> Result<(), ParseJsonGridError>
pub fn add_row(&mut self, row: Value) -> Result<(), ParseJsonGridError>
Add a row to the grid. The row must be a JSON object.
Sourcepub fn add_rows(&mut self, rows: Vec<Value>) -> Result<(), ParseJsonGridError>
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.
Sourcepub fn concat_grid(&mut self, grid: Grid)
pub fn concat_grid(&mut self, grid: Grid)
Concatenate the rows in the given grid to the current grid.
Sourcepub fn concat_grids(&mut self, grids: Vec<Grid>)
pub fn concat_grids(&mut self, grids: Vec<Grid>)
For each given grid, concatenate its rows to the current grid.
Sourcepub fn concat_all(grids: Vec<Grid>) -> Grid
pub fn concat_all(grids: Vec<Grid>) -> Grid
Return a new grid which is formed by concatenating all the given grids together.
Sourcepub fn to_json_string(&self) -> String
pub fn to_json_string(&self) -> String
Return the string representation of the underlying JSON value.
Sourcepub fn to_json_string_pretty(&self) -> String
pub fn to_json_string_pretty(&self) -> String
Return a pretty formatted string representing the underlying JSON value.
Examples found in repository?
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
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}Sourcepub fn error_trace(&self) -> Option<String>
pub fn error_trace(&self) -> Option<String>
Return the error trace if present.