So now we can go over to libraries and begin creating our chat protocol.
We want to implement going from the client and from the server.
So we will create two enemies, client and server.
But the first thing we're going to do is add in a couple of use statements that we're going to need.
So we need to add in Saturday and in here we want de serialize and serialize.
We also want to use standard sink and arc and then obviously bring in our utils.
So now we can create our two enemies.
So we'll have our client enum.
Which is going to contain join.
We're going to have a chat.
Name of arc string.
And then we're also going to have a post which is going to be chat name
and contain a message of an arch string.
And then there we go.
And now we want our server enum,
which we're going to have a message.
Which will have a chat name.
And then obviously we'll have a message.
And they will have an air
containing a string so we can make this a little bit more robust format using cargo format, and you'll
see that it'll automatically do it for us.
So now let's.
Test out our set up real quick to make sure we are sending and receiving messages in the JSON format
like we wanted so we can run
test and then we'll say test our client.
And then in here we'll add a use statement.
We'll use.
And I will say let client equal client post.
And then inside here, we're going to have our chat name, which is going to be a new atomic reference
counter and we'll say string from.
We'll just call it chat and then we'll have our message, which will also be a reference counted string
from message sent.
All right.
And that and I will create our JSON message using Saturday JSON to string a reference to our client
and then we will unwrap it.
And now we will assert equals Jason.
And now we want to do create a JSON formatted post manually.
So we'll do that right here.
And it will be.
Post.
Chat.
Name?
Chat.
Now we have our message.
Which is a message sent.
Now we will add.
There we go.
And we will also do assert equals.
It's Saturday json from string.
So we turn it back from a JSON value.
Client Jason Dot unwrap and then make sure it matches our original message, which we created here.
And I want this to be a reference.
All right.
So we see we have some squiggles here.
So it says the trait bound client serialize is not satisfied.
So luckily this is something the compiler tells us and it's actually a really easy fix for us to do.
And it gave me all that.
So now we want to derive a couple of traits.
So we want to derive debug just in case we want to print anything out to the standard output.
We want to derive the serialize.
We want to derive serialize and since we're using assert equals, we actually also want to derive partial
IQ for equality.
That way it knows how to handle our assert equals in this case.
And then I need to also add in.
Collins right there.
So now, if all this works correctly, we should be able to run cargo test and it will compile all of
our dependencies for us, which will take a second.
But then once it's done compiling this.
And it runs our test cases.
We should see that these outputted successfully that they ran okay and it did so we passed our test
client which is great.
So now we verified that we are creating a proper JSON from our message because we explicitly created
a JSON formatted output of our message.
But then we also made sure that when we want to go from JSON back to our string, it matches the original
message which we were able to confirm in this case.
So now that we are done with that, we can begin working on setting up our client.