@@ -186,44 +186,56 @@ impl ErrorStatusConversionExt for TrampolineErrCode {
186186 }
187187}
188188
189+ // We need to respect rusts orphan rule which does not let us use a simple
190+ // type TrampolineError = GreenlightError<TrampolineErrCode>, as this would
191+ // not let us implement own methods on TrampolineError since it IS
192+ // GreenlightError and therefore not defined in this crate.
193+ // We can avoid to implement all these delegations of the trait methods and
194+ // the builder methods (furhter down) in the future by providing a proc
195+ // macro that derives all the boilerplate for us.
189196pub struct TrampolineError ( pub GreenlightError < TrampolineErrCode > ) ;
190197
191- impl core:: ops:: Deref for TrampolineError {
192- type Target = GreenlightError < TrampolineErrCode > ;
198+ impl core:: fmt:: Display for TrampolineError {
199+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
200+ self . 0 . fmt ( f)
201+ }
202+ }
193203
194- fn deref ( & self ) -> & Self :: Target {
195- & self . 0
204+ impl From < TrampolineError > for tonic:: Status {
205+ fn from ( value : TrampolineError ) -> Self {
206+ tonic:: Status :: from ( value. 0 )
196207 }
197208}
198209
199- // Implement From for easy conversion
200210impl From < GreenlightError < TrampolineErrCode > > for TrampolineError {
201211 fn from ( err : GreenlightError < TrampolineErrCode > ) -> Self {
202212 TrampolineError ( err)
203213 }
204214}
205215
206- /// Type aliases for convenience.
207- // pub type TrampolineError = GreenlightError<TrampolineErrCode>;
216+ /// Type alias for convenience.
208217type Result < T , E = TrampolineError > = core:: result:: Result < T , E > ;
209218
210- /// Needed to bypass rusts orphan rule.
211- // trait TrampolineErrorExt {
212- // fn feature_not_supported(features: impl Into<String>) -> Self;
213- // fn invalid_node_id(source: impl StdError + Send + Sync + 'static) -> Self;
214- // fn network(reason: impl Into<String>) -> Self;
215- // fn internal(message: impl Into<String>) -> Self;
216- // }
217-
218219/// TrampolineError Convenience Constructors
219220impl TrampolineError {
221+ // Delegate builder methods, wrapping the result
220222 fn new ( code : TrampolineErrCode , message : impl Into < String > ) -> Self {
221223 TrampolineError ( GreenlightError :: new ( code, message) )
222224 }
223225
224- // impl TrampolineErrorExt for TrampolineError {
225- /// Creates an error indicating that the peer doesn't support required
226- /// trampoline features.
226+ pub fn with_hint ( self , hint : impl Into < String > ) -> Self {
227+ TrampolineError ( self . 0 . with_hint ( hint) )
228+ }
229+
230+ pub fn with_context ( self , context : impl Into < String > ) -> Self {
231+ TrampolineError ( self . 0 . with_context ( context) )
232+ }
233+
234+ pub fn with_source ( self , source : impl StdError + Send + Sync + ' static ) -> Self {
235+ TrampolineError ( self . 0 . with_source ( source) )
236+ }
237+
238+ // Implement convenince constructors.
227239 fn feature_not_supported ( features : impl Into < String > ) -> Self {
228240 Self :: new (
229241 TrampolineErrCode :: FeatureNotSupported ,
0 commit comments