Skip to content

Commit

Permalink
Optimize for binary size
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Oct 6, 2024
1 parent 88455da commit 6e87053
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ edition = "2021"
[profile.release]
lto = true
opt-level = "z"
panic = "abort"
strip = "symbols"

[dependencies]

Expand Down
34 changes: 18 additions & 16 deletions src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Result;
use dirs::data_dir;
use rusqlite::{params, Connection};
use std::{fs::create_dir, path::PathBuf};
use crate::Result;

pub fn connect() -> Result<Connection> {
let conn = Connection::open(path()?).map_err(|e| {
Expand All @@ -17,10 +17,10 @@ pub fn insert_settings_string(name: &str, value: &str, conn: &Connection) -> Res
&format!("UPDATE settings SET json = json_set(json, '$.{name}', ?1);"),
params![value],
)
.map_err(|e| {
eprintln!("failed to set {name} to {value}: {e}");
e
})?;
.map_err(|e| {
eprintln!("failed to set {name} to {value}: {e}");
e
})?;
Ok(())
}

Expand All @@ -37,13 +37,15 @@ pub fn query_settings_string(name: &str, conn: &Connection) -> Result<String> {
eprintln!("failed to query {name} from settings");
e
})?;
Ok(match rows.next().map_err(|e| {
eprintln!("failed to query {name} from settings");
e
})? {
Some(first_row) => first_row.get(0).unwrap_or("".into()),
None => "".into(),
})
Ok(
match rows.next().map_err(|e| {
eprintln!("failed to query {name} from settings");
e
})? {
Some(first_row) => first_row.get(0).unwrap_or("".into()),
None => "".into(),
},
)
}

fn path() -> Result<PathBuf> {
Expand All @@ -63,10 +65,10 @@ fn init(conn: &Connection) -> Result<()> {
"CREATE TABLE IF NOT EXISTS settings (json TEXT NOT NULL);",
(),
)
.map_err(|e| {
eprintln!("failed to create settings table: {e}");
e
})?;
.map_err(|e| {
eprintln!("failed to create settings table: {e}");
e
})?;
let mut stmt = conn
.prepare("SELECT COUNT (*) FROM settings;")
.map_err(|e| {
Expand Down
8 changes: 4 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ type Result<T> = std::result::Result<T, Box<dyn Error>>;
fn main() -> Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
Err(
"you need to provide an action, run btcmap-cli help to see all supported actions"
)?;
Err("you need to provide an action, run btcmap-cli help to see all supported actions")?;
}
let action = args[1].as_str();
let password = db::query_settings_string("password", &db::connect()?)?;
Expand Down Expand Up @@ -101,7 +99,9 @@ fn main() -> Result<()> {
json!({"period_start":period_start,"period_end":period_end}),
)?;
}
"generate-element-issues" => rpc::call_remote_procedure("generateelementissues", json!({}))?,
"generate-element-issues" => {
rpc::call_remote_procedure("generateelementissues", json!({}))?
}
"sync-elements" => rpc::call_remote_procedure("syncelements", json!({}))?,
"get-most-commented-countries" => {
let period_start = args[2].clone();
Expand Down
16 changes: 7 additions & 9 deletions src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
use crate::db;
use crate::Result;
use colored_json::ToColoredJson;
use reqwest::blocking::ClientBuilder;
use reqwest::blocking::Response;
use serde_json::{json, Map, Value};
use crate::Result;

pub fn call_remote_procedure(name: &str, mut params: Value) -> Result<()> {
let params = params.as_object_mut()
let params = params
.as_object_mut()
.ok_or("params value is not a valid JSON object")?;
params.insert(
"password".into(),
db::query_settings_string("password", &db::connect()?)?.into(),
);
let http_client = ClientBuilder::new()
.timeout(None)
.build()
.map_err(|e| {
eprintln!("failed to initialize HTTP client: {e}");
e
})?;
let http_client = ClientBuilder::new().timeout(None).build().map_err(|e| {
eprintln!("failed to initialize HTTP client: {e}");
e
})?;
let args = json!(
{"jsonrpc": "2.0", "method": name, "params": params, "id": 1}
);
Expand Down

0 comments on commit 6e87053

Please sign in to comment.