@@ -6,6 +6,8 @@ use crate::{
6
6
impl Protocol {
7
7
/// The `protocol.allow` key.
8
8
pub const ALLOW : Allow = Allow :: new_with_validate ( "allow" , & config:: Tree :: PROTOCOL , validate:: Allow ) ;
9
+ /// The `protocol.version` key.
10
+ pub const VERSION : Version = Version :: new_with_validate ( "version" , & config:: Tree :: PROTOCOL , validate:: Version ) ;
9
11
10
12
/// The `protocol.<name>` subsection
11
13
pub const NAME_PARAMETER : NameParameter = NameParameter ;
@@ -14,6 +16,9 @@ impl Protocol {
14
16
/// The `protocol.allow` key type.
15
17
pub type Allow = keys:: Any < validate:: Allow > ;
16
18
19
+ /// The `protocol.version` key.
20
+ pub type Version = keys:: Any < validate:: Version > ;
21
+
17
22
#[ cfg( any( feature = "blocking-network-client" , feature = "async-network-client" ) ) ]
18
23
mod allow {
19
24
use std:: borrow:: Cow ;
@@ -39,7 +44,7 @@ mod allow {
39
44
pub struct NameParameter ;
40
45
41
46
impl NameParameter {
42
- /// The `credential.<url>.helper ` key.
47
+ /// The `protocol.<name>.allow ` key.
43
48
pub const ALLOW : Allow = Allow :: new_with_validate ( "allow" , & Protocol :: NAME_PARAMETER , validate:: Allow ) ;
44
49
}
45
50
@@ -63,14 +68,46 @@ impl Section for Protocol {
63
68
}
64
69
65
70
fn keys ( & self ) -> & [ & dyn Key ] {
66
- & [ & Self :: ALLOW ]
71
+ & [ & Self :: ALLOW , & Self :: VERSION ]
67
72
}
68
73
69
74
fn sub_sections ( & self ) -> & [ & dyn Section ] {
70
75
& [ & Self :: NAME_PARAMETER ]
71
76
}
72
77
}
73
78
79
+ mod key_impls {
80
+ impl super :: Version {
81
+ /// Convert `value` into the corresponding protocol version, possibly applying the correct default.
82
+ #[ cfg( any( feature = "blocking-network-client" , feature = "async-network-client" ) ) ]
83
+ pub fn try_into_protocol_version (
84
+ & ' static self ,
85
+ value : Option < Result < i64 , gix_config:: value:: Error > > ,
86
+ ) -> Result < gix_protocol:: transport:: Protocol , crate :: config:: key:: GenericErrorWithValue > {
87
+ let value = match value {
88
+ None => return Ok ( gix_protocol:: transport:: Protocol :: V2 ) ,
89
+ Some ( v) => v,
90
+ } ;
91
+ Ok ( match value {
92
+ Ok ( 0 ) => gix_protocol:: transport:: Protocol :: V0 ,
93
+ Ok ( 1 ) => gix_protocol:: transport:: Protocol :: V1 ,
94
+ Ok ( 2 ) => gix_protocol:: transport:: Protocol :: V2 ,
95
+ Ok ( other) => {
96
+ return Err ( crate :: config:: key:: GenericErrorWithValue :: from_value (
97
+ self ,
98
+ other. to_string ( ) . into ( ) ,
99
+ ) )
100
+ }
101
+ Err ( err) => {
102
+ return Err (
103
+ crate :: config:: key:: GenericErrorWithValue :: from_value ( self , "unknown" . into ( ) ) . with_source ( err) ,
104
+ )
105
+ }
106
+ } )
107
+ }
108
+ }
109
+ }
110
+
74
111
mod validate {
75
112
use crate :: { bstr:: BStr , config:: tree:: keys} ;
76
113
@@ -82,4 +119,17 @@ mod validate {
82
119
Ok ( ( ) )
83
120
}
84
121
}
122
+
123
+ pub struct Version ;
124
+ impl keys:: Validate for Version {
125
+ fn validate ( & self , value : & BStr ) -> Result < ( ) , Box < dyn std:: error:: Error + Send + Sync + ' static > > {
126
+ let value = gix_config:: Integer :: try_from ( value) ?
127
+ . to_decimal ( )
128
+ . ok_or_else ( || format ! ( "integer {value} cannot be represented as integer" ) ) ?;
129
+ match value {
130
+ 0 | 1 | 2 => Ok ( ( ) ) ,
131
+ _ => Err ( format ! ( "protocol version {value} is unknown" ) . into ( ) ) ,
132
+ }
133
+ }
134
+ }
85
135
}
0 commit comments