refactor: prefer Arc<str> to String

This commit is contained in:
2026-05-23 18:45:44 +01:00
parent 1ef91cb41e
commit 1843622540
15 changed files with 524 additions and 544 deletions

View File

@@ -1,4 +1,4 @@
use std::ops::Deref;
use std::sync::Arc;
use graphql_client::GraphQLQuery;
use serde::Deserialize;
@@ -21,33 +21,7 @@ type GitObjectID = String;
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash, Deserialize)]
#[serde(transparent)]
#[repr(transparent)]
pub(crate) struct Id(String);
impl Deref for Id {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<&str> for Id {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl From<String> for Id {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<Id> for String {
fn from(value: Id) -> Self {
value.0
}
}
pub(crate) struct Id(pub(crate) Arc<str>);
#[derive(Debug, Deserialize)]
pub(crate) struct PullRequestPaginatedResponse {
@@ -59,32 +33,32 @@ pub(crate) struct PullRequestPaginatedResponse {
#[derive(Debug, Deserialize)]
pub(crate) struct PullRequest {
pub(crate) id: Id,
pub(crate) title: String,
pub(crate) title: Arc<str>,
pub(crate) state: PullRequestState,
pub(crate) is_draft: bool,
pub(crate) repo_slug: String,
pub(crate) repo_slug: Arc<str>,
}
#[derive(Debug, Deserialize)]
pub(crate) struct DetailedPullRequest {
pub(crate) title: String,
pub(crate) title: Arc<str>,
pub(crate) state: PullRequestState,
pub(crate) is_draft: bool,
pub(crate) body: String,
pub(crate) body: Arc<str>,
pub(crate) created_at: Option<chrono::DateTime<chrono::FixedOffset>>,
pub(crate) author: Option<super::user::Actor>,
pub(crate) base_branch_name: String,
pub(crate) base_repo_slug: String,
pub(crate) base_ref: String,
pub(crate) head_branch_name: String,
pub(crate) head_ref: String,
pub(crate) head_repo_slug: String,
pub(crate) base_branch_name: Arc<str>,
pub(crate) base_repo_slug: Arc<str>,
pub(crate) base_ref: Arc<str>,
pub(crate) head_branch_name: Arc<str>,
pub(crate) head_ref: Arc<str>,
pub(crate) head_repo_slug: Arc<str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PullRequestTimeline {
pub(crate) items: Vec<PullRequestTimelineItem>,
pub(crate) end_cursor: Option<String>,
pub(crate) end_cursor: Option<Arc<str>>,
pub(crate) has_next_page: bool,
}
@@ -245,12 +219,6 @@ pub(crate) struct TimelineActor {
pub(crate) avatar_url: Option<String>,
}
impl std::fmt::Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub(crate) enum PullRequestState {
@@ -310,6 +278,24 @@ pub(crate) struct ListPullRequests {
pub page: u32,
}
impl std::fmt::Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl From<String> for Id {
fn from(value: String) -> Self {
Self(value.into())
}
}
impl From<&str> for Id {
fn from(value: &str) -> Self {
Self(value.into())
}
}
impl query::QueryFn for ListPullRequests {
type Data = PullRequestPaginatedResponse;
type Error = api::Error;
@@ -357,13 +343,14 @@ impl query::QueryFn for ListPullRequests {
| PullRequestPaginationQuerySearchEdgesNode::PullRequest(p) => {
Some(PullRequest {
id: p.id.into(),
title: p.title,
title: p.title.into(),
state: p.state,
is_draft: p.is_draft,
repo_slug: format!(
"{}/{}",
p.repository.owner.login, p.repository.name
),
)
.into(),
})
}
| _ => None,
@@ -399,7 +386,7 @@ impl query::QueryFn for FetchPullRequest {
}
let gql = PullRequestQuery::build_query(pull_request_query::Variables {
id: self.id.clone().into(),
id: self.id.to_string(),
});
let res = c.github_graphql_request(&gql)?.send().await?;
@@ -421,26 +408,26 @@ impl query::QueryFn for FetchPullRequest {
})?;
Ok(DetailedPullRequest {
title: p.title,
title: p.title.into(),
state: p.state,
is_draft: p.is_draft,
body: p.body,
body: p.body.into(),
author: p.author.map(|it| api::user::Actor {
login: it.login,
avatar_url: it.avatar_url,
login: it.login.into(),
avatar_url: it.avatar_url.into(),
}),
base_repo_slug: p
.base_repository
.map(|it| it.name_with_owner)
.map(|it| it.name_with_owner.into())
.unwrap_or_default(),
base_branch_name: p.base_ref_name,
base_ref: p.base_ref_oid,
base_branch_name: p.base_ref_name.into(),
base_ref: p.base_ref_oid.into(),
head_repo_slug: p
.head_repository
.map(|it| it.name_with_owner)
.map(|it| it.name_with_owner.into())
.unwrap_or_default(),
head_branch_name: p.head_ref_name,
head_ref: p.head_ref_oid,
head_branch_name: p.head_ref_name.into(),
head_ref: p.head_ref_oid.into(),
created_at: Some(created_at),
})
}
@@ -473,7 +460,7 @@ impl query::QueryFn for FetchPullRequestFileTree {
} else {
let gql =
PullRequestFileTreeQuery::build_query(pull_request_file_tree_query::Variables {
id: self.id.clone().into(),
id: self.id.to_string(),
first: self.first,
});
@@ -558,7 +545,7 @@ impl query::QueryFn for FetchPullRequestFileTree {
pub(crate) struct FetchPullRequestTimeline {
pub(crate) id: Id,
pub(crate) first: i64,
pub(crate) after: Option<String>,
pub(crate) after: Option<Arc<str>>,
}
impl query::QueryFn for FetchPullRequestTimeline {
@@ -841,9 +828,9 @@ impl query::QueryFn for FetchPullRequestTimeline {
} else {
let gql =
PullRequestTimelineQuery::build_query(pull_request_timeline_query::Variables {
id: self.id.clone().into(),
id: self.id.to_string(),
first: self.first,
after: self.after.clone(),
after: self.after.as_ref().map(|it| it.to_string()),
});
let res = c.github_graphql_request(&gql)?.send().await?;
@@ -892,7 +879,7 @@ impl query::QueryFn for FetchPullRequestTimeline {
Ok(PullRequestTimeline {
items,
end_cursor: timeline.page_info.end_cursor,
end_cursor: timeline.page_info.end_cursor.map(|it| it.into()),
has_next_page: timeline.page_info.has_next_page,
})
}