@@ -46,6 +46,7 @@ pub struct Icon {
46
46
47
47
#[ derive( Serialize , Deserialize , Default , Debug , Clone , PartialEq ) ]
48
48
pub struct State {
49
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
49
50
pub open : Option < bool > ,
50
51
#[ serde( skip_serializing_if = "Option::is_none" ) ]
51
52
pub lastchange : Option < u64 > ,
@@ -57,6 +58,15 @@ pub struct State {
57
58
pub icon : Option < Icon > ,
58
59
}
59
60
61
+ impl State {
62
+ fn verify ( & self , version : StatusBuilderVersion ) -> Result < ( ) , String > {
63
+ if version != StatusBuilderVersion :: V14 && self . open . is_none ( ) {
64
+ return Err ( "state.open must be present" . into ( ) ) ;
65
+ }
66
+ Ok ( ( ) )
67
+ }
68
+ }
69
+
60
70
#[ derive( Serialize , Deserialize , Default , Debug , Clone , PartialEq ) ]
61
71
pub struct Event {
62
72
pub name : String ,
@@ -229,7 +239,8 @@ pub struct Status {
229
239
pub issue_report_channels : Vec < IssueReportChannel > ,
230
240
231
241
// Mutable data
232
- pub state : State ,
242
+ #[ serde( skip_serializing_if = "Option::is_none" ) ]
243
+ pub state : Option < State > ,
233
244
#[ serde( skip_serializing_if = "Option::is_none" ) ]
234
245
pub sensors : Option < Sensors > ,
235
246
@@ -295,6 +306,7 @@ pub struct StatusBuilder {
295
306
radio_show : Option < Vec < RadioShow > > ,
296
307
issue_report_channels : Vec < IssueReportChannel > ,
297
308
extensions : Extensions ,
309
+ state : Option < State > ,
298
310
}
299
311
300
312
impl StatusBuilder {
@@ -329,6 +341,11 @@ impl StatusBuilder {
329
341
}
330
342
}
331
343
344
+ pub fn state ( mut self , state : State ) -> Self {
345
+ self . state = Some ( state) ;
346
+ self
347
+ }
348
+
332
349
pub fn logo < S : Into < String > > ( mut self , logo : S ) -> Self {
333
350
self . logo = Some ( logo. into ( ) ) ;
334
351
self
@@ -410,6 +427,9 @@ impl StatusBuilder {
410
427
if let Some ( spacefed) = & self . spacefed {
411
428
spacefed. verify ( self . version ) ?;
412
429
}
430
+ if let Some ( state) = & self . state {
431
+ state. verify ( self . version ) ?;
432
+ }
413
433
414
434
if self . version == StatusBuilderVersion :: V14 {
415
435
if contact. jabber . is_some ( ) {
@@ -425,8 +445,13 @@ impl StatusBuilder {
425
445
if !self . issue_report_channels . is_empty ( ) {
426
446
return Err ( "issue_report_channels key was removed" . into ( ) ) ;
427
447
}
428
- } else if self . issue_report_channels . is_empty ( ) {
429
- return Err ( "issue_report_channels must not be empty" . into ( ) ) ;
448
+ } else {
449
+ if self . issue_report_channels . is_empty ( ) {
450
+ return Err ( "issue_report_channels must not be empty" . into ( ) ) ;
451
+ }
452
+ if self . state . is_none ( ) {
453
+ return Err ( "state must be present in v0.13" . into ( ) ) ;
454
+ }
430
455
}
431
456
432
457
Ok ( Status {
@@ -444,6 +469,7 @@ impl StatusBuilder {
444
469
events : self . events ,
445
470
radio_show : self . radio_show ,
446
471
issue_report_channels : self . issue_report_channels ,
472
+ state : self . state ,
447
473
extensions : self . extensions ,
448
474
..Default :: default ( )
449
475
} )
@@ -567,6 +593,10 @@ mod test {
567
593
let status = StatusBuilder :: mixed ( "foo" )
568
594
. logo ( "bar" )
569
595
. url ( "foobar" )
596
+ . state ( State {
597
+ open : Some ( false ) ,
598
+ ..State :: default ( )
599
+ } )
570
600
. location ( Location :: default ( ) )
571
601
. contact ( Contact :: default ( ) )
572
602
. add_issue_report_channel ( IssueReportChannel :: Email )
@@ -580,6 +610,10 @@ mod test {
580
610
space: "foo" . into( ) ,
581
611
logo: "bar" . into( ) ,
582
612
url: "foobar" . into( ) ,
613
+ state: Some ( State {
614
+ open: Some ( false ) ,
615
+ ..State :: default ( )
616
+ } ) ,
583
617
issue_report_channels: vec![ IssueReportChannel :: Email ] ,
584
618
..Status :: default ( )
585
619
}
@@ -591,6 +625,10 @@ mod test {
591
625
let status = StatusBuilder :: new ( "foo" )
592
626
. logo ( "bar" )
593
627
. url ( "foobar" )
628
+ . state ( State {
629
+ open : Some ( false ) ,
630
+ ..State :: default ( )
631
+ } )
594
632
. location ( Location :: default ( ) )
595
633
. contact ( Contact :: default ( ) )
596
634
. spacefed ( Spacefed {
@@ -650,6 +688,10 @@ mod test {
650
688
let status = StatusBuilder :: new ( "foo" )
651
689
. logo ( "bar" )
652
690
. url ( "foobar" )
691
+ . state ( State {
692
+ open : Some ( false ) ,
693
+ ..State :: default ( )
694
+ } )
653
695
. location ( Location :: default ( ) )
654
696
. contact ( Contact :: default ( ) )
655
697
. add_extension ( "aaa" , Value :: Array ( vec ! [ Value :: Null , Value :: from( 42 ) ] ) )
@@ -666,6 +708,10 @@ mod test {
666
708
let status = StatusBuilder :: new ( "a" )
667
709
. logo ( "b" )
668
710
. url ( "c" )
711
+ . state ( State {
712
+ open : Some ( false ) ,
713
+ ..State :: default ( )
714
+ } )
669
715
. location ( Location :: default ( ) )
670
716
. contact ( Contact :: default ( ) )
671
717
. add_issue_report_channel ( IssueReportChannel :: Email )
@@ -675,7 +721,7 @@ mod test {
675
721
& to_string( & status. unwrap( ) ) . unwrap( ) ,
676
722
"{\" api\" :\" 0.13\" ,\" space\" :\" a\" ,\" logo\" :\" b\" ,\" url\" :\" c\" ,\
677
723
\" location\" :{\" lat\" :0.0,\" lon\" :0.0},\" contact\" :{},\" issue_report_channels\" :[\" email\" ],\
678
- \" state\" :{\" open\" :null }}"
724
+ \" state\" :{\" open\" :false }}"
679
725
) ;
680
726
}
681
727
@@ -684,6 +730,10 @@ mod test {
684
730
let status = StatusBuilder :: new ( "a" )
685
731
. logo ( "b" )
686
732
. url ( "c" )
733
+ . state ( State {
734
+ open : Some ( false ) ,
735
+ ..State :: default ( )
736
+ } )
687
737
. location ( Location :: default ( ) )
688
738
. contact ( Contact :: default ( ) )
689
739
. add_issue_report_channel ( IssueReportChannel :: Email )
@@ -695,7 +745,7 @@ mod test {
695
745
& to_string( & status. unwrap( ) ) . unwrap( ) ,
696
746
"{\" api\" :\" 0.13\" ,\" space\" :\" a\" ,\" logo\" :\" b\" ,\" url\" :\" c\" ,\
697
747
\" location\" :{\" lat\" :0.0,\" lon\" :0.0},\" contact\" :{},\" issue_report_channels\" :[\" email\" ],\
698
- \" state\" :{\" open\" :null },\" ext_aaa\" :\" xxx\" ,\" ext_bbb\" :[null,42]}"
748
+ \" state\" :{\" open\" :false },\" ext_aaa\" :\" xxx\" ,\" ext_bbb\" :[null,42]}"
699
749
) ;
700
750
}
701
751
@@ -704,6 +754,10 @@ mod test {
704
754
let mut status = StatusBuilder :: new ( "foo" )
705
755
. logo ( "bar" )
706
756
. url ( "foobar" )
757
+ . state ( State {
758
+ open : Some ( false ) ,
759
+ ..State :: default ( )
760
+ } )
707
761
. location ( Location :: default ( ) )
708
762
. contact ( Contact :: default ( ) )
709
763
. add_issue_report_channel ( IssueReportChannel :: Email )
@@ -728,6 +782,10 @@ mod test {
728
782
let status = StatusBuilder :: new ( "a" )
729
783
. logo ( "b" )
730
784
. url ( "c" )
785
+ . state ( State {
786
+ open : Some ( false ) ,
787
+ ..State :: default ( )
788
+ } )
731
789
. location ( Location :: default ( ) )
732
790
. contact ( Contact :: default ( ) )
733
791
. add_issue_report_channel ( IssueReportChannel :: Email )
0 commit comments