Now that we have our chat protocol set up, let's start working on implementing our client.
Our chat is going to read from Standard N and then send it to the server.
So let's start to add this logic to our client file.
So client dot press.
So as usual, the first thing that we want to do is add in a couple of the use statements of dependencies
that we're going to need.
So we'll need async and then everything inside the prelude, we'll use chat, utils, self and chat
result.
Now if you named your project something other than chat, that's what you will place there.
And then we're going to use async, standard, IO and net.
And now we know that the client is going to send messages.
So we are going to create an asynchronous function called send that will accept one argument, a mutable
TCP stream, and then the function will return a chat result so we can set that up async send.
Mutable send variable of net TCP stream and we're going to return a chat result.
So our chat is going to have a few options and we want the user to be aware of them so we can just go
ahead and print them out.
So we'll say print line.
And we will have our options and we'll have join chat.
Post chat message and then.
To exit the chat.
You'll do control D on Unix or control Z on windows to close the.
Connection.
All right.
So now that we have that out the way, we are going to take the standard input and then wrap it into
a buffer reader so that our data is now contained inside a buffer.
So we'll say let mute options equals IO buff reader for buffer reader a new one on the standard n.
Lines.
So we call lines so that the users input is processed on a line by line basis, since it is processed
line by line.
We can start setting up logic to parse each line and match it on a value in our client enum.
We will want our logic to send the value to the server if it's successfully matches.
Otherwise, we want it to generate an error and continue in a loop waiting for the next command.
So let's start working on that logic.
So why let some option result equals option?
Next.
Oh, wait.
Let opt.
Equal option.
Result.
My.
Have been in air and now we will say let request.
So our IQ match pass input on a reference to an option.
Some requests where we return our request or none.
Where we continue.
And then we'll say Utils.
Sinned.
And what did I.
Name my I have not sinned.
We'll say sinned, Jason and mute sinned and request dot await.
And then send flush.
Dot.
Oh, wait.
So we have a lot going on in here, so.
We created a function in here called parse input.
This function is going to allow us to get the values our user passes in and then match on them so that
we know what we want our user to do.
So let's set up that function.
But before we start setting up past input, we're actually going to need a helper function that is going
to give us our values and take out the white space accordingly.
So we'll call this function get value.
So let's, let's set up get value first.
So we'll say get value mute input reference to a string slice.
And then we're going to return an option with references to a string slice.
So we are returning to string slices inside our option because we want the command such as post message
or joint chat, which will be the first value we're going to get from the user.
And then we want the remaining string to be the to be the rest of the string that the user passed in.
So this is actually a pretty straightforward implementation.
So we'll say our input equals to input dot.
Trim start.
And if we hover over this, we can see what it does.
It's going to return a string slice with leading white space removed.
Great.
That's what we need.
If our input.
Is empty.
Then we want to return on.
Otherwise we're going to match on input.
Find
char is whitespace.
And then some white space.
Overturn some.
An input from the beginning all the way to the white space.
Like how it did that.
There you go.
And then the remainder is going to be input from white space.
To the end.
And then in the event of none, we returned some.
Input.
With nothing following.
So this is just going to allow us to get the first value and our argument.
The user gives us and then just start.
Breaking down the command that they are putting in.
So now we can start implementing our parsing input.
And these two functions will make more sense when you actually see the arguments that our user is going
to put in to our program.
And then now we will create a tuple which is going to be our input and our remainder, which is why
we returned back.
To.
Reference to string slices.
And now we will say, get value of our line.
And then if input equals join.
So remember that.
Is an option for our client.
So if input equals join.
Let chat.
And then we'll say remainder.
Equal.
Get value on the remainder.
And then if remainder.
Not equal trim.
Start is empty.
Return?
None.
Otherwise.
Return some client
join.
And now we set up our join and we have our chat name.
Which is going to be atomic reference counter and then chat dot.
To string.
And then else if input equals post.
So if you can kind of see what we're doing here now we're handling if it's a post given to us from our
user.
Now we're going to handle that logic.
So we'll say Let chat.
Remainder.
Get value remainder.
And this is going to be very similar to what we just did.
So we'll say let message equals remainder dot trim start.
To string.
And then we're going to return some client post.
Chat name.
Is atomic reference counter to a new chat dot to string.
And then we want our message.
Which is atomic inference counter to our message.
And now otherwise.
We must have some kind of input that we were not able to match on.
So we have unrecognized input.
And lets print out whatever the user gave us and we will return none.
And then just to make this pretty.
Oop.
Expected one of the following.
So online.
12.
Let request equals and then now line 35.
If not in.
So now let's take everything.
Line up.
Good.
So awesome.
So we have in here.
A call to utils.
Send JSON.
But we haven't implemented that yet.
So let's begin doing that in the next lecture.