-
-
Notifications
You must be signed in to change notification settings - Fork 346
Reference Name Joining #251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
#251) This shows that despite being possible, it's still relatively cumbersome when names have to be combined due to the need for intermediate storage.
Thanks for posting, I remember that in order to make it convenient to use, I kept adding TryFrom implementations to cover the inputs encountered during testing. It's likely that many more are missing and I hope it's possible to add them when discovered.
I have added a test to validate currently possible inputs and to reproduce something akin to what you are describing or so I hope.
I love this macro and can imagine something like it to become a feature-gated part of What I don't understand is how a macro could help with the issue as I see it as it doesn't work at runtime, and if the name of the partial name is known at compile time, then I think it would be possible to turn something like
Based on my recent ramblings, what do you think would these additions look like? To me making |
I did quickly try the The same would be possible for Since the only thing that's saved by this is the Edit: Yes, this implementation I was missing: |
I can expand a little bit more on what I was trying to get across using the same example as I had: pub enum Remote {
Default,
Peer(PeerId),
}
pub struct Reference {
pub remote: Remote,
pub urn: Urn,
} So the way I would attempt to tackle this is implement impl TryFrom<Remote> for PartialNameRef {
type Error = refs::name::Error;
fn try_from(remote: Remote) -> Result<Self, Self::Error> {
match remote {
Remote::Default => partial_name!("default"), // macro can do the validation at compile time
Remote::Peer(peer) => ParitalNameRef::try_from(peer),
}
}
} AsideAdmittedly, the macro doesn't shine here, but it is slightly nicer than using namespace_prefix.join(reflike!("refs/remotes")).join(peer).join(reflike!("refs")) I would then go on to build on top of the impl TryFrom<Reference> for PartialNameRef {
type Error = refs::name::Error;
fn try_from(r: Reference) -> Result<Self, Self::Error> {
let remote = PartialNameRef::try_from(r.remote)?;
let namespace = PartialNameRef::try_from(r.urn)?;
Ok(partial_ref!("refs/rad/remotes").join(namespace).join(remote))
}
} The fn add_remotes(prefix: PartialNameRef, remotes: &[String]) -> Result<Vec<PartialNameRef>, name::Error> To implement this today, I would have to In a nutshell, I find this structural way of building up reference names more natural and avoids having to mess with the formatting of strings (too much :)).
Ya, that's true. But I would find out at runtime if I got my reference formatting wrong, something I don't enjoy 😄 This is all in the vain of catching my idiotic mistakes as soon as possible :) Thanks for the response! And hopefully the above makes it more clear what I was thinking -- I didn't explain myself well enough the first time :) The |
Thanks a lot for going the extra mile to make crystal clear how having compile-time validation and Also I am glad that Edit: Here is how it looks like. The macro I could possibly rip off from how its done in |
Move tests closer to what they are actually testing
That's great! Obviously, no rush since the API is usable :) Thanks for taking my suggestions on board!
Hehe fun =] |
Always appreciated :), keep 'em coming! |
Hey 👋
I've been perusing the
git-ref
family of names, i.e.FullName
,FullNameRef
, andPartialNameRef
. I want to see if I'm using it right and also suggest a couple of additions that might be helpful for consumers of the API.My use case is the following. I want to end up using
try_find_reference
, which takes one parameter that isimpl TryInto<PartialNameRef>
. I have a structural representation of the reference which looks like:Now, I can't directly write a
TryFrom
forPartialNameRef
as far as I can tell because of the associated lifetimes. But I can write aTryFrom
forFullName
and useto_partial
. I'm wondering if that's the correct and intended way of using the API?Regarding my suggestions, when I wrote a
TryFrom
forRemote
, I wasn't able to reuse this when writingTryFrom
forReference
, since there's no way tojoin
twoFullName
s. I ended up having to useformat!
and use theTryFrom
instance on&String
. It would be useful to be able to join two names to get another valid name.The other (possibly) useful suggestion would be to have a macro for writing static names, that are known to be valid at compile time. We used this technique in radicle-link[0] and it was extremely nice compare to using
try_from
everywhere :)Let me know what you think, and perhaps I could help contribute with these additions ✌️
The text was updated successfully, but these errors were encountered: