style: 应用 Rust 格式化
This commit is contained in:
1 parent
67a7ef4746
commit
5b62f936d1
6 files changed
+38
-47
No files matched your search
@@ -364,7 +364,9 @@ async fn safe_query(args: &[String]) -> CliEnvelope<serde_json::Value> {
|
|||||||
};
|
};
|
||||||
let database = option_value(args, "--db").or(config.database.as_deref()).unwrap_or("");
|
let database = option_value(args, "--db").or(config.database.as_deref()).unwrap_or("");
|
||||||
|
|
||||||
match dbx_core::query::execute_sql_statement_with_row_limit(&state, &config.id, database, sql, None, None, limit).await {
|
match dbx_core::query::execute_sql_statement_with_row_limit(&state, &config.id, database, sql, None, None, limit)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(result) => ok(
|
Ok(result) => ok(
|
||||||
CliSource::Headless,
|
CliSource::Headless,
|
||||||
serde_json::json!({
|
serde_json::json!({
|
||||||
@@ -593,16 +595,19 @@ mod tests {
|
|||||||
async fn create_large_sqlite_fixture(path: &std::path::Path, row_count: usize) {
|
async fn create_large_sqlite_fixture(path: &std::path::Path, row_count: usize) {
|
||||||
std::fs::File::create(path).unwrap();
|
std::fs::File::create(path).unwrap();
|
||||||
let pool = dbx_core::db::sqlite::connect_path(&path.display().to_string()).await.unwrap();
|
let pool = dbx_core::db::sqlite::connect_path(&path.display().to_string()).await.unwrap();
|
||||||
dbx_core::db::sqlite::execute_query(&pool, "CREATE TABLE numbers (id INTEGER PRIMARY KEY, value TEXT NOT NULL)")
|
dbx_core::db::sqlite::execute_query(
|
||||||
.await
|
&pool,
|
||||||
.unwrap();
|
"CREATE TABLE numbers (id INTEGER PRIMARY KEY, value TEXT NOT NULL)",
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
for id in 1..=row_count {
|
for id in 1..=row_count {
|
||||||
dbx_core::db::sqlite::execute_query(
|
dbx_core::db::sqlite::execute_query(
|
||||||
&pool,
|
&pool,
|
||||||
&format!("INSERT INTO numbers (id, value) VALUES ({id}, 'value-{id}')"),
|
&format!("INSERT INTO numbers (id, value) VALUES ({id}, 'value-{id}')"),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
pool.close().await;
|
pool.close().await;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,10 +4,7 @@ mod runtime_client;
|
|||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
if let Err(err) = commands::run(std::env::args().skip(1).collect()).await {
|
if let Err(err) = commands::run(std::env::args().skip(1).collect()).await {
|
||||||
println!(
|
println!("{}", serde_json::to_string_pretty(&err).unwrap_or_else(|_| "{\"ok\":false}".to_string()));
|
||||||
"{}",
|
|
||||||
serde_json::to_string_pretty(&err).unwrap_or_else(|_| "{\"ok\":false}".to_string())
|
|
||||||
);
|
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,8 +17,7 @@ pub fn app_data_dir() -> PathBuf {
|
|||||||
return PathBuf::from(path);
|
return PathBuf::from(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
let home = std::env::var(if cfg!(windows) { "APPDATA" } else { "HOME" })
|
let home = std::env::var(if cfg!(windows) { "APPDATA" } else { "HOME" }).unwrap_or_else(|_| ".".to_string());
|
||||||
.unwrap_or_else(|_| ".".to_string());
|
|
||||||
|
|
||||||
if cfg!(target_os = "macos") {
|
if cfg!(target_os = "macos") {
|
||||||
PathBuf::from(home).join("Library/Application Support/com.dbx.app")
|
PathBuf::from(home).join("Library/Application Support/com.dbx.app")
|
||||||
@@ -43,44 +42,30 @@ pub async fn get_json(path: &str) -> Result<serde_json::Value, String> {
|
|||||||
let runtime = load_runtime().ok_or_else(|| "runtime unavailable".to_string())?;
|
let runtime = load_runtime().ok_or_else(|| "runtime unavailable".to_string())?;
|
||||||
let url = runtime_url(&runtime, path, &[])?;
|
let url = runtime_url(&runtime, path, &[])?;
|
||||||
|
|
||||||
let response = reqwest::Client::new()
|
let response =
|
||||||
.get(url)
|
reqwest::Client::new().get(url).bearer_auth(runtime.token).send().await.map_err(|err| err.to_string())?;
|
||||||
.bearer_auth(runtime.token)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(|err| err.to_string())?;
|
|
||||||
|
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
return Err(format!("runtime request failed with status {status}"));
|
return Err(format!("runtime request failed with status {status}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
response
|
response.json().await.map_err(|err| err.to_string())
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|err| err.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_json_with_query(path: &str, query: &[(&str, String)]) -> Result<serde_json::Value, String> {
|
pub async fn get_json_with_query(path: &str, query: &[(&str, String)]) -> Result<serde_json::Value, String> {
|
||||||
let runtime = load_runtime().ok_or_else(|| "runtime unavailable".to_string())?;
|
let runtime = load_runtime().ok_or_else(|| "runtime unavailable".to_string())?;
|
||||||
let url = runtime_url(&runtime, path, query)?;
|
let url = runtime_url(&runtime, path, query)?;
|
||||||
|
|
||||||
let response = reqwest::Client::new()
|
let response =
|
||||||
.get(url)
|
reqwest::Client::new().get(url).bearer_auth(runtime.token).send().await.map_err(|err| err.to_string())?;
|
||||||
.bearer_auth(runtime.token)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
.map_err(|err| err.to_string())?;
|
|
||||||
|
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
if !status.is_success() {
|
if !status.is_success() {
|
||||||
return Err(format!("runtime request failed with status {status}"));
|
return Err(format!("runtime request failed with status {status}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
response
|
response.json().await.map_err(|err| err.to_string())
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|err| err.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn post_json(path: &str, body: serde_json::Value) -> Result<serde_json::Value, String> {
|
pub async fn post_json(path: &str, body: serde_json::Value) -> Result<serde_json::Value, String> {
|
||||||
@@ -100,10 +85,7 @@ pub async fn post_json(path: &str, body: serde_json::Value) -> Result<serde_json
|
|||||||
return Err(format!("runtime request failed with status {status}"));
|
return Err(format!("runtime request failed with status {status}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
response
|
response.json().await.map_err(|err| err.to_string())
|
||||||
.json()
|
|
||||||
.await
|
|
||||||
.map_err(|err| err.to_string())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runtime_url(runtime: &RuntimeDiscovery, path: &str, query: &[(&str, String)]) -> Result<reqwest::Url, String> {
|
fn runtime_url(runtime: &RuntimeDiscovery, path: &str, query: &[(&str, String)]) -> Result<reqwest::Url, String> {
|
||||||
|
|||||||
@@ -116,8 +116,12 @@ where
|
|||||||
let source = source.ok_or_else(|| de::Error::missing_field("source"))?;
|
let source = source.ok_or_else(|| de::Error::missing_field("source"))?;
|
||||||
|
|
||||||
match (ok, has_data, has_error) {
|
match (ok, has_data, has_error) {
|
||||||
(true, true, false) => Ok(CliEnvelope::Success { ok, source, data: data.expect("data presence was checked") }),
|
(true, true, false) => {
|
||||||
(false, false, true) => Ok(CliEnvelope::Failure { ok, source, error: error.expect("error presence was checked") }),
|
Ok(CliEnvelope::Success { ok, source, data: data.expect("data presence was checked") })
|
||||||
|
}
|
||||||
|
(false, false, true) => {
|
||||||
|
Ok(CliEnvelope::Failure { ok, source, error: error.expect("error presence was checked") })
|
||||||
|
}
|
||||||
(true, _, _) => Err(de::Error::custom("ok=true requires data without error")),
|
(true, _, _) => Err(de::Error::custom("ok=true requires data without error")),
|
||||||
(false, _, _) => Err(de::Error::custom("ok=false requires error without data")),
|
(false, _, _) => Err(de::Error::custom("ok=false requires error without data")),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -165,7 +165,11 @@ pub async fn execute_query(pool: &SqlitePool, sql: &str) -> Result<QueryResult,
|
|||||||
execute_query_with_row_limit(pool, sql, crate::query::MAX_ROWS).await
|
execute_query_with_row_limit(pool, sql, crate::query::MAX_ROWS).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn execute_query_with_row_limit(pool: &SqlitePool, sql: &str, row_limit: usize) -> Result<QueryResult, String> {
|
pub async fn execute_query_with_row_limit(
|
||||||
|
pool: &SqlitePool,
|
||||||
|
sql: &str,
|
||||||
|
row_limit: usize,
|
||||||
|
) -> Result<QueryResult, String> {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
let row_limit = row_limit.max(1);
|
let row_limit = row_limit.max(1);
|
||||||
|
|
||||||
|
|||||||
@@ -144,10 +144,11 @@ async fn ensure_history_columns(pool: &SqlitePool) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn ensure_handoffs_sequence(pool: &SqlitePool) -> Result<(), String> {
|
async fn ensure_handoffs_sequence(pool: &SqlitePool) -> Result<(), String> {
|
||||||
let (seq_columns,): (i64,) = sqlx::query_as("SELECT COUNT(*) FROM pragma_table_info('handoffs') WHERE name = 'seq'")
|
let (seq_columns,): (i64,) =
|
||||||
.fetch_one(pool)
|
sqlx::query_as("SELECT COUNT(*) FROM pragma_table_info('handoffs') WHERE name = 'seq'")
|
||||||
.await
|
.fetch_one(pool)
|
||||||
.map_err(|e| e.to_string())?;
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
if seq_columns > 0 {
|
if seq_columns > 0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -1057,10 +1058,8 @@ mod handoff_tests {
|
|||||||
async fn handoffs_table_has_stable_autoincrement_sequence() {
|
async fn handoffs_table_has_stable_autoincrement_sequence() {
|
||||||
let storage = open_temp_storage().await;
|
let storage = open_temp_storage().await;
|
||||||
|
|
||||||
let columns: Vec<(String,)> = sqlx::query_as("SELECT name FROM pragma_table_info('handoffs')")
|
let columns: Vec<(String,)> =
|
||||||
.fetch_all(&storage.db)
|
sqlx::query_as("SELECT name FROM pragma_table_info('handoffs')").fetch_all(&storage.db).await.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
assert!(columns.iter().any(|(name,)| name == "seq"));
|
assert!(columns.iter().any(|(name,)| name == "seq"));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user