mirror of
https://seed.radicle.dev/rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt.git
synced 2026-11-15 16:00:35 +08:00
ref-format: improve Qualified/NamespacedPattern API
Some elements of the `QaulifiedPattern` and `NamespacedPattern` API were missing: - `From` implementations - `Display` implementations - `Serialize` and `Deserialize` implementations - `qualified_pattern` macro This also requires bumping versions: - `git-ref-format`: 0.3.1 - `git-ref-core`: 0.3.1 - `git-ref-format-macro`: 0.3.1 - `radicle-git-ext`: 0.8.1 - `radicle-surf`: 0.22.1 Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com> X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
@@ -13,7 +13,7 @@ use std::{
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{check, lit, RefStr, RefString};
|
||||
use crate::{check, lit, Namespaced, Qualified, RefStr, RefString};
|
||||
|
||||
mod iter;
|
||||
pub use iter::{component, Component, Components, Iter};
|
||||
@@ -430,6 +430,20 @@ impl AsRef<Self> for QualifiedPattern<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for QualifiedPattern<'_> {
|
||||
#[inline]
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Qualified<'a>> for QualifiedPattern<'a> {
|
||||
#[inline]
|
||||
fn from(q: Qualified<'a>) -> Self {
|
||||
Self(Cow::Owned(q.into_refstring().into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<QualifiedPattern<'a>> for Cow<'a, PatternStr> {
|
||||
#[inline]
|
||||
fn from(q: QualifiedPattern<'a>) -> Self {
|
||||
@@ -534,6 +548,13 @@ impl Borrow<PatternStr> for NamespacedPattern<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<Namespaced<'a>> for NamespacedPattern<'a> {
|
||||
#[inline]
|
||||
fn from(ns: Namespaced<'a>) -> Self {
|
||||
NamespacedPattern(Cow::Owned(ns.to_ref_string().into()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<NamespacedPattern<'a>> for QualifiedPattern<'a> {
|
||||
#[inline]
|
||||
fn from(ns: NamespacedPattern<'a>) -> Self {
|
||||
|
||||
@@ -5,10 +5,10 @@
|
||||
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use ::serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use crate::{
|
||||
refspec::{PatternStr, PatternString},
|
||||
refspec::{NamespacedPattern, PatternStr, PatternString, QualifiedPattern},
|
||||
Namespaced, Qualified, RefStr, RefString,
|
||||
};
|
||||
|
||||
@@ -145,3 +145,51 @@ impl Serialize for Namespaced<'_> {
|
||||
serializer.serialize_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for QualifiedPattern<'_> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Deserialize::deserialize(deserializer).and_then(|s: String| {
|
||||
let s = PatternString::try_from(s).map_err(de::Error::custom)?;
|
||||
s.qualified()
|
||||
.ok_or_else(|| de::Error::custom("not a qualified ref"))
|
||||
.map(|q| q.into_owned())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for QualifiedPattern<'_> {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for NamespacedPattern<'_> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
Deserialize::deserialize(deserializer).and_then(|s: String| {
|
||||
let s = PatternString::try_from(s).map_err(de::Error::custom)?;
|
||||
s.to_namespaced()
|
||||
.ok_or_else(|| de::Error::custom("not a qualified ref"))
|
||||
.map(|q| q.into_owned())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for NamespacedPattern<'_> {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user