nats.rs icon indicating copy to clipboard operation
nats.rs copied to clipboard

Add a subject type

Open MattesWhite opened this issue 3 years ago • 12 comments

This PR adds a Subject type to the NATS client. It also includes some convenience stuff like a mutable, owned version SubjectBuf, Tokens as an own type, the possibility to iterate a subjects tokens and a matching algorithm.

For the validation algorithm I tried to stick to the docs from nats.io.

The subject type is for now nowhere integrated, i.e. it would only be useful for users. However, piece by piece substituting str with Subject(Buf) should proof beneficial.

This could also be used to solve #221 .

MattesWhite avatar Sep 17 '21 09:09 MattesWhite

Yes, in my application I don't validate subjects that much either. But frequently use the Subjects to parse messages I receive from subscription with wildcards to check out where it actually came from and subject.tokens().nth(4) is just way simpler than writting some custum parser every time.

MattesWhite avatar Oct 06 '21 14:10 MattesWhite

Interesting but I'm unsure. Any thoughts on this addition @Jarema?

caspervonb avatar Jan 06 '22 03:01 caspervonb

I think the ability to pull tokens and such is a good addition, but would not want to force changes to normal publish and subscribe.

derekcollison avatar Jan 06 '22 03:01 derekcollison

Yes, an integration of the Subject API into the crate was my long time goal. However, I was not sure if the work would be accepted so I waited for a positive response. When I'm done with my other PR I'll continue here.

MattesWhite avatar Jan 06 '22 12:01 MattesWhite

As predicted, incorporating Subject and SubjectBuf into the whole crate is a lot of work. At the moment I'm not done but the work in the crate itself is mostly done.

I reworked Subject and Token into dynamically sized types, i.e. they behave like str or Path now. Sadly, this includes a little bit of unsafe. I'd like to have your feedback on this matter.

ToDo:

  • [x] Fix benches, examples and tests
  • [x] Reevaluate the uses of Subject::new_unchecked() and SubjectBuf::new_unchecked()
  • [x] Write proper documentation for Subject and SubjectBuf
  • [x] DISCUSS: Public API could take impl TryInto<&Subject> or a bound to another converter trait. This would probably lessen the migration effort for users.

MattesWhite avatar Jan 09 '22 12:01 MattesWhite

FYI we need two types. One for publish which can not have wildcards (must be literal) and one for subscriptions which can include wildcards.

derekcollison avatar Jan 15 '22 17:01 derekcollison

The current implementation has a contains_wildcards() -> bool isn't that enough?

MattesWhite avatar Jan 19 '22 20:01 MattesWhite

Possibly, depends on the contract and when they are valid or not and whether or not we want that correctness enforced in a zero abstractions scenario.

derekcollison avatar Jan 20 '22 01:01 derekcollison

Discussed this a little bit offline last week with @Jarema. I think a type for this can be useful, in that we provide a guarantee that once constructed the type is valid but this feels a bit heavy handed with both Subject and SubjectBuf's.

I feel like the sweet spot could be to have a Subject type, that is a validated type. Then we can implement AsSubject/Into<Subject> traits for conversions from &str, String, etc. Basically somewhere in-between this PR and https://github.com/nats-io/nats.rs/pull/295

I think I might take a quick pass at this soon, see if my vague idea is actually any better in practice 🙂

caspervonb avatar Jan 25 '22 12:01 caspervonb

So after long time I have again some time to work on this. So far I got everything to compile again. Next I'll marge main to catch up with recent development (good work by the way +1). Then I'll fix tests to work again. And after all this is done we can start reviewing this giant of a PR...

MattesWhite avatar Mar 07 '22 07:03 MattesWhite

@caspervonb Finally, after months of delay this PR is ready for review, CI is passing, and Subject is introduced to the whole crate.

I slimed the added features a bit down:

  • Token as an own type is gone
  • Subject::sub_subject() is removed

The AsSubject trait was introduced to allow the use of &str in most places of the public API so the doc tests are mostly left unchanged, and the change for users should be minimal.

MattesWhite avatar Apr 05 '22 14:04 MattesWhite

@MattesWhite hey! We will look into this as soon as we will tackle this topic in async-nats client to have it in both, consistent.

Jarema avatar Apr 28 '22 10:04 Jarema

Closing this PR as it is way outdated and superseded by #596 and #952

MattesWhite avatar Aug 30 '23 07:08 MattesWhite