Skip to content

Commit 38c3b84

Browse files
nepetcdecker
authored andcommitted
tramp: Switch to GreenlightError in gl-util
Signed-off-by: Peter Neuroth <[email protected]>
1 parent 181e4d9 commit 38c3b84

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

libs/gl-plugin/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cln-rpc = { workspace = true }
2323
env_logger = "^0.7.1"
2424
futures = "0.3"
2525
gl-client = { version = "^0.3.0", path = "../gl-client" }
26+
gl-util = { version = "0.1", path = "../gl-util" }
2627
governor = { version = "0.5", default-features = false, features = ["std"] }
2728
hex = "0.4"
2829
hyper = "0.14.28"

libs/gl-plugin/src/tramp.rs

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
189196
pub 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
200210
impl 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.
208217
type 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
219220
impl 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

Comments
 (0)