1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
//! # Overview
//! This crate provides functions which can query a SkySpark server, using
//! the Haystack REST API and the SkySpark REST API's `eval` operation.
//! Some Haystack operations are not implemented
//! (watch* operations, pointWrite and invokeAction).
//!
//! # Usage
//! 1. Create a new rust project: ```cargo new --bin my_project```
//! 1. Put these dependencies in your Cargo.toml:
//!     ```toml
//!     [dependencies]
//!     chrono = "0.4.7"
//!     chrono-tz = "0.5.1"
//!     raystack = "0.1.0"
//!     serde_json = "1.0.40"
//!     url = "1.7.2"
//!     ```
//! 1. Put this in `main.rs` to create and use a `SkySparkClient`:
//!     ```rust,no_run
//!     use raystack::{
//!         HaystackRest,
//!         SkySparkClient,
//!         SkySparkRest,
//!         ValueExt,
//!     };
//!     use url::Url;
//!     
//!     fn main() {
//!         let url = Url::parse("https://www.example.com/api/projName/").unwrap();
//!         let client = SkySparkClient::new(url, "username", "p4ssw0rd", None).unwrap();
//!         let sites_grid = client.eval("readAll(site)").unwrap();
//!     
//!         // Print the raw JSON:
//!         println!("{}", sites_grid.to_string_pretty());
//!     
//!         // Working with the Grid struct:
//!         println!("All columns: {:?}", sites_grid.cols());
//!         println!("first site id: {:?}", sites_grid.rows()[0]["id"].as_hs_ref().unwrap());
//!     }
//!     ```
//!
//! The Grid struct is a wrapper around the underlying JSON Value enum
//! provided by the `serde_json` crate. See the
//! [documentation for Value](https://docs.serde.rs/serde_json/enum.Value.html)
//! for more information on how to query for data stored within it.

mod api;
pub mod auth;
mod coord;
mod err;
mod grid;
mod hsref;
mod number;
mod tag;
mod value_ext;

use api::HaystackUrl;
pub use api::{HaystackRest, HisReadRange, SkySparkRest};
use chrono::DateTime;
use chrono_tz::Tz;
pub use coord::Coord;
use err::Result;
pub use err::{Error, ErrorKind};
pub use grid::{Grid, ParseJsonGridError};
pub use hsref::{ParseRefError, Ref};
pub use number::{Number, ParseNumberError};
use reqwest::Client as ReqwestClient;
use serde_json::json;
use std::convert::TryInto;
pub use tag::{is_tag_name, ParseTagNameError, TagName};
use url::Url;
pub use value_ext::ValueExt;

/// A client for interacting with a SkySpark server.
#[derive(Debug)]
pub struct SkySparkClient {
    auth_token: String,
    client: ReqwestClient,
    project_api_url: Url,
}

impl SkySparkClient {
    /// Create a new `SkySparkClient`.
    /// # Example
    /// ```rust,no_run
    /// use raystack::SkySparkClient;
    /// use url::Url;
    /// let url = Url::parse("https://skyspark.company.com/api/bigProject/").unwrap();
    /// let client = SkySparkClient::new(url, "username", "p4ssw0rd", None).unwrap();
    /// ```
    pub fn new(
        project_api_url: Url,
        username: &str,
        password: &str,
        timeout_in_seconds: Option<u64>,
    ) -> Result<Self> {
        use std::time::Duration;

        let project_api_url = add_backslash_if_necessary(project_api_url);

        let client = ReqwestClient::builder()
            .timeout(timeout_in_seconds.map(Duration::from_secs))
            .build()?;

        let mut auth_url = project_api_url.clone();
        auth_url.set_path("/ui");

        let auth_token = auth::new_auth_token(
            &client,
            auth_url.as_str(),
            username,
            password,
        )?;

        Ok(SkySparkClient {
            auth_token,
            client,
            project_api_url,
        })
    }

    fn auth_header_value(&self) -> String {
        format!("BEARER authToken={}", self.auth_token)
    }

    fn eval_url(&self) -> Url {
        self.append_to_url("eval")
    }

    fn get(&self, url: Url) -> reqwest::RequestBuilder {
        self.client
            .get(url)
            .header("Accept", "application/json")
            .header("Authorization", self.auth_header_value())
    }

