Skip to content
Clone

refactor(rust): inline single-use wrappers

A single-call-site sweep over codegraph call edges (verified afterwards with ripgrep, because the index misses cfg'd and non-AST callers) found very few pointless wrappers: am_as_model was a bare clone (pass &row directly), and strip_apostrophes, in_nets, empty_state, lock_key, normalize_host and IndexSink::take_uids each had a single call site and no other meaning. All are inlined. Everything else with one call site is deliberate API - config accessors with defaults, documented delegation layers (git's LFS facade, search outbox enqueuers, agent notification shims), key/format builders and semantic predicates - and was left alone; several apparent single-call candidates (repo_dir_for, new_id, pow_solver::solve) actually had callers the index missed and were kept.
ZhenYi committed
parent 149bec6 commit 209a1c0 Browse files

8 files changed

+10 −38

File tree 8
Modified lib/admin/src/services/users.rs View lib/admin/src/services/users.rs
@@ -319,7 +319,7 @@
319319 }
320320 if changed.is_empty() {
321321 let suspended = self.active_suspension_ids(&[uid]).await?;
322 return Ok(user_summary(&am_as_model(&row), suspended.contains(&uid)));
322 return Ok(user_summary(&row, suspended.contains(&uid)));
323323 }
324324 am.updated_at = Set(OffsetDateTime::now_utc());
325325 am.version = Set(row.version + 1);
@@ -595,10 +595,6 @@
595595 anonymized: true,
596596 })
597597 }
598}
599
600fn am_as_model(row: &user::Model) -> user::Model {
601 row.clone()
602598}
603599
604600#[derive(Debug, Clone, Deserialize, IntoParams)]
Modified lib/git/src/handler/html/mod.rs View lib/git/src/handler/html/mod.rs
@@ -369,7 +369,3 @@
369369 None => (String::new(), true),
370370 }
371371}
372
373pub fn empty_state(dto: &RepoDto) -> bool {
374 dto.commits_count == 0
375}
Modified lib/git/src/handler/html/quickstart.rs View lib/git/src/handler/html/quickstart.rs
@@ -2,9 +2,7 @@
22use axum::response::Response;
33use maud::html;
44
5use crate::handler::html::{
6 HtmlRepoPath, clone_https_url, clone_ssh_url, empty_state, resolve, response_html,
7};
5use crate::handler::html::{HtmlRepoPath, clone_https_url, clone_ssh_url, resolve, response_html};
86use crate::service::GitService;
97use crate::share::access::BasicAuth;
108
@@ -29,7 +27,7 @@
2927 Ok(c) => c,
3028 Err(resp) => return resp,
3129 };
32 let is_empty = empty_state(&ctx.dto);
30 let is_empty = ctx.dto.commits_count == 0;
3331 let base = "git-quickstart space-y-3 rounded-lg border border-dashed border-zinc-300 p-4";
3432 let cls = if ctx.dto.is_archived {
3533 format!("{base} opacity-60")
Modified lib/git/src/ssh/git_service.rs View lib/git/src/ssh/git_service.rs
@@ -49,12 +49,8 @@
4949 Some((service, path))
5050}
5151
52fn strip_apostrophes(s: &str) -> &str {
53 s.trim().trim_matches('\'')
54}
55
5652pub fn parse_repo_path(path: &str) -> Option<(&str, &str)> {
57 let path = strip_apostrophes(path).trim_matches('/');
53 let path = path.trim().trim_matches('\'').trim_matches('/');
5854 if path.is_empty() || path.len() > MAX_REPO_PATH_LEN {
5955 return None;
6056 }
Modified lib/git/src/sync/lock.rs View lib/git/src/sync/lock.rs
@@ -71,16 +71,12 @@
7171 }
7272}
7373
74fn lock_key(repo_id: Uuid) -> String {
75 format!("{LOCK_KEY_PREFIX}{repo_id}")
76}
77
7874pub async fn acquire_lock(
7975 redis: &AppRedis,
8076 repo_id: Uuid,
8177 ttl_secs: u64,
8278) -> SyncResult<Option<SyncLock>> {
83 let key = lock_key(repo_id);
79 let key = format!("{LOCK_KEY_PREFIX}{repo_id}");
8480 let value = format!("{}:{}", Uuid::now_v7(), process::id());
8581
8682 let mut conn = redis.connection_clone();
Modified lib/mcp-api/src/net.rs View lib/mcp-api/src/net.rs
@@ -9,14 +9,12 @@
99 trusted_proxies: &[IpNet],
1010) -> Option<IpAddr> {
1111 match peer {
12 Some(addr) if in_nets(addr.ip(), trusted_proxies) => forwarded_ip(headers),
12 Some(addr) if trusted_proxies.iter().any(|net| net.contains(&addr.ip())) => {
13 forwarded_ip(headers)
14 }
1315 Some(addr) => Some(addr.ip()),
1416 None => None,
1517 }
16}
17
18pub fn in_nets(ip: IpAddr, nets: &[IpNet]) -> bool {
19 nets.iter().any(|net| net.contains(&ip))
2018}
2119
2220fn forwarded_ip(headers: &HeaderMap) -> Option<IpAddr> {
Modified lib/search/src/service.rs View lib/search/src/service.rs
@@ -962,7 +962,7 @@
962962 sink.absorb(code_docs, symbol_docs).await?;
963963 }
964964 sink.finish().await?;
965 task_uids.extend(sink.take_uids());
965 task_uids.extend(std::mem::take(&mut sink.uids));
966966
967967 let wait_permits = Arc::new(Semaphore::new(self.config.meili_inflight.max(1)));
968968 let mut waiters = JoinSet::new();
@@ -1822,10 +1822,6 @@
18221822 }
18231823 }
18241824 Ok(())
1825 }
1826
1827 fn take_uids(&mut self) -> Vec<u64> {
1828 std::mem::take(&mut self.uids)
18291825 }
18301826
18311827 fn files(&self) -> usize {
Modified lib/social/src/parse/mod.rs View lib/social/src/parse/mod.rs
@@ -202,13 +202,9 @@
202202 })
203203}
204204
205fn normalize_host(host: &str) -> String {
206 host.trim_end_matches('.').to_ascii_lowercase()
207}
208
209205/// 按域名正则把 URL 路由到平台。
210206pub fn route(url: &Url) -> Option<&'static dyn Platform> {
211 let host = normalize_host(url.host_str()?);
207 let host = url.host_str()?.trim_end_matches('.').to_ascii_lowercase();
212208 routes()
213209 .iter()
214210 .find(|route| route.host.is_match(&host))