style: fix clippy collapsible_if warnings in nvim-core

This commit is contained in:
Riz Ashraf committed 2026-09-22 06:07:50 +01:00
1 parent 148025c5a7
commit b2f15de58c
8 files changed
+105 -89

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
#![cfg(unix)] #![cfg(unix)]
use serde_json::{json, Value}; use serde_json::{Value, json};
use std::io::{BufRead, BufReader, Read, Write}; use std::io::{BufRead, BufReader, Read, Write};
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
+92 -74
View File
@@ -1,5 +1,5 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::{json, Value}; use serde_json::{Value, json};
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
@@ -133,10 +133,10 @@ static NVIM_CONN: LazyLock<Arc<std::sync::Mutex<Option<mpsc::Sender<NvimRequest>
async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> { async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
{ {
let conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner()); let conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner());
if let Some(sender) = conn_lock.as_ref() { if let Some(sender) = conn_lock.as_ref()
if !sender.is_closed() { && !sender.is_closed()
return Ok(sender.clone()); {
} return Ok(sender.clone());
} }
} }
@@ -200,20 +200,21 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
Ok(val) => { Ok(val) => {
offset += cursor.position() as usize; offset += cursor.position() as usize;
if let rmpv::Value::Array(ref arr) = val { if let rmpv::Value::Array(ref arr) = val
if arr.len() >= 4 && arr[0] == rmpv::Value::Integer(1.into()) { && arr.len() >= 4
let msgid = match &arr[1] { && arr[0] == rmpv::Value::Integer(1.into())
rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0), {
_ => 0, let msgid = match &arr[1] {
}; rmpv::Value::Integer(i) => i.as_u64().unwrap_or(0),
_ => 0,
};
if let Some(reply_sender) = pending_clone2 if let Some(reply_sender) = pending_clone2
.lock() .lock()
.unwrap_or_else(|e| e.into_inner()) .unwrap_or_else(|e| e.into_inner())
.remove(&msgid) .remove(&msgid)
{ {
let _ = reply_sender.send(Ok(val)); let _ = reply_sender.send(Ok(val));
}
} }
} }
if offset == resp_buf.len() { if offset == resp_buf.len() {
@@ -225,11 +226,17 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
} }
continue; continue;
} }
Err(e) if match &e { Err(e)
rmpv::decode::Error::InvalidMarkerRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof, if match &e {
rmpv::decode::Error::InvalidDataRead(io_err) => io_err.kind() == std::io::ErrorKind::UnexpectedEof, rmpv::decode::Error::InvalidMarkerRead(io_err) => {
_ => false, io_err.kind() == std::io::ErrorKind::UnexpectedEof
} => { }
rmpv::decode::Error::InvalidDataRead(io_err) => {
io_err.kind() == std::io::ErrorKind::UnexpectedEof
}
_ => false,
} =>
{
resp_buf.drain(..offset); resp_buf.drain(..offset);
offset = 0; offset = 0;
@@ -277,11 +284,11 @@ async fn get_nvim_connection() -> Result<mpsc::Sender<NvimRequest>, String> {
}); });
let mut conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner()); let mut conn_lock = NVIM_CONN.lock().unwrap_or_else(|e| e.into_inner());
if let Some(existing_sender) = conn_lock.as_ref() { if let Some(existing_sender) = conn_lock.as_ref()
if !existing_sender.is_closed() { && !existing_sender.is_closed()
// Another task established the connection while we were waiting {
return Ok(existing_sender.clone()); // Another task established the connection while we were waiting
} return Ok(existing_sender.clone());
} }
*conn_lock = Some(tx.clone()); *conn_lock = Some(tx.clone());
Ok(tx) Ok(tx)
@@ -348,21 +355,25 @@ async fn send_nvim_command(cmd: &str) -> Result<(), String> {
} }
async fn get_nvim_active_buffer() -> Result<String, String> { async fn get_nvim_active_buffer() -> Result<String, String> {
let result = call_nvim_method("nvim_buf_get_lines", vec![ let result = call_nvim_method(
rmpv::Value::Integer(0.into()), "nvim_buf_get_lines",
rmpv::Value::Integer(0.into()), vec![
rmpv::Value::Integer((-1).into()), rmpv::Value::Integer(0.into()),
rmpv::Value::Boolean(true), rmpv::Value::Integer(0.into()),
]).await?; rmpv::Value::Integer((-1).into()),
rmpv::Value::Boolean(true),
],
)
.await?;
if let rmpv::Value::Array(lines) = result { if let rmpv::Value::Array(lines) = result {
let mut text = String::new(); let mut text = String::new();
for line in lines { for line in lines {
if let rmpv::Value::String(s) = line { if let rmpv::Value::String(s) = line
if let Some(s) = s.as_str() { && let Some(s) = s.as_str()
text.push_str(s); {
text.push('\n'); text.push_str(s);
} text.push('\n');
} }
} }
return Ok(text); return Ok(text);
@@ -371,14 +382,14 @@ async fn get_nvim_active_buffer() -> Result<String, String> {
} }
async fn get_nvim_cursor() -> Result<String, String> { async fn get_nvim_cursor() -> Result<String, String> {
let result = call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?; let result =
call_nvim_method("nvim_win_get_cursor", vec![rmpv::Value::Integer(0.into())]).await?;
if let rmpv::Value::Array(pos) = result {
if pos.len() == 2 { if let rmpv::Value::Array(pos) = result
if let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1]) { && pos.len() == 2
return Ok(format!("Line: {row}, Column: {col}")); && let (rmpv::Value::Integer(row), rmpv::Value::Integer(col)) = (&pos[0], &pos[1])
} {
} return Ok(format!("Line: {row}, Column: {col}"));
} }
Err("Invalid response format".to_string()) Err("Invalid response format".to_string())
} }
@@ -395,15 +406,19 @@ async fn get_nvim_visual_selection() -> Result<String, String> {
end end
"#; "#;
let result = call_nvim_method("nvim_exec_lua", vec![ let result = call_nvim_method(
rmpv::Value::String(lua_script.into()), "nvim_exec_lua",
rmpv::Value::Array(vec![]), vec![
]).await?; rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
if let rmpv::Value::String(s) = result { ],
if let Some(text) = s.as_str() { )
return Ok(text.to_string()); .await?;
}
if let rmpv::Value::String(s) = result
&& let Some(text) = s.as_str()
{
return Ok(text.to_string());
} }
Err("Invalid response format".to_string()) Err("Invalid response format".to_string())
} }
@@ -423,11 +438,15 @@ async fn set_nvim_diagnostics(line: i64, message: &str) -> Result<(), String> {
"# "#
); );
call_nvim_method("nvim_exec_lua", vec![ call_nvim_method(
rmpv::Value::String(lua_script.into()), "nvim_exec_lua",
rmpv::Value::Array(vec![]), vec![
]).await?; rmpv::Value::String(lua_script.into()),
rmpv::Value::Array(vec![]),
],
)
.await?;
Ok(()) Ok(())
} }
@@ -489,22 +508,21 @@ fn json_to_rmpv(val: &serde_json::Value) -> rmpv::Value {
} }
} }
serde_json::Value::String(s) => rmpv::Value::String(s.as_str().into()), serde_json::Value::String(s) => rmpv::Value::String(s.as_str().into()),
serde_json::Value::Array(arr) => { serde_json::Value::Array(arr) => rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect()),
rmpv::Value::Array(arr.iter().map(json_to_rmpv).collect()) serde_json::Value::Object(obj) => rmpv::Value::Map(
} obj.iter()
serde_json::Value::Object(obj) => { .map(|(k, v)| (rmpv::Value::String(k.as_str().into()), json_to_rmpv(v)))
rmpv::Value::Map(obj.iter().map(|(k, v)| { .collect(),
(rmpv::Value::String(k.as_str().into()), json_to_rmpv(v)) ),
}).collect())
}
} }
} }
async fn execute_nvim_lua_with_args(code: &str, args: Vec<rmpv::Value>) -> Result<String, String> { async fn execute_nvim_lua_with_args(code: &str, args: Vec<rmpv::Value>) -> Result<String, String> {
let result = call_nvim_method("nvim_exec_lua", vec![ let result = call_nvim_method(
rmpv::Value::String(code.into()), "nvim_exec_lua",
rmpv::Value::Array(args), vec![rmpv::Value::String(code.into()), rmpv::Value::Array(args)],
]).await?; )
.await?;
let json_res = rmpv_to_json(&result); let json_res = rmpv_to_json(&result);
// If it's a simple string, return it unquoted, otherwise format it pretty // If it's a simple string, return it unquoted, otherwise format it pretty
if let serde_json::Value::String(s) = json_res { if let serde_json::Value::String(s) = json_res {
+4 -2
View File
@@ -68,7 +68,9 @@ pub async fn gate_verify_handler(
Err(AppError::Forbidden(msg)) Err(AppError::Forbidden(msg))
} }
} }
None => Err(AppError::NotFound("Action not yet authorized (no gate record found).".to_string())), None => Err(AppError::NotFound(
"Action not yet authorized (no gate record found).".to_string(),
)),
} }
} }
@@ -98,7 +100,7 @@ pub async fn gate_set_handler(
gates.retain(|g| !(g.action == record.action && g.target == record.target)); gates.retain(|g| !(g.action == record.action && g.target == record.target));
gates.push(record); gates.push(record);
}); });
Ok((axum::http::StatusCode::OK, "Gate state updated.")) Ok((axum::http::StatusCode::OK, "Gate state updated."))
} }
+3 -6
View File
@@ -1,7 +1,7 @@
use crate::store::STORE_TABLE;
use redb::{Database, ReadableTable};
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use redb::{Database, ReadableTable};
use crate::store::STORE_TABLE;
pub fn init_redb(base: &Path) -> Arc<Database> { pub fn init_redb(base: &Path) -> Arc<Database> {
let redb_path = base.join("mcp_store.redb"); let redb_path = base.join("mcp_store.redb");
@@ -50,10 +50,7 @@ pub fn init_redb(base: &Path) -> Arc<Database> {
table table
.insert(*key, data.as_slice()) .insert(*key, data.as_slice())
.expect("Failed to insert migrated data"); .expect("Failed to insert migrated data");
let _ = std::fs::rename( let _ = std::fs::rename(&json_path, json_path.with_extension("json.migrated"));
&json_path,
json_path.with_extension("json.migrated"),
);
} }
} }
} }
+1 -1
View File
@@ -1,7 +1,7 @@
use axum::{ use axum::{
Json,
http::StatusCode, http::StatusCode,
response::{IntoResponse, Response}, response::{IntoResponse, Response},
Json,
}; };
use serde_json::json; use serde_json::json;
use thiserror::Error; use thiserror::Error;
+1 -2
View File
@@ -244,8 +244,7 @@ impl McpTool for GenerateStandupReportHandler {
async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> { async fn execute(&self, args: Value, state: Arc<MemoryState>) -> Result<String, String> {
let req: GenerateStandupReportTool = let req: GenerateStandupReportTool =
serde_json::from_value(args).map_err(|e| e.to_string())?; serde_json::from_value(args).map_err(|e| e.to_string())?;
let cutoff = let cutoff = crate::handlers::utils::now_secs().saturating_sub(req.hours_lookback * 3600);
crate::handlers::utils::now_secs().saturating_sub(req.hours_lookback * 3600);
let report_str = state.tasks.read_with(|items| { let report_str = state.tasks.read_with(|items| {
state.ledger.read_with(|changes| { state.ledger.read_with(|changes| {
+2 -2
View File
@@ -4,13 +4,13 @@
)] )]
mod api; mod api;
pub mod db;
pub mod error;
mod handlers; mod handlers;
mod mcp; mod mcp;
mod models; mod models;
mod router; mod router;
mod search; mod search;
pub mod db;
pub mod error;
mod state; mod state;
mod store; mod store;
mod tools; mod tools;
+1 -1
View File
@@ -1,4 +1,4 @@
use serde_json::{json, Value}; use serde_json::{Value, json};
use std::io::{BufRead, BufReader, Write}; use std::io::{BufRead, BufReader, Write};
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};