Skip to content

Commit 0ebbd2c

Browse files
fixup: Remove unused String fields from errors and use &'static str for messages
1 parent bf6e361 commit 0ebbd2c

File tree

1 file changed

+25
-65
lines changed
  • lightning-liquidity/src/lsps5

1 file changed

+25
-65
lines changed

lightning-liquidity/src/lsps5/msgs.rs

Lines changed: 25 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,15 @@ pub enum LSPS5ProtocolError {
8181
/// Webhook URL is not a valid URL.
8282
///
8383
/// Sent when the provided webhook URL cannot be parsed or is syntactically invalid.
84-
UrlParse(String),
84+
UrlParse,
8585

8686
/// Webhook URL does not use HTTPS.
8787
///
8888
/// The LSPS5 specification requires all webhook URLs to use HTTPS.
8989
UnsupportedProtocol,
9090

9191
/// Client has reached their maximum allowed number of webhooks.
92-
///
93-
/// The string contains the maximum number of webhooks allowed.
94-
TooManyWebhooks(String),
92+
TooManyWebhooks,
9593

9694
/// The specified app name was not found in the registered webhooks.
9795
///
@@ -103,37 +101,29 @@ pub enum LSPS5ProtocolError {
103101
}
104102

105103
impl LSPS5ProtocolError {
106-
/// private code range so we never collide with the specs codes
104+
/// private code range so we never collide with the spec's codes
107105
pub fn code(&self) -> i32 {
108106
match self {
109107
LSPS5ProtocolError::AppNameTooLong | LSPS5ProtocolError::WebhookUrlTooLong => {
110108
LSPS5_TOO_LONG_ERROR_CODE
111109
},
112-
LSPS5ProtocolError::UrlParse(_) => LSPS5_URL_PARSE_ERROR_CODE,
110+
LSPS5ProtocolError::UrlParse => LSPS5_URL_PARSE_ERROR_CODE,
113111
LSPS5ProtocolError::UnsupportedProtocol => LSPS5_UNSUPPORTED_PROTOCOL_ERROR_CODE,
114-
LSPS5ProtocolError::TooManyWebhooks { .. } => LSPS5_TOO_MANY_WEBHOOKS_ERROR_CODE,
112+
LSPS5ProtocolError::TooManyWebhooks => LSPS5_TOO_MANY_WEBHOOKS_ERROR_CODE,
115113
LSPS5ProtocolError::AppNameNotFound => LSPS5_APP_NAME_NOT_FOUND_ERROR_CODE,
116114
LSPS5ProtocolError::UnknownError => LSPS5_UNKNOWN_ERROR_CODE,
117115
}
118116
}
119117
/// The error message for the LSPS5 protocol error.
120-
pub fn message(&self) -> String {
118+
pub fn message(&self) -> &'static str {
121119
match self {
122-
LSPS5ProtocolError::AppNameTooLong => {
123-
format!("App name exceeds maximum length of {} bytes", MAX_APP_NAME_LENGTH)
124-
},
125-
LSPS5ProtocolError::WebhookUrlTooLong => {
126-
format!("Webhook URL exceeds maximum length of {} bytes", MAX_WEBHOOK_URL_LENGTH)
127-
},
128-
LSPS5ProtocolError::UrlParse(m) => m.clone(),
129-
LSPS5ProtocolError::UnsupportedProtocol => {
130-
"Unsupported protocol: HTTPS is required".to_string()
131-
},
132-
LSPS5ProtocolError::TooManyWebhooks(max) => {
133-
format!("Maximum of {} webhooks allowed per client", max)
134-
},
135-
LSPS5ProtocolError::AppNameNotFound => "App name not found".to_string(),
136-
LSPS5ProtocolError::UnknownError => "Unknown error".to_string(),
120+
LSPS5ProtocolError::AppNameTooLong => "App name exceeds maximum length",
121+
LSPS5ProtocolError::WebhookUrlTooLong => "Webhook URL exceeds maximum length",
122+
LSPS5ProtocolError::UrlParse => "Error parsing URL",
123+
LSPS5ProtocolError::UnsupportedProtocol => "Unsupported protocol: HTTPS is required",
124+
LSPS5ProtocolError::TooManyWebhooks => "Maximum number of webhooks allowed per client",
125+
LSPS5ProtocolError::AppNameNotFound => "App name not found",
126+
LSPS5ProtocolError::UnknownError => "Unknown error",
137127
}
138128
}
139129
}
@@ -143,14 +133,9 @@ impl Serialize for LSPS5ProtocolError {
143133
where
144134
S: Serializer,
145135
{
146-
let mut s = ser.serialize_struct("error", 3)?;
136+
let mut s = ser.serialize_struct("error", 2)?;
147137
s.serialize_field("code", &self.code())?;
148138
s.serialize_field("message", &self.message())?;
149-
let data = match self {
150-
LSPS5ProtocolError::TooManyWebhooks(max) => Some(max),
151-
_ => None,
152-
};
153-
s.serialize_field("data", &data)?;
154139
s.end()
155140
}
156141
}
@@ -170,18 +155,7 @@ pub enum LSPS5ClientError {
170155
/// Notification timestamp is too old or too far in the future.
171156
///
172157
/// LSPS5 requires timestamps to be within ±10 minutes of current time.
173-
/// The string contains the problematic timestamp.
174-
InvalidTimestamp(String),
175-
176-
/// Failed to serialize an object to JSON.
177-
///
178-
/// The string contains the detailed error message.
179-
SerializeError(String),
180-
181-
/// Failed to deserialize JSON into an expected object.
182-
///
183-
/// The string contains the detailed error message.
184-
DeserializeError(String),
158+
InvalidTimestamp,
185159

186160
/// Detected a reused notification signature.
187161
///
@@ -197,21 +171,17 @@ impl LSPS5ClientError {
197171
use LSPS5ClientError::*;
198172
match self {
199173
InvalidSignature => Self::BASE + 1,
200-
InvalidTimestamp(_) => Self::BASE + 2,
201-
SerializeError(_) => Self::BASE + 3,
202-
DeserializeError(_) => Self::BASE + 4,
203-
ReplayAttack => Self::BASE + 5,
174+
InvalidTimestamp => Self::BASE + 2,
175+
ReplayAttack => Self::BASE + 3,
204176
}
205177
}
206178
/// The error message for the client error.
207-
pub fn message(&self) -> String {
179+
pub fn message(&self) -> &'static str {
208180
use LSPS5ClientError::*;
209181
match self {
210-
InvalidSignature => "Invalid signature".into(),
211-
InvalidTimestamp(m) => format!("Timestamp out of range: {}", m),
212-
SerializeError(m) => format!("Serialization error: {}", m),
213-
DeserializeError(m) => format!("Deserialization error: {}", m),
214-
ReplayAttack => "Replay attack detected".into(),
182+
InvalidSignature => "Invalid signature",
183+
InvalidTimestamp => "Timestamp out of range",
184+
ReplayAttack => "Replay attack detected",
215185
}
216186
}
217187
}
@@ -256,12 +226,9 @@ impl From<LSPSResponseError> for LSPS5ProtocolError {
256226
fn from(err: LSPSResponseError) -> Self {
257227
match err.code {
258228
LSPS5_TOO_LONG_ERROR_CODE => LSPS5ProtocolError::AppNameTooLong,
259-
LSPS5_URL_PARSE_ERROR_CODE => LSPS5ProtocolError::UrlParse(err.message),
229+
LSPS5_URL_PARSE_ERROR_CODE => LSPS5ProtocolError::UrlParse,
260230
LSPS5_UNSUPPORTED_PROTOCOL_ERROR_CODE => LSPS5ProtocolError::UnsupportedProtocol,
261-
LSPS5_TOO_MANY_WEBHOOKS_ERROR_CODE => match err.data {
262-
Some(d) => LSPS5ProtocolError::TooManyWebhooks(d),
263-
None => LSPS5ProtocolError::UnknownError,
264-
},
231+
LSPS5_TOO_MANY_WEBHOOKS_ERROR_CODE => LSPS5ProtocolError::TooManyWebhooks,
265232
LSPS5_APP_NAME_NOT_FOUND_ERROR_CODE => LSPS5ProtocolError::AppNameNotFound,
266233
_ => LSPS5ProtocolError::UnknownError,
267234
}
@@ -270,14 +237,7 @@ impl From<LSPSResponseError> for LSPS5ProtocolError {
270237

271238
impl From<LSPS5ProtocolError> for LSPSResponseError {
272239
fn from(e: LSPS5ProtocolError) -> Self {
273-
LSPSResponseError {
274-
code: e.code(),
275-
message: e.message(),
276-
data: match e {
277-
LSPS5ProtocolError::TooManyWebhooks(max) => Some(max.to_string()),
278-
_ => None,
279-
},
280-
}
240+
LSPSResponseError { code: e.code(), message: e.message().into(), data: None }
281241
}
282242
}
283243

@@ -286,7 +246,7 @@ impl From<LSPS5Error> for LSPSResponseError {
286246
match e {
287247
LSPS5Error::Protocol(p) => p.into(),
288248
LSPS5Error::Client(c) => {
289-
LSPSResponseError { code: c.code(), message: c.message(), data: None }
249+
LSPSResponseError { code: c.code(), message: c.message().into(), data: None }
290250
},
291251
}
292252
}

0 commit comments

Comments
 (0)