@@ -69,6 +69,23 @@ pub enum Value {
6969 List ( Vec < Self > ) ,
7070}
7171
72+ impl FromValue for Value {
73+ fn from_value ( v : Value ) -> ClientResult < Self > {
74+ Ok ( v)
75+ }
76+ }
77+
78+ impl Value {
79+ /// Attempt to parse this value into a different type
80+ pub fn parse < T : FromValue > ( self ) -> ClientResult < T > {
81+ T :: from_value ( self )
82+ }
83+ /// Attempt to parse this value into a different type, by cloning the value first
84+ pub fn parse_cloned < T : FromValue > ( & self ) -> ClientResult < T > {
85+ T :: from_value ( self . clone ( ) )
86+ }
87+ }
88+
7289#[ derive( Debug , PartialEq , Clone ) ]
7390/// A row returned by the server
7491pub struct Row {
@@ -87,6 +104,18 @@ impl Row {
87104 pub fn into_values ( self ) -> Vec < Value > {
88105 self . values
89106 }
107+ /// Returns the first [`Value`] in the [`Row`] if present
108+ pub fn into_first ( mut self ) -> ClientResult < Value > {
109+ if self . values . is_empty ( ) {
110+ Err ( Error :: ParseError ( ParseError :: ResponseMismatch ) )
111+ } else {
112+ Ok ( self . values . remove ( 0 ) )
113+ }
114+ }
115+ /// Returns the first [`Value`] in the [`Row`] if present, as the given type
116+ pub fn into_first_as < T : FromValue > ( self ) -> ClientResult < T > {
117+ self . into_first ( ) . and_then ( FromValue :: from_value)
118+ }
90119}
91120
92121#[ derive( Debug , PartialEq , Clone ) ]
@@ -141,6 +170,7 @@ pub enum Response {
141170/// assert_eq!(myuser.username, "bob");
142171/// ```
143172pub trait FromResponse : Sized {
173+ /// Decode the target type from the [`Response`]
144174 fn from_response ( resp : Response ) -> ClientResult < Self > ;
145175}
146176
@@ -268,3 +298,12 @@ from_response_row!(
268298 ( A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y ) as 25 ,
269299 ( A , B , C , D , E , F , G , H , I , J , K , L , M , N , O , P , Q , R , S , T , U , V , W , X , Y , Z ) as 26 ,
270300) ;
301+
302+ impl FromResponse for Row {
303+ fn from_response ( resp : Response ) -> ClientResult < Self > {
304+ match resp {
305+ Response :: Row ( r) => Ok ( r) ,
306+ _ => Err ( Error :: ParseError ( ParseError :: ResponseMismatch ) ) ,
307+ }
308+ }
309+ }
0 commit comments