Files
radicle-git/git-ref-format/core/src/cbor.rs
T
Fintan Halpenny daffac4101 git-ref-format: rename namespace functions
The namespace functions can read confusingly:
* `namespaced` - the naming sounds like it is namespacing the input,
when in fact it is ensuring that the input is namespaced.
* `add_namespace` - can be read as adding the namespace to the end of
a ref, when in fact it is placed at the beginning.

Change the naming, respectively, to:
* `to_namespaced` - this uses the conversion naming convention
detailed here[0].
* `with_namespace` - reads as plainly namespacing the ref string.

[0]: https://rust-lang.github.io/api-guidelines/naming.html#ad-hoc-conversions-follow-as_-to_-into_-conventions-c-conv

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
2022-10-13 09:21:53 +01:00

118 lines
3.0 KiB
Rust

// Copyright © 2021 The Radicle Link Contributors
//
// This file is part of radicle-link, distributed under the GPLv3 with Radicle
// Linking Exception. For full terms see the included LICENSE file.
use std::convert::TryFrom;
use minicbor::{
decode,
encode::{self, Write},
Decode,
Decoder,
Encode,
Encoder,
};
use crate::{
refspec::{PatternStr, PatternString},
Namespaced,
Qualified,
RefStr,
RefString,
};
impl<'de: 'a, 'a> Decode<'de> for &'a RefStr {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
d.str()
.and_then(|s| Self::try_from(s).map_err(|e| decode::Error::Custom(Box::new(e))))
}
}
impl<'a> Encode for &'a RefStr {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
e.str(self.as_str())?;
Ok(())
}
}
impl<'de> Decode<'de> for RefString {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
Decode::decode(d).map(|s: &RefStr| s.to_owned())
}
}
impl Encode for RefString {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
self.as_refstr().encode(e)
}
}
impl<'de: 'a, 'a> Decode<'de> for &'a PatternStr {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
d.str()
.and_then(|s| Self::try_from(s).map_err(|e| decode::Error::Custom(Box::new(e))))
}
}
impl<'a> Encode for &'a PatternStr {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
e.str(self.as_str())?;
Ok(())
}
}
impl<'de> Decode<'de> for PatternString {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
Decode::decode(d).map(|s: &PatternStr| s.to_owned())
}
}
impl Encode for PatternString {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
self.as_pattern_str().encode(e)
}
}
impl<'de: 'a, 'a> Decode<'de> for Qualified<'a> {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
Decode::decode(d).and_then(|s: &RefStr| {
s.qualified()
.ok_or(decode::Error::Message("not a qualified ref"))
})
}
}
impl<'a> Encode for Qualified<'a> {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
self.as_str().encode(e)
}
}
impl<'de: 'a, 'a> Decode<'de> for Namespaced<'a> {
#[inline]
fn decode(d: &mut Decoder<'de>) -> Result<Self, decode::Error> {
Decode::decode(d).and_then(|s: &RefStr| {
s.to_namespaced()
.ok_or(decode::Error::Message("not a namespaced ref"))
})
}
}
impl<'a> Encode for Namespaced<'a> {
#[inline]
fn encode<W: Write>(&self, e: &mut Encoder<W>) -> Result<(), encode::Error<W::Error>> {
self.as_str().encode(e)
}
}