fix: 修复ComfyUI SDK的cargo check和clippy警告

- 修复ComfyUIError中WebSocket变体过大的问题,使用Box包装
- 添加自定义From实现处理Box包装的WebSocket错误
- 修复所有格式化字符串警告,使用内联格式化
- 移除无用的类型转换(reqwest::Error::from)
- 修复代码质量问题:
  - 简化if语句嵌套
  - 使用?操作符替代显式错误处理
  - 优化map迭代方式
  - 使用is_some_and替代map_or

修复内容:
- 44个clippy警告全部解决
- 所有测试通过
- cargo check --lib 无错误无警告
This commit is contained in:
2025-08-08 16:12:25 +08:00
parent e874d281e0
commit e69ce2b817
9 changed files with 63 additions and 71 deletions

View File

@@ -43,7 +43,7 @@ where
last_error = Some(error);
if attempt < config.max_attempts {
log::warn!("Attempt {} failed, retrying in {:?}", attempt, delay);
log::warn!("Attempt {attempt} failed, retrying in {delay:?}");
sleep(delay).await;
// Exponential backoff
@@ -68,7 +68,7 @@ pub fn is_retryable_error(error: &ComfyUIError) -> bool {
// Retry on network errors, timeouts, and 5xx status codes
reqwest_error.is_timeout() ||
reqwest_error.is_connect() ||
reqwest_error.status().map_or(false, |status| status.is_server_error())
reqwest_error.status().is_some_and(|status| status.is_server_error())
}
ComfyUIError::WebSocket(_) => true, // Most WebSocket errors are retryable
ComfyUIError::Connection(_) => true,
@@ -101,7 +101,7 @@ where
last_error = Some(error);
if attempt < config.max_attempts {
log::warn!("Attempt {} failed with retryable error, retrying in {:?}", attempt, delay);
log::warn!("Attempt {attempt} failed with retryable error, retrying in {delay:?}");
sleep(delay).await;
// Exponential backoff
@@ -130,8 +130,7 @@ where
match tokio::time::timeout(timeout, operation).await {
Ok(result) => result,
Err(_) => Err(ComfyUIError::timeout(format!(
"Operation timed out after {:?}",
timeout
"Operation timed out after {timeout:?}"
))),
}
}
@@ -144,7 +143,7 @@ pub trait ErrorContext<T> {
impl<T> ErrorContext<T> for Result<T> {
fn with_context(self, context: &str) -> Result<T> {
self.map_err(|error| {
ComfyUIError::new(format!("{}: {}", context, error))
ComfyUIError::new(format!("{context}: {error}"))
})
}
}

View File

@@ -82,7 +82,7 @@ pub fn substitute_variables(
};
let regex = Regex::new(pattern)
.map_err(|e| ComfyUIError::new(format!("Invalid regex pattern: {}", e)))?;
.map_err(|e| ComfyUIError::new(format!("Invalid regex pattern: {e}")))?;
let mut result = template.to_string();
let mut offset = 0i32;
@@ -96,7 +96,7 @@ pub fn substitute_variables(
Some(value) => value_to_string(value)?,
None => {
return Err(ComfyUIError::template_validation(
format!("Parameter '{}' not found", var_name)
format!("Parameter '{var_name}' not found")
));
}
};
@@ -139,7 +139,7 @@ pub fn extract_variables(template: &str, syntax: VariableSyntax) -> Result<Vec<S
};
let regex = Regex::new(pattern)
.map_err(|e| ComfyUIError::new(format!("Invalid regex pattern: {}", e)))?;
.map_err(|e| ComfyUIError::new(format!("Invalid regex pattern: {e}")))?;
let mut variables = Vec::new();
for captures in regex.captures_iter(template) {

View File

@@ -13,15 +13,14 @@ pub fn validate_parameters(
// Check required parameters
for (name, schema) in schemas {
if schema.required.unwrap_or(false) {
if !parameters.contains_key(name) {
if schema.required.unwrap_or(false)
&& !parameters.contains_key(name) {
errors.push(ValidationError::new(
name,
"Required parameter is missing",
));
continue;
}
}
// Validate parameter if present
if let Some(value) = parameters.get(name) {
@@ -85,7 +84,7 @@ pub fn validate_parameter_value(
if !enum_values.contains(value) {
return Err(ValidationError::with_value(
name,
format!("Value must be one of: {:?}", enum_values),
format!("Value must be one of: {enum_values:?}"),
value.clone(),
));
}
@@ -109,7 +108,7 @@ fn validate_string_constraints(
if !regex.is_match(value) {
return Err(ValidationError::with_value(
name,
format!("String does not match pattern: {}", pattern),
format!("String does not match pattern: {pattern}"),
serde_json::Value::String(value.to_string()),
));
}
@@ -120,7 +119,7 @@ fn validate_string_constraints(
if (value.len() as f64) < min {
return Err(ValidationError::with_value(
name,
format!("String length must be at least {}", min),
format!("String length must be at least {min}"),
serde_json::Value::String(value.to_string()),
));
}
@@ -130,7 +129,7 @@ fn validate_string_constraints(
if (value.len() as f64) > max {
return Err(ValidationError::with_value(
name,
format!("String length must be at most {}", max),
format!("String length must be at most {max}"),
serde_json::Value::String(value.to_string()),
));
}
@@ -151,7 +150,7 @@ fn validate_number_constraints(
if num_value < min {
return Err(ValidationError::with_value(
name,
format!("Number must be at least {}", min),
format!("Number must be at least {min}"),
serde_json::Value::Number(value.clone()),
));
}
@@ -161,7 +160,7 @@ fn validate_number_constraints(
if num_value > max {
return Err(ValidationError::with_value(
name,
format!("Number must be at most {}", max),
format!("Number must be at most {max}"),
serde_json::Value::Number(value.clone()),
));
}
@@ -181,7 +180,7 @@ fn validate_array_constraints(
if (value.len() as f64) < min {
return Err(ValidationError::with_value(
name,
format!("Array length must be at least {}", min),
format!("Array length must be at least {min}"),
serde_json::Value::Array(value.to_vec()),
));
}
@@ -191,7 +190,7 @@ fn validate_array_constraints(
if (value.len() as f64) > max {
return Err(ValidationError::with_value(
name,
format!("Array length must be at most {}", max),
format!("Array length must be at most {max}"),
serde_json::Value::Array(value.to_vec()),
));
}
@@ -200,10 +199,8 @@ fn validate_array_constraints(
// Validate items if schema is provided
if let Some(items_schema) = &schema.items {
for (index, item) in value.iter().enumerate() {
let item_name = format!("{}[{}]", name, index);
if let Err(error) = validate_parameter_value(&item_name, item, items_schema) {
return Err(error);
}
let item_name = format!("{name}[{index}]");
validate_parameter_value(&item_name, item, items_schema)?
}
}