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

Introduce a `Subject` type to async-nats

Open MattesWhite opened this issue 2 years ago • 2 comments

Following #222 this introduces the subject type to the new asnyc-nats client. It seemed to me that async-nats is now mature enough to bring this in again 😄

I adapted the contents to the new async API. BTW great work the new crates feels much better to work with!

Current state of the PR

At the moment I only tackled the main crate, unit and doc tests to give a feeling for the change. I also have the feeling that I missed some points in the jetstream area where names should be subjects or not.

Next steps

I'll leave the PR in this state until further notice if you want this in the crate I gladly will finish the work to be done.

MattesWhite avatar Aug 05 '22 20:08 MattesWhite

Don't feel pressured to react to this PR immediately. I just need this for my own project to dive into procedural macros.

My goal is to provide derive macros for The ToSubject and FromSubject traits so something like this will be possible:

#[derive(Debug, ToSubject)]
#[subject("my.[ adj ].api.thingy.[ id ]")]
struct ThingApi {
    adj: String,
    id: u32,
}

fn main() {
    let thing = ThingApi {
        adj: "awesome".to_string(),
        id: 42,
    };
    assert_eq!(thing.to_subject().unwrap(), subject!("my.awesome.api.thingy.42").unwrap());
}

My progress can be tracked in my forked repo: https://github.com/MattesWhite/nats.rs/pull/2

MattesWhite avatar Aug 06 '22 11:08 MattesWhite

@MattesWhite I will revisit this PR when done with KV :)

Jarema avatar Sep 08 '22 17:09 Jarema

This PR has become very outdated. In addition, there is async_nats::Subject in the meantime which already does what this PR intended to do. Accordingly, I close this PR.

One thing that remains that this PR implements but async-nats doesn't, yet, is the possibility to iterate over the tokens of a Subject. This is very convenient when you subscribe to a wildcard subject and want to know by what the wildcard was replaced on reception, e.g.:

let mut subscriber = client.subscribe("a.*.subject").await?;
let message = subscriber.next().await.unwrap();
let wildcard_value = message.subject.tokens().nth(1).unwrap();

I'll open another PR for this when I find the time 😉

MattesWhite avatar Apr 25 '24 06:04 MattesWhite

@MattesWhite iterating over tokens and validating subjects are additions we would love to have :).

Jarema avatar Apr 26 '24 12:04 Jarema