@@ -42,8 +42,45 @@ pub struct DbgpError {
4242
4343#[ derive( Debug , Clone , PartialEq ) ]
4444pub struct ContextGetResponse {
45+ pub properties : Properties ,
46+ }
47+ impl ContextGetResponse {
48+ }
49+ #[ derive( Debug , Clone , PartialEq , Default ) ]
50+ pub struct Properties {
4551 pub properties : Vec < Property > ,
4652}
53+ impl Properties {
54+ pub fn is_empty ( & self ) -> bool {
55+ self . properties . is_empty ( )
56+ }
57+ pub fn none ( ) -> Self {
58+ Self { properties : vec ! [ ] }
59+ }
60+ pub fn defined_properties ( & self ) -> Vec < & Property > {
61+ let mut props = vec ! [ ] ;
62+ for property in & self . properties {
63+ if property. property_type == PropertyType :: Undefined {
64+ continue ;
65+ }
66+ props. push ( property) ;
67+ }
68+ props
69+ }
70+
71+ pub ( crate ) fn get ( & self , name : & str ) -> Option < & Property > {
72+ for property in self . defined_properties ( ) {
73+ if property. name == name {
74+ return Some ( property)
75+ }
76+ }
77+ None
78+ }
79+
80+ pub fn from_properties ( vec : Vec < Property > ) -> Properties {
81+ Self { properties : vec}
82+ }
83+ }
4784
4885#[ derive( PartialEq , Clone , Debug , Default ) ]
4986pub enum PropertyType {
@@ -108,7 +145,7 @@ pub struct Property {
108145 pub property_type : PropertyType ,
109146 pub facet : Option < String > ,
110147 pub size : Option < u32 > ,
111- pub children : Vec < Property > ,
148+ pub children : Properties ,
112149 pub key : Option < String > ,
113150 pub address : Option < String > ,
114151 pub encoding : Option < String > ,
@@ -146,7 +183,7 @@ pub struct ContinuationResponse {
146183pub struct EvalResponse {
147184 pub success : bool ,
148185 pub error : Option < DbgpError > ,
149- pub properties : Vec < Property > ,
186+ pub properties : Properties ,
150187}
151188
152189#[ derive( Debug , Clone ) ]
@@ -411,7 +448,7 @@ fn parse_source(element: &Element) -> Result<String, anyhow::Error> {
411448
412449
413450fn parse_context_get ( element : & mut Element ) -> Result < ContextGetResponse , anyhow:: Error > {
414- Ok ( ContextGetResponse { properties : parse_properties ( element) ?} )
451+ Ok ( ContextGetResponse { properties : Properties :: from_properties ( parse_properties ( element) ?) } )
415452}
416453
417454fn parse_eval ( element : & mut Element ) -> Result < EvalResponse , anyhow:: Error > {
@@ -433,7 +470,7 @@ fn parse_eval(element: &mut Element) -> Result<EvalResponse, anyhow::Error> {
433470 None
434471 } ;
435472
436- Ok ( EvalResponse { success : true , properties : parse_properties ( element) ?, error } )
473+ Ok ( EvalResponse { success : true , properties : Properties :: from_properties ( parse_properties ( element) ?) , error} )
437474}
438475
439476fn parse_properties ( element : & mut Element ) -> Result < Vec < Property > , anyhow:: Error > {
@@ -481,7 +518,7 @@ fn parse_properties(element: &mut Element) -> Result<Vec<Property>, anyhow::Erro
481518 key : child. attributes . get ( "key" ) . map ( |name| name. to_string ( ) ) ,
482519 address : child. attributes . get ( "address" ) . map ( |name| name. to_string ( ) ) ,
483520 encoding : encoding. clone ( ) ,
484- children : parse_properties ( & mut child) ?,
521+ children : Properties :: from_properties ( parse_properties ( & mut child) ?) ,
485522 value : decode_element ( Some ( & child) ) ,
486523 } ;
487524 properties. push ( p) ;
@@ -669,7 +706,7 @@ function call_function(string $hello) {
669706 let expected = EvalResponse {
670707 success : true ,
671708 error : None ,
672- properties : vec ! [
709+ properties : Properties :: from_properties ( vec ! [
673710 Property {
674711 name: "" . to_string( ) ,
675712 fullname: "" . to_string( ) ,
@@ -679,13 +716,13 @@ function call_function(string $hello) {
679716 property_type: PropertyType :: Int ,
680717 facet: None ,
681718 size: None ,
682- children: vec! [ ] ,
719+ children: Properties :: none ( ) ,
683720 key: None ,
684721 address: None ,
685722 encoding: None ,
686723 value: Some ( 2 . to_string( ) ) ,
687724 } ,
688- ] ,
725+ ] ) ,
689726 } ;
690727 assert_eq ! ( expected, response)
691728 }
@@ -715,7 +752,7 @@ function call_function(string $hello) {
715752 message : "error evaluating code: Undefined constant \" asda\" " . to_string ( ) ,
716753 code : "206" . to_string ( )
717754 } ) ,
718- properties : vec ! [ ] ,
755+ properties : Properties :: none ( ) ,
719756 } ;
720757 assert_eq ! ( expected, response)
721758 }
@@ -750,7 +787,7 @@ function call_function(string $hello) {
750787 match r. command {
751788 CommandResponse :: ContextGet ( response) => {
752789 let expected = ContextGetResponse {
753- properties : vec ! [
790+ properties : Properties :: from_properties ( vec ! [
754791 Property {
755792 name: "$bar" . to_string( ) ,
756793 fullname: "$bar" . to_string( ) ,
@@ -760,7 +797,7 @@ function call_function(string $hello) {
760797 property_type: PropertyType :: String ,
761798 facet: None ,
762799 size: Some ( 3 ) ,
763- children: vec! [ ] ,
800+ children: Properties :: none ( ) ,
764801 key: None ,
765802 address: None ,
766803 encoding: Some ( "base64" . to_string( ) ) ,
@@ -775,7 +812,7 @@ function call_function(string $hello) {
775812 property_type: PropertyType :: Float ,
776813 facet: None ,
777814 size: None ,
778- children: vec! [ ] ,
815+ children: Properties :: none ( ) ,
779816 key: None ,
780817 address: None ,
781818 encoding: None ,
@@ -790,7 +827,7 @@ function call_function(string $hello) {
790827 property_type: PropertyType :: Int ,
791828 facet: None ,
792829 size: None ,
793- children: vec! [ ] ,
830+ children: Properties :: none ( ) ,
794831 key: None ,
795832 address: None ,
796833 encoding: None ,
@@ -805,7 +842,7 @@ function call_function(string $hello) {
805842 property_type: PropertyType :: Bool ,
806843 facet: None ,
807844 size: None ,
808- children: vec! [ ] ,
845+ children: Properties :: none ( ) ,
809846 key: None ,
810847 address: None ,
811848 encoding: None ,
@@ -820,7 +857,7 @@ function call_function(string $hello) {
820857 property_type: PropertyType :: Object ,
821858 facet: None ,
822859 size: None ,
823- children: vec![
860+ children: Properties :: from_properties ( vec![
824861 Property {
825862 name: "true" . to_string( ) ,
826863 fullname: "$this->true" . to_string( ) ,
@@ -830,7 +867,7 @@ function call_function(string $hello) {
830867 property_type: PropertyType :: Bool ,
831868 facet: Some ( "public" . to_string( ) ) ,
832869 size: None ,
833- children: vec! [ ] ,
870+ children: Properties :: none ( ) ,
834871 key: None ,
835872 address: None ,
836873 encoding: None ,
@@ -845,7 +882,7 @@ function call_function(string $hello) {
845882 property_type: PropertyType :: String ,
846883 facet: Some ( "public" . to_string( ) ) ,
847884 size: Some ( 3 ) ,
848- children: vec! [ ] ,
885+ children: Properties :: none ( ) ,
849886 key: None ,
850887 address: None ,
851888 encoding: Some ( "base64" . to_string( ) ) ,
@@ -860,13 +897,13 @@ function call_function(string $hello) {
860897 property_type: PropertyType :: Resource ,
861898 facet: Some ( "private" . to_string( ) ) ,
862899 size: None ,
863- children: vec! [ ] ,
900+ children: Properties :: none ( ) ,
864901 key: None ,
865902 address: None ,
866903 encoding: None ,
867904 value: Some ( "resource id='18' type='stream'" . to_string( ) ) ,
868905 } ,
869- ] ,
906+ ] ) ,
870907 key: None ,
871908 address: None ,
872909 encoding: None ,
@@ -881,14 +918,14 @@ function call_function(string $hello) {
881918 property_type: PropertyType :: Array ,
882919 facet: None ,
883920 size: None ,
884- children: vec! [ ] ,
921+ children: Properties :: none ( ) ,
885922 key: None ,
886923 address: None ,
887924 encoding: None ,
888925 value: None ,
889926 } ,
890- ] ,
891- } ;
927+ ] ) ,
928+ } ;
892929 assert_eq ! ( expected, response)
893930 }
894931 _ => panic ! ( "Could not parse context_get" ) ,
0 commit comments