    fn post(&self, url: Url, grid: &Grid) -> reqwest::RequestBuilder {
        self.client
            .post(url)
            .header("Accept", "application/json")
            .header("Authorization", self.auth_header_value())
            .header("Content-Type", "application/json")
            .body(grid.to_string())
    }

    fn res_to_grid(mut res: reqwest::Response) -> Result<Grid> {
        let json: serde_json::Value = res.json()?;
        let grid: Grid = json.try_into()?;

        if grid.is_error() {
            Err(Error::new(ErrorKind::Grid { err_grid: grid }))
        } else {
            Ok(grid)
        }
    }

    fn append_to_url(&self, s: &str) -> Url {
        self.project_api_url
            .join(s)
            .expect("since url ends with '/' this should never fail")
    }

    /// Return the project name for this client. If the url given to the
    /// `SkySparkClient` was correct, then this function should return a
    /// project name.
    pub fn project_name(&self) -> Option<&str> {
        let mut path_split =
            self.project_api_url.path_segments()?.collect::<Vec<_>>();
        match path_split.pop() {
            Some("") => path_split.pop(), // If empty, get the second last element
            last_elem @ Some(_) => last_elem,
            None => None,
        }
    }

    /// Return the project API url being used by this client.
    pub fn project_api_url(&self) -> &Url {
        &self.project_api_url
    }
}

/// If the given url ends with a backslash, return the url without
/// any modifications. If the given url does not end with a backslash,
/// append a backslash to the end and return a new `Url`.
fn add_backslash_if_necessary(url: Url) -> Url {
    let chars = url.as_str().chars().collect::<Vec<_>>();
    let last_char = chars.last().expect("parsed url should have >= 1 chars");
    if *last_char != '/' {
        Url::parse(&(url.to_string() + "/")).expect("adding '/' to the end of a parsable url should create another parsable url")
    } else {
        url
    }
}

impl HaystackRest for SkySparkClient {
    fn about(&self) -> Result<Grid> {
        let res = self.get(self.about_url()).send()?;
        Self::res_to_grid(res)
    }

    fn formats(&self) -> Result<Grid> {
        let res = self.get(self.formats_url()).send()?;
        Self::res_to_grid(res)
    }

    fn his_read(&self, id: &Ref, range: &HisReadRange) -> Result<Grid> {
        let row = json!({
            "id": id.to_encoded_json_string(),
            "range": range.to_string()
        });
        let req_grid = Grid::new_internal(vec![row]);

        let res = self.post(self.his_read_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn his_write_bool(
        &self,
        id: &Ref,
        his_data: &[(DateTime<Tz>, bool)],
    ) -> Result<Grid> {
        use api::to_zinc_encoded_string;

        let rows = his_data
            .iter()
            .map(|(date_time, value)| {
                json!({
                    "ts": format!("t:{}", to_zinc_encoded_string(date_time)),
                    "val": value
                })
            })
            .collect();

        let mut req_grid = Grid::new_internal(rows);
        req_grid.add_ref_to_meta(id);

        let res = self.post(self.his_write_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn his_write_num(
        &self,
        id: &Ref,
        his_data: &[(DateTime<Tz>, f64)],
        unit: &str,
    ) -> Result<Grid> {
        use api::to_zinc_encoded_string;

        let rows = his_data
            .iter()
            .map(|(date_time, value)| {
                json!({
                    "ts": format!("t:{}", to_zinc_encoded_string(date_time)),
                    "val": format!("n:{} {}", value, unit)
                })
            })
            .collect();

        let mut req_grid = Grid::new_internal(rows);
        req_grid.add_ref_to_meta(id);

        let res = self.post(self.his_write_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn his_write_str(
        &self,
        id: &Ref,
        his_data: &[(DateTime<Tz>, String)],
    ) -> Result<Grid> {
        use api::to_zinc_encoded_string;

        let rows = his_data
            .iter()
            .map(|(date_time, value)| {
                json!({
                    "ts": format!("t:{}", to_zinc_encoded_string(date_time)),
                    "val": value
                })
            })
            .collect();

        let mut req_grid = Grid::new_internal(rows);
        req_grid.add_ref_to_meta(id);

        let res = self.post(self.his_write_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn nav(&self, nav_id: Option<&str>) -> Result<Grid> {
        let req_grid = match nav_id {
            Some(nav_id) => {
                let row = json!({ "navId": nav_id });
                Grid::new_internal(vec![row])
            }
            None => Grid::new_internal(Vec::new()),
        };

        let res = self.post(self.nav_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn ops(&self) -> Result<Grid> {
        let res = self.get(self.ops_url()).send()?;
        Self::res_to_grid(res)
    }

    fn read(&self, filter: &str, limit: Option<u64>) -> Result<Grid> {
        let row = match limit {
            Some(integer) => json!({"filter": filter, "limit": integer}),
            None => json!({"filter": filter, "limit": "N"}),
        };

        let req_grid = Grid::new_internal(vec![row]);
        let res = self.post(self.read_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }

    fn read_by_ids(&self, ids: &[Ref]) -> Result<Grid> {
        let rows = ids
            .iter()
            .map(|id| json!({"id": id.to_encoded_json_string()}))
            .collect();

        let req_grid = Grid::new_internal(rows);
        let res = self.post(self.read_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }
}

impl HaystackUrl for SkySparkClient {
    fn about_url(&self) -> Url {
        self.append_to_url("about")
    }

    fn formats_url(&self) -> Url {
        self.append_to_url("formats")
    }

    fn his_read_url(&self) -> Url {
        self.append_to_url("hisRead")
    }

    fn his_write_url(&self) -> Url {
        self.append_to_url("hisWrite")
    }

    fn nav_url(&self) -> Url {
        self.append_to_url("nav")
    }

    fn ops_url(&self) -> Url {
        self.append_to_url("ops")
    }

    fn read_url(&self) -> Url {
        self.append_to_url("read")
    }
}

impl SkySparkRest for SkySparkClient {
    fn eval(&self, axon_expr: &str) -> Result<Grid> {
        let row = json!({ "expr": axon_expr });
        let req_grid = Grid::new_internal(vec![row]);
        let res = self.post(self.eval_url(), &req_grid).send()?;
        Self::res_to_grid(res)
    }
}

#[cfg(test)]
mod test {
    use crate::api::{HaystackRest, HisReadRange, SkySparkRest};
    use crate::grid::Grid;
    use crate::hsref::Ref;
    use crate::SkySparkClient;
    use serde_json::json;
    use url::Url;

    fn project_api_url() -> Url {
        let url_str = std::env::var("RUST_SKYSPARK_PROJECT_API_URL").unwrap();
        Url::parse(&url_str).unwrap()
    }

    fn username() -> String {
        std::env::var("RUST_SKYSPARK_USERNAME").unwrap()
    }

    fn password() -> String {
        std::env::var("RUST_SKYSPARK_PASSWORD").unwrap()
    }

    fn new_client() -> SkySparkClient {
        SkySparkClient::new(project_api_url(), &username(), &password(), None)
            .unwrap()
    }

    fn pprint(grid: &Grid) {
        println!("\n{}", grid.to_string_pretty());
    }

    #[test]
    fn about() {
        let client = new_client();
        let grid = client.about().unwrap();
        pprint(&grid);
        assert_eq!(grid.rows()[0]["whoami"], json!(username()));
    }

    #[test]
    fn formats() {
        let client = new_client();
        let grid = client.formats().unwrap();
        pprint(&grid);
        assert!(grid.rows()[0]["dis"].is_string());
    }

    #[test]
    fn his_read_today() {
        his_read(&HisReadRange::Today);
    }

    #[test]
    fn his_read_yesterday() {
        his_read(&HisReadRange::Yesterday);
    }

    #[test]
    fn his_read_date() {
        his_read(&HisReadRange::Date(chrono::NaiveDate::from_ymd(2019, 1, 1)));
    }

    #[test]
    fn his_read_date_span() {
        his_read(&HisReadRange::DateSpan {
            start: chrono::NaiveDate::from_ymd(2019, 1, 1),
            end: chrono::NaiveDate::from_ymd(2019, 1, 2),
        });
    }

    #[test]
    fn his_read_date_time_span() {
        use chrono::{DateTime, Duration};
        use chrono_tz::Australia::Sydney;

        let start = DateTime::parse_from_rfc3339("2019-01-01T00:00:00+10:00")
            .unwrap()
            .with_timezone(&Sydney);
        let end = start + Duration::days(1);
        his_read(&HisReadRange::DateTimeSpan { start, end });
    }

    #[test]
    fn his_read_date_time() {
        use chrono::DateTime;
        use chrono_tz::Australia::Sydney;

        let date_time =
            DateTime::parse_from_rfc3339("2012-10-01T00:00:00+10:00")
                .unwrap()
                .with_timezone(&Sydney);
        his_read(&HisReadRange::SinceDateTime { date_time });
    }

    #[test]
    fn his_read_date_time_utc() {
        use chrono::DateTime;
        use chrono_tz::Etc::UTC;

        let date_time = DateTime::parse_from_rfc3339("2012-10-01T00:00:00Z")
            .unwrap()
            .with_timezone(&UTC);
        his_read(&HisReadRange::SinceDateTime { date_time });
    }

    fn his_read(range: &HisReadRange) {
        let filter = format!("point and his and hisEnd");

        let client = new_client();
        let points_grid = client.read(&filter, Some(1)).unwrap();
        pprint(&points_grid);

        let point_ref_str = points_grid.rows()[0]["id"].as_str().unwrap();
        let point_ref = Ref::from_encoded_json_string(&point_ref_str).unwrap();
        let his_grid = client.his_read(&point_ref, &range).unwrap();
        pprint(&his_grid);

        assert!(his_grid.meta()["hisStart"].is_string());
        assert!(his_grid.meta()["hisEnd"].is_string());
    }

    #[test]
    fn his_write_bool() {
        assert_eq!("Add a matching point to the project", "");
        use chrono::{DateTime, Duration};
        use chrono_tz::Australia::Sydney;

        let date_time1 =
            DateTime::parse_from_rfc3339("2019-08-01T00:00:00+10:00")
                .unwrap()
                .with_timezone(&Sydney);
        let date_time2 = date_time1 + Duration::minutes(5);
        let date_time3 = date_time1 + Duration::minutes(10);

        let client = new_client();
        let id =
            Ref::new("@p:the_project:r:24efe1c4-24aef280".to_owned()).unwrap();
        let his_data =
            vec![(date_time1, true), (date_time2, false), (date_time3, true)];

        let res = client.his_write_bool(&id, &his_data[..]).unwrap();
        pprint(&res);
        assert_eq!(res.rows().len(), 0);
    }

    #[test]
    fn his_write_num() {
        assert_eq!("Add a matching point to the project", "");
        use chrono::{DateTime, Duration};
        use chrono_tz::Australia::Sydney;

        let date_time1 =
            DateTime::parse_from_rfc3339("2019-08-01T00:00:00+10:00")
                .unwrap()
                .with_timezone(&Sydney);
        let date_time2 = date_time1 + Duration::minutes(5);
        let date_time3 = date_time1 + Duration::minutes(10);

        let client = new_client();
        let id =
            Ref::new("@p:the_project:r:24efe317-acdc8f48".to_owned()).unwrap();
        let his_data =
            vec![(date_time1, 10.0), (date_time2, 15.34), (date_time3, 1.234)];

        let res = client.his_write_num(&id, &his_data[..], "L/s").unwrap();
        pprint(&res);
        assert_eq!(res.rows().len(), 0);
    }

    #[test]
    fn his_write_str() {
        assert_eq!("Add a matching point to the project", "");
        use chrono::{DateTime, Duration};
        use chrono_tz::Australia::Sydney;

        let date_time1 =
            DateTime::parse_from_rfc3339("2019-08-01T00:00:00+10:00")
                .unwrap()
                .with_timezone(&Sydney);
        let date_time2 = date_time1 + Duration::minutes(5);
        let date_time3 = date_time1 + Duration::minutes(10);

        let client = new_client();
        let id =
            Ref::new("@p:the_project:r:24efdc96-96baaf9d".to_owned()).unwrap();
        let his_data = vec![
            (date_time1, "hello".to_owned()),
            (date_time2, "world".to_owned()),
            (date_time3, "!".to_owned()),
        ];

        let res = client.his_write_str(&id, &his_data[..]).unwrap();
        pprint(&res);
        assert_eq!(res.rows().len(), 0);
    }

    #[test]
    fn nav_root() {
        let client = new_client();
        let grid = client.nav(None).unwrap();
        pprint(&grid);
        assert!(grid.rows()[0]["navId"].is_string());
    }

    #[test]
    fn nav() {
        let client = new_client();
        let root_grid = client.nav(None).unwrap();
        let child_nav_id = root_grid.rows()[0]["navId"].as_str().unwrap();

        let child_grid = client.nav(Some(&child_nav_id)).unwrap();
        pprint(&child_grid);
        let final_nav_id = child_grid.rows()[0]["navId"].as_str().unwrap();
        assert_ne!(child_nav_id, final_nav_id);
    }

    #[test]
    fn ops() {
        let client = new_client();
        let grid = client.ops().unwrap();
        pprint(&grid);
        assert!(grid.rows()[0]["name"].is_string());
    }

    #[test]
    fn read_with_no_limit() {
        let client = new_client();
        let grid = client.read("projMeta or uiMeta", None).unwrap();
        pprint(&grid);

        assert!(grid.rows()[0]["id"].is_string());
        let proj_meta = &grid.rows()[0]["projMeta"];
        let ui_meta = &grid.rows()[0]["uiMeta"];
        let marker = json!("m:");
        assert!(*proj_meta == marker || *ui_meta == marker);
    }

    #[test]
    fn read_with_zero_limit() {
        let client = new_client();
        let grid = client.read("id", Some(0)).unwrap();
        pprint(&grid);
        assert_eq!(grid.rows().len(), 0);
    }

    #[test]
    fn read_with_non_zero_limit() {
        let client = new_client();
        let grid = client.read("id", Some(1)).unwrap();
        pprint(&grid);
        assert_eq!(grid.rows().len(), 1);

        let grid = client.read("id", Some(3)).unwrap();
        pprint(&grid);
        assert_eq!(grid.rows().len(), 3);
    }

    #[test]
    fn read_by_ids_with_no_ids() {
        let client = new_client();
        let grid_result = client.read_by_ids(&Vec::new());
        assert!(grid_result.is_err());
    }

    #[test]
    fn read_by_ids_single() {
        let client = new_client();
        // Get some valid ids:
        let grid1 = client.read("id", Some(1)).unwrap();
        let raw_id1 = &grid1.rows()[0]["id"].as_str().unwrap();
        let ref1 = Ref::from_encoded_json_string(raw_id1).unwrap();

        let grid2 = client.read_by_ids(&vec![ref1]).unwrap();
        pprint(&grid2);
        assert_eq!(grid1, grid2);
    }

    #[test]
    fn read_by_ids_multiple() {
        let client = new_client();
        // Get some valid ids:
        let grid1 = client.read("id", Some(2)).unwrap();
        let raw_id1 = &grid1.rows()[0]["id"].as_str().unwrap();
        let raw_id2 = &grid1.rows()[1]["id"].as_str().unwrap();
        let ref1 = Ref::from_encoded_json_string(raw_id1).unwrap();
        let ref2 = Ref::from_encoded_json_string(raw_id2).unwrap();

        let grid2 = client.read_by_ids(&vec![ref1, ref2]).unwrap();
        pprint(&grid2);
        assert_eq!(grid1, grid2);
    }

    #[test]
    fn eval() {
        let client = new_client();
        let axon_expr = "readAll(id and mod)[0..1].keepCols([\"id\", \"mod\"])";
        let grid = client.eval(axon_expr).unwrap();
        pprint(&grid);
        assert!(grid.rows()[0]["id"].is_string());
    }

    #[test]
    fn add_backslash_necessary() {
        use crate::add_backslash_if_necessary;
        let url = Url::parse("http://www.example.com/api/proj").unwrap();
        let expected = Url::parse("http://www.example.com/api/proj/").unwrap();
        assert_eq!(add_backslash_if_necessary(url), expected);
    }

    #[test]
    fn add_backslash_not_necessary() {
        use crate::add_backslash_if_necessary;
        let url = Url::parse("http://www.example.com/api/proj/").unwrap();
        let expected = Url::parse("http://www.example.com/api/proj/").unwrap();
        assert_eq!(add_backslash_if_necessary(url), expected);
    }

    #[test]
    fn error_grid() {
        use crate::err::ErrorKind;

        let client = new_client();
        let grid_result = client.eval("reabDDDAll(test");

        assert!(grid_result.is_err());
        let err = grid_result.err().unwrap();
        let kind = err.kind();

        match kind {
            ErrorKind::Grid { err_grid } => {
                assert!(err_grid.is_error());
                assert!(err_grid.error_trace().is_some());
            }
            _ => panic!(),
        }
    }
}