Radish alpha
r
rad:z6cFWeWpnZNHh9rUW8phgA3b5yGt
Git libraries for Radicle
Radicle
Git
Merge remote-tracking branch 'origin/fix/ref-serde'
Fintan Halpenny committed 3 years ago
commit 39ce2f934915a563f9420ac9c85480df8a591481
parent 1316a13
5 files changed +45 -8
modified git-ref-format/core/src/serde.rs
@@ -42,7 +42,8 @@ impl<'de> Deserialize<'de> for RefString {
    where
        D: Deserializer<'de>,
    {
-
        Deserialize::deserialize(deserializer).map(|x: &RefStr| x.to_owned())
+
        Deserialize::deserialize(deserializer)
+
            .and_then(|x: String| Self::try_from(x).map_err(de::Error::custom))
    }
}

@@ -83,7 +84,8 @@ impl<'de> Deserialize<'de> for PatternString {
    where
        D: Deserializer<'de>,
    {
-
        Deserialize::deserialize(deserializer).map(|x: &PatternStr| x.to_owned())
+
        Deserialize::deserialize(deserializer)
+
            .and_then(|x: String| Self::try_from(x).map_err(de::Error::custom))
    }
}

@@ -97,15 +99,17 @@ impl Serialize for PatternString {
    }
}

-
impl<'de: 'a, 'a> Deserialize<'de> for Qualified<'a> {
+
impl<'de> Deserialize<'de> for Qualified<'static> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
-
        Deserialize::deserialize(deserializer).and_then(|s: &RefStr| {
+
        Deserialize::deserialize(deserializer).and_then(|s: String| {
+
            let s = RefString::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())
        })
    }
}
@@ -120,15 +124,17 @@ impl Serialize for Qualified<'_> {
    }
}

-
impl<'de: 'a, 'a> Deserialize<'de> for Namespaced<'a> {
+
impl<'de> Deserialize<'de> for Namespaced<'static> {
    #[inline]
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
-
        Deserialize::deserialize(deserializer).and_then(|s: &RefStr| {
+
        Deserialize::deserialize(deserializer).and_then(|s: String| {
+
            let s = RefString::try_from(s).map_err(de::Error::custom)?;
            s.to_namespaced()
                .ok_or_else(|| de::Error::custom("not a namespaced ref"))
+
                .map(|ns| ns.into_owned())
        })
    }
}
modified git-ref-format/t/Cargo.toml
@@ -19,6 +19,7 @@ proptest = "1"

[dev-dependencies]
assert_matches = "1.5"
+
serde_json = "1"

[dev-dependencies.git-ref-format]
path = ".."
modified git-ref-format/t/src/properties/name.rs
@@ -3,7 +3,7 @@

use std::convert::TryFrom;

-
use git_ref_format::{Error, RefStr, RefString};
+
use git_ref_format::{name, refname, Error, RefStr, RefString};
use proptest::prelude::*;
use test_helpers::roundtrip;

@@ -75,7 +75,22 @@ proptest! {

    #[test]
    fn json(input in gen::valid()) {
-
       roundtrip::json(RefString::try_from(input).unwrap())
+
        let input = RefString::try_from(input).unwrap();
+
        roundtrip::json(input.clone());
+
        let qualified = refname!("refs/heads").and(input).qualified().unwrap().into_owned();
+
        roundtrip::json(qualified.clone());
+
        let namespaced = qualified.with_namespace(name::component!("foo"));
+
        roundtrip::json(namespaced);
+
    }
+

+
    #[test]
+
    fn json_value(input in gen::valid()) {
+
        let input = RefString::try_from(input).unwrap();
+
        roundtrip::json_value(input.clone());
+
        let qualified = refname!("refs/heads").and(input).qualified().unwrap().into_owned();
+
        roundtrip::json_value(qualified.clone());
+
        let namespaced = qualified.with_namespace(name::component!("foo"));
+
        roundtrip::json_value(namespaced);
    }

    #[test]
modified git-ref-format/t/src/properties/pattern.rs
@@ -49,6 +49,11 @@ proptest! {
    }

    #[test]
+
    fn json_value(input in gen::with_glob()) {
+
        roundtrip::json_value(refspec::PatternString::try_from(input).unwrap())
+
    }
+

+
    #[test]
    fn cbor(input in gen::with_glob()) {
        roundtrip::cbor(refspec::PatternString::try_from(input).unwrap())
    }
modified test/test-helpers/src/roundtrip.rs
@@ -20,6 +20,16 @@ where
    )
}

+
pub fn json_value<A>(a: A)
+
where
+
    for<'de> A: Clone + Debug + PartialEq + serde::Serialize + serde::Deserialize<'de>,
+
{
+
    assert_eq!(
+
        a.clone(),
+
        serde_json::from_value(serde_json::to_value(a).unwrap()).unwrap()
+
    )
+
}
+

pub fn cbor<A>(a: A)
where
    for<'de> A: Debug + PartialEq + minicbor::Encode + minicbor::Decode<'de>,