refactor serde crate to avoid name duplication

This commit is contained in:
Sebastian Thiel 2024-08-08 16:52:51 +02:00
parent ee7660f395
commit 8ed90644a1
No known key found for this signature in database
GPG Key ID: 9CB5EE7895E8268B
13 changed files with 123 additions and 127 deletions

View File

@ -31,9 +31,9 @@ pub struct BaseBranch {
pub remote_url: String,
pub push_remote_name: Option<String>,
pub push_remote_url: String,
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub base_sha: git2::Oid,
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub current_sha: git2::Oid,
pub behind: usize,
pub upstream_commits: Vec<RemoteCommit>,

View File

@ -347,7 +347,7 @@ pub struct BranchListing {
pub name: BranchIdentity,
/// This is a list of remotes that this branch can be found on (e.g. `origin`, `upstream` etc.),
/// by collecting remotes from all local branches with the same identity that have a tracking setup.
#[serde(serialize_with = "gitbutler_serde::serde::as_string_lossy_vec")]
#[serde(serialize_with = "gitbutler_serde::as_string_lossy_vec")]
pub remotes: Vec<BString>,
/// The branch may or may not have a virtual branch associated with it.
pub virtual_branch: Option<VirtualBranchReference>,
@ -536,10 +536,10 @@ pub struct BranchEntry {
/// The name of the branch (e.g. `main`, `feature/branch`)
pub name: String,
/// The head commit of the branch
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
head: git2::Oid,
/// The commit base of the branch
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
base: git2::Oid,
/// The list of commits associated with the branch
pub commits: Vec<CommitEntry>,
@ -568,18 +568,18 @@ pub struct RemoteBranchEntry {
#[serde(rename_all = "camelCase")]
pub struct CommitEntry {
/// The commit sha that it can be referenced by
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub id: git2::Oid,
/// If the commit is referencing a specific change, this is its change id
pub change_id: Option<String>,
/// The commit message
#[serde(serialize_with = "gitbutler_serde::serde::as_string_lossy")]
#[serde(serialize_with = "gitbutler_serde::as_string_lossy")]
pub description: BString,
/// The timestamp of the commit in milliseconds
pub created_at: u128,
/// The author of the commit
pub authors: Vec<Author>,
/// The parent commits of the commit
#[serde(with = "gitbutler_serde::serde::oid_vec")]
#[serde(with = "gitbutler_serde::oid_vec")]
pub parent_ids: Vec<git2::Oid>,
}

View File

@ -21,16 +21,16 @@ use crate::{
#[derive(Debug, PartialEq, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct VirtualBranchCommit {
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub id: git2::Oid,
#[serde(serialize_with = "gitbutler_serde::serde::as_string_lossy")]
#[serde(serialize_with = "gitbutler_serde::as_string_lossy")]
pub description: BString,
pub created_at: u128,
pub author: Author,
pub is_remote: bool,
pub files: Vec<VirtualBranchFile>,
pub is_integrated: bool,
#[serde(with = "gitbutler_serde::serde::oid_vec")]
#[serde(with = "gitbutler_serde::oid_vec")]
pub parent_ids: Vec<git2::Oid>,
pub branch_id: BranchId,
pub change_id: Option<String>,

View File

@ -24,7 +24,7 @@ use serde::Serialize;
#[serde(rename_all = "camelCase")]
pub struct VirtualBranchHunk {
pub id: String,
#[serde(serialize_with = "gitbutler_serde::serde::as_string_lossy")]
#[serde(serialize_with = "gitbutler_serde::as_string_lossy")]
pub diff: BString,
pub modified_at: u128,
pub file_path: PathBuf,
@ -50,7 +50,7 @@ pub struct VirtualBranchHunk {
#[serde(rename_all = "camelCase")]
pub struct HunkLock {
pub branch_id: BranchId,
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub commit_id: git2::Oid,
}

View File

@ -23,7 +23,7 @@ use crate::author::Author;
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RemoteBranch {
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub sha: git2::Oid,
pub name: Refname,
pub upstream: Option<RemoteRefname>,
@ -36,13 +36,13 @@ pub struct RemoteBranch {
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RemoteBranchData {
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub sha: git2::Oid,
pub name: Refname,
pub upstream: Option<RemoteRefname>,
pub behind: u32,
pub commits: Vec<RemoteCommit>,
#[serde(with = "gitbutler_serde::serde::oid_opt", default)]
#[serde(with = "gitbutler_serde::oid_opt", default)]
pub fork_point: Option<git2::Oid>,
}
@ -50,12 +50,12 @@ pub struct RemoteBranchData {
#[serde(rename_all = "camelCase")]
pub struct RemoteCommit {
pub id: String,
#[serde(serialize_with = "gitbutler_serde::serde::as_string_lossy")]
#[serde(serialize_with = "gitbutler_serde::as_string_lossy")]
pub description: BString,
pub created_at: u128,
pub author: Author,
pub change_id: Option<String>,
#[serde(with = "gitbutler_serde::serde::oid_vec")]
#[serde(with = "gitbutler_serde::oid_vec")]
pub parent_ids: Vec<git2::Oid>,
}

View File

@ -68,13 +68,13 @@ pub struct VirtualBranch {
pub updated_at: u128,
pub selected_for_changes: bool,
pub allow_rebasing: bool,
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub head: git2::Oid,
/// The merge base between the target branch and the virtual branch
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub merge_base: git2::Oid,
/// The fork point between the target branch and the virtual branch
#[serde(with = "gitbutler_serde::serde::oid_opt", default)]
#[serde(with = "gitbutler_serde::oid_opt", default)]
pub fork_point: Option<git2::Oid>,
}

View File

@ -24,7 +24,7 @@ pub struct Branch {
/// The local tracking branch, holding the state of the remote.
pub upstream: Option<RemoteRefname>,
// upstream_head is the last commit on we've pushed to the upstream branch
#[serde(with = "gitbutler_serde::serde::oid_opt", default)]
#[serde(with = "gitbutler_serde::oid_opt", default)]
pub upstream_head: Option<git2::Oid>,
#[serde(
serialize_with = "serialize_u128",
@ -37,10 +37,10 @@ pub struct Branch {
)]
pub updated_timestamp_ms: u128,
/// tree is the last git tree written to a session, or merge base tree if this is new. use this for delta calculation from the session data
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub tree: git2::Oid,
/// head is id of the last "virtual" commit in this branch
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub head: git2::Oid,
pub ownership: BranchOwnershipClaims,
// order is the number by which UI should sort branches

View File

@ -49,10 +49,7 @@ pub struct GitHunk {
pub new_start: u32,
pub new_lines: u32,
/// The `+`, `-` or ` ` prefixed lines of the diff produced by `git2`, along with their line separator.
#[serde(
rename = "diff",
serialize_with = "gitbutler_serde::serde::as_string_lossy"
)]
#[serde(rename = "diff", serialize_with = "gitbutler_serde::as_string_lossy")]
pub diff_lines: BString,
pub binary: bool,
pub change_type: ChangeType,

View File

@ -16,10 +16,10 @@ use strum::EnumString;
#[serde(rename_all = "camelCase")]
pub struct Snapshot {
/// The id of the commit that represents the snapshot
#[serde(rename = "id", with = "gitbutler_serde::serde::oid")]
#[serde(rename = "id", with = "gitbutler_serde::oid")]
pub commit_id: git2::Oid,
/// Snapshot creation time in seconds from Unix epoch seconds, based on a commit as `commit_id`.
#[serde(serialize_with = "gitbutler_serde::serde::as_time_seconds_from_unix_epoch")]
#[serde(serialize_with = "gitbutler_serde::as_time_seconds_from_unix_epoch")]
pub created_at: git2::Time,
/// The number of working directory lines added in the snapshot
pub lines_added: usize,

View File

@ -26,7 +26,7 @@ fn unix_epoch() -> SystemTime {
#[derive(Serialize, Deserialize, Debug)]
pub struct Oplog {
/// This is the sha of the last oplog commit
#[serde(with = "gitbutler_serde::serde::oid_opt", default)]
#[serde(with = "gitbutler_serde::oid_opt", default)]
pub head_sha: Option<git2::Oid>,
/// The time when the last snapshot was created. Seconds since Epoch
#[serde(

View File

@ -61,7 +61,7 @@ impl FetchResult {
#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct CodePushState {
#[serde(with = "gitbutler_serde::serde::oid")]
#[serde(with = "gitbutler_serde::oid")]
pub id: git2::Oid,
pub timestamp: time::SystemTime,
}

View File

@ -1 +1,94 @@
pub mod serde;
use bstr::{BString, ByteSlice};
use serde::Serialize;
pub fn as_string_lossy<S>(v: &BString, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.to_str_lossy().serialize(s)
}
pub fn as_string_lossy_vec<S>(v: &[BString], s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let vec: Vec<String> = v.iter().map(|v| v.to_string()).collect();
vec.serialize(s)
}
pub fn as_time_seconds_from_unix_epoch<S>(v: &git2::Time, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.seconds().serialize(s)
}
pub mod oid_opt {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &Option<git2::Oid>, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.as_ref().map(|v| v.to_string()).serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<Option<git2::Oid>, D::Error>
where
D: Deserializer<'de>,
{
let hex = <Option<String> as Deserialize>::deserialize(d)?;
hex.map(|v| {
v.parse()
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
})
.transpose()
}
}
pub mod oid_vec {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &[git2::Oid], s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let vec: Vec<String> = v.iter().map(|v| v.to_string()).collect();
vec.serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<Vec<git2::Oid>, D::Error>
where
D: Deserializer<'de>,
{
let hex = <Vec<String> as Deserialize>::deserialize(d)?;
let hex: Result<Vec<git2::Oid>, D::Error> = hex
.into_iter()
.map(|v| {
git2::Oid::from_str(v.as_str())
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
})
.collect();
hex
}
}
pub mod oid {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &git2::Oid, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.to_string().serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<git2::Oid, D::Error>
where
D: Deserializer<'de>,
{
let hex = String::deserialize(d)?;
hex.parse()
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
}
}

View File

@ -1,94 +0,0 @@
use bstr::{BString, ByteSlice};
use serde::Serialize;
pub fn as_string_lossy<S>(v: &BString, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.to_str_lossy().serialize(s)
}
pub fn as_string_lossy_vec<S>(v: &[BString], s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let vec: Vec<String> = v.iter().map(|v| v.to_string()).collect();
vec.serialize(s)
}
pub fn as_time_seconds_from_unix_epoch<S>(v: &git2::Time, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.seconds().serialize(s)
}
pub mod oid_opt {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &Option<git2::Oid>, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.as_ref().map(|v| v.to_string()).serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<Option<git2::Oid>, D::Error>
where
D: Deserializer<'de>,
{
let hex = <Option<String> as Deserialize>::deserialize(d)?;
hex.map(|v| {
v.parse()
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
})
.transpose()
}
}
pub mod oid_vec {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &[git2::Oid], s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let vec: Vec<String> = v.iter().map(|v| v.to_string()).collect();
vec.serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<Vec<git2::Oid>, D::Error>
where
D: Deserializer<'de>,
{
let hex = <Vec<String> as Deserialize>::deserialize(d)?;
let hex: Result<Vec<git2::Oid>, D::Error> = hex
.into_iter()
.map(|v| {
git2::Oid::from_str(v.as_str())
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
})
.collect();
hex
}
}
pub mod oid {
use serde::{Deserialize, Deserializer, Serialize};
pub fn serialize<S>(v: &git2::Oid, s: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
v.to_string().serialize(s)
}
pub fn deserialize<'de, D>(d: D) -> Result<git2::Oid, D::Error>
where
D: Deserializer<'de>,
{
let hex = String::deserialize(d)?;
hex.parse()
.map_err(|err: git2::Error| serde::de::Error::custom(err.to_string()))
}
}