In this episode, we continue our exploration of nine essential Rust programming best practices, focusing on utilities, practical examples, and software testing. Building reliable Rust applications requires more than writing functional code. Developers must understand how to organise reusable utilities, provide meaningful examples, and implement effective testing strategies to ensure their software behaves as expected. Through practical development concepts, we explore how these practices contribute to clean code, improved maintainability, reusability, and software reliability. This session is particularly valuable for developers building Rust libraries, open-source projects, and production-ready applications who want to improve their engineering standards. What you'll learn:
Organising reusable utility functions in Rust.
Writing practical examples for Rust libraries.
Understanding Rust testing fundamentals.
Creating unit tests and integration tests.
Improving code reliability through testing.
Structuring maintainable Rust projects.
Applying best practices to real-world li
Audio and video play straight from the show's own hosting. PodSubs stores and serves no media.
Transcript
From the show. Tap a time to play from there.
Speaker 1Here are nine Rust best practices I'm using when building a library. In the first episode, we saw the first three. And in this one, we will talk about the utils best practices, including the extension trait pattern, examples, and tests. And we'll still use GenAI as a real example. Okay, so first, let's talk about the utils. So for utils, I'm using two terminologies. The first one here is going to be utils. And that is going to be for things that are shared outside of the crate. So for example,
Speaker 1that will be something like source utils mod.rs. And then I will put all my utils below that. And then typically in the source, the mod.rs, I will have a pubMod utils, which we can go down. Now I'm going to talk about that for libraries. If we can avoid those, that might be pretty good. I'm going to talk about that later. But one place where I use it a lot is when I have a multi-crate project, a workspace, a cargo workspace. Then I often have crates, libutils, lib.rs.
Speaker 1And then I put within the libutils all of the utilities that I want organized by function, base64, uuid, and so on and so on. And those I can reuse them in all of the other crates. And that is very useful because first it removes some of the common code and also it normalizes behavior across all of the crates. So for example, when you are using UUID, you want to use it one way and you don't have to have expose a whole UUID crate to everybody. So that
Speaker 1is a nice way here of doing this when you are multi-crate. Then the other terminology I'm using is support. And the two have the same functions to provide utils to a code base. And that is private to the crate or the module. And in fact, it's more about the modules than anything. And then obviously when it's a root module, it's for the crate. And then the example of that will be, and we're going to see in the GenAI here how it works, is we have the source support.rs. If it's small and I can put everything
Speaker 1in one file, or if there's more utilities, I have the module name and then I have support mod.rs. So that's the two option here. depending if it's just a couple of functions, or if it's more, then I put it and organize it like that. And that is really private to the crate of the module. So one example here, probably we can have a source adapter support.rs. Or it could be this one as well, obviously. And that will only be accessible by my code design inside the adapter.
Speaker 1So like this, I can have different levels of utilities in a way, and when I see support, I know it's a subnet space support of whatever the module it is, and it's private to this module structure here. So I start here, and then everybody below can use it, but people above cannot use it. So let's see how it works by example. So if we go to our gen-ai lib.rs, we see that the first thing that we have is a support here. So I have a mod support, And that means that that would be private to where it
Speaker 1is defined, which is the root of the lib.rs, which means that everybody below the root can have access to it. So as long as something within support is public, then it can have access to it. I used to have also a PubModUtils here to follow the pattern that I was discussing before, but typically for libraries, we want to avoid that because in many ways, what our library is about is about being a utility for something. So having
Speaker 1some utils here, PubUtils, will be a little bit odd. And this is why I use two different names, because just by looking at my library, at my code, at the code structure over there, I can see that I have support, so that is my internal cooking, my private utilities, but I don't have a public utils. Now let me show you what I had before and how I changed it and why. So in JNI version 0.1.5, I had this pub utils over there at the beginning, and I have my utils over there. And that again, this means
Speaker 1that utils is at the root, and by my convention, this should be exported. And if I don't want to export, I put it in support. That again is the best practice. Again, some other module can have support over there. We'll see that later. But the point here, is that here I decided to put the PubModUtils because I had this kind of printer, chat printer, which was not really a core function of chat and everything. It's more like utilities for debug, and I didn't want to have a feature or whatever. So that's why I did it this way here. And
Speaker 1so we can see the function here was just printChatStream, and then those guys, and I think I flattened them out over there. And I cherry-picked them to make sure that it was clear what I was exposing. And that was fine, but it was a little bit kind of a stretch because now I follow this potential anti-pattern of trying to have a utils. It was not really needed. So now in the latest version, 0.16, 1.7 and on, I remove the pub utils, so we don't expose that, and I put it inside the chat. And I have the chat, I have printer over there, and then I
Speaker 1have the mod printer. And this one, I'm not flatting it out. I'm just leaving the printer like that. So when I think that UTR is kind of the exception for libraries, it might fit in some case, but it's more the exception than the rule. I do think it's a very good best practice for when you are doing a multi-crate project. Let me explain. So I'm going to show you the awesome app, the future awesome app, which is going to be open source, mid end of August. And this is a multi-AI provider desktop application written in Tori.
Speaker 1That's pretty cool. But the point here is that because it's a relatively big application, I like a lot the multi-create approach, and that allows our code to scale very well. And for those type of application, there's always kind of a crate that I have, and I often prefix the one with lib. It's a little bit redundant, but it's very clear when we import. And I have often, if it's not all the time, a lib-utils. And that is the base utils that I'm going to reuse in all
Speaker 1of these libraries. So for example, I didn't put in utils the thing about the RPC that has its own library. But things that are more low-level and basic, I put it inside lib. So for example, if I go into lib-utils, we can see that we have the things for base64, environment, handlebars, some macro utilities, then we have our trait extension pattern here that I will explain later, and then our UID. And if we go to lib, I re-export them, I don't flatten them out, because I want them to be
Speaker 1mostly usable independently. So I don't even have a common error. And in many ways, one, for example, if I go to base64, this one has its own error. And then because it's so small, I don't want to have a folder with mod.rs and error. So I have the function over there and I have the error over here. And that allows also for me to normalize the type of errors that I want to support or to express in my application and to not be too dependent into the
Speaker 1libraries that I'm using. So here I'm abstracting most of it here. That's one reason why I do that. And usually I like to have all of those being relatively independent on each other. And so for example, if I go to UUID here, I decided that for my application, when I create a new UUID, I want UUID v7 of now. That might not work for everybody. And the API of UUID is very cool because we can do whatever we want. But I don't need to expose this flexibility everywhere. I want to normalize it. Now I have
Speaker 1cool kind of macro here. I have another naming convention, which is that is macro to help derive things. A little bit of a hack, but for big derive, that can help a lot. This is not flexible, so I'm not saying it's the best practice. I'm still experimenting with that. And then I have some macro helpers here for my S string here. I'm not very proud of this one, but I have that until they have it. And then this is kind of a more experiment for option. I'm not saying that that is good. But the cool things about this is, again, the libutils, it's an app, so
Speaker 1it's not going to be exposed outside. And it allows me to experiment and have my own little code here. So that is a way that I'm doing the lib utils in the context of a multi-create. So that works pretty well. So now if we go back to our genii, we have now the printer over there. So that is, we don't have utils anymore, which is good. And then we have our support. And within our support today, we just have two things, which is a mod.rs that just
Speaker 1include the value ext. Usually when I have a support at the root, I start directly with a folder kind of things because I know that that would probably grow. Right now I keep it simple. And then here in this case, we can see that we have a support only for the adapters modules and submodules. Even inside adapters, I have again a support and I know by my convention that that is only for the adapter adapters module. So this one is this way. It's always like that.
Speaker 1I never flatten them out. and then within it, I decide what I want to do. So in this case, I have this little type here that is just used into this one. I could have created a module, streameroption.rs, that will have worked as well. But in this case, I wanted to put everything in the same one here because I have this other type. And so I didn't want to have too many little things over there. So typically that if it's utils or support, They are always structured the same for what I'm concerned. Just that one
Speaker 1is more across. crates and then the other one is just within the module. And then after is a very cool pattern in Rust and that is the extension trait pattern. And before going there, let's understand what is the extension trait pattern and why it can be very useful. So first to understand what is an extension trait pattern, let's have some terminologies. If I start a type with my, that means that is within the same crate. And when I'm going to start a type with author, that would
Speaker 1mean that is outside of the crate. So far so good. So now we can do three things, and there's one thing we cannot do. First, obviously, we can implement my trait for my type. So that is obvious. The second thing that we can do is implement other trait from the standard library or from another crate for my type. And that is what we do when we implement default from and all of that. So that is a way
Speaker 1that external crate or the standard library allows our type to fit into their model. That is very useful. And the third one that we can do is we can implement our trait, my trait for other type. And that is what is called the extension trait. And that is because we're taking an external type and then we're extending it with some of our behavior. That is very cool here and specifically cool for utilities. And then for completeness,
Speaker 1there's one thing that we cannot do, and that is implementing another trait for other type. And that is because in this case, in these three cases, the compiler can always disambiguate who is implementing what for whom. But in this case, if two external crates will implement the same trait on the same type outside, we wouldn't have a way to disambiguate things. So that wouldn't really work. So that is a concept here of implementing trait for other things. This is why sometimes we have to have the new
Speaker 1type pattern. But in this case here, in the extension trait, we have our utilities as my trait, and then we can extend an external type. That is very nice. And there's no other way in Rust to implement another function on another type, but by defining our own trait. That is a way to do it. Okay, so now we can go back to our Gen AI and we're going to get into our value extension. The way that I like to do it is the type that we're going to extend with ext. So usually that is something that
Speaker 1is relatively well accepted in the Rust community. So that is common. And then in this case, I like to have the matching file name, which is obviously with the underscore ext.rs. So far, so good. Sometime I can put multiple extension trait inside the same file. That's maybe more rare. And sometime I have multiple implementation of the same extension trait. And for example, I can have a ref string, string and ref str implementation of the same extension trait. Now, my second best practice is when I define my trait
Speaker 1is a prefix of my functions. So I'd like to have this x underscore prefix. This way, no matter the name I'm choosing, the likelihood of conflicting with either a function name of the type itself or another implementation of a trait is close to zero. And because it's my internal cooking anyway, so usually I use that only for things that are in support or my lib utils when I do multi-create, then I know that when I prefix the things with X underscore, that is my little utilities.
Speaker 1And this way also it helps when you auto-complete and it's very nice to see if you have implemented this trait extension on this type. Now, obviously, because of the way that the trait implementation work, even if you have a conflicting name, you can disambiguate it with some notations, but then the code becomes kind of very verbose. So obviously the value extension will need to be imported, but usually the IDE, VS Code, Rust Rover does that pretty well. And then I can use my own function. And then depending on the use case, I have the reference of self.
Speaker 1I try to get reference of str when it's just a string. Then in this case, I'm taking a value. So I'm going to take a reference of mute self. So I don't want to have it consuming here. It would not be as fluid of having a ref mute of something. And then also I have a cool insert, which would get a ref mute of self with a name and a value. And then the get is basically a little wrapper on the get that also do a deserialization on of value T. It's just a convenient. The whole thing is to be convenient. And then I also
Speaker 1have another one, which is my x pretty, and that allows us to make a JSON value a pretty string. And that is useful for test, or when I do the print debug and everything, it's actually pretty useful. So I annotate it, allow unused. Usually I try to avoid those, but in this case it's fitting because I don't want that only for test. I want to be able to use it and have some debug print in some places. So that is my extension trait definition. Now, on the implementation side, of our trait extension, some of the implementations
Speaker 1can be very simple. So for example, this one, the xget is almost like a get, but has a couple of benefits. So for example, in this case, I allow path to be just a property name. So that would be this guy. But if it starts with slash, it will use the CRT JSON pointer path. And so I didn't have to re-implement that is JSON pointer spec. And that allows me to plug a value deep inside the JSON value. That is pretty neat. And then once I have the value, the other benefit is that it will
Speaker 1do a set a JSON from value. And obviously the T that it returns will need to be deserialized on. So that allows me to get a value out without having to do this kind of things. And the way that I use it often is, for example, if I have my data, which is a value, and I get it from a request, I can have my let total tokens, and then I32, I give the type, and I just can do a data xget, and we'll have the autocomplete. If I have the autocomplete, but it doesn't import, then
Speaker 1I can import it with just command dot, so it's very simple. And then I can say, well, it's actually deep inside, so I'm going to say usage total tokens. And then this one is an error, that can be returned. And then that's it. Just by doing that, I save this couple of line and that makes it much nicer to work with. Now, next cool example is we have the take, which is a little similar to the get, but try to avoid to do a clone. So it's used the value take implementation. So I didn't redo this implementation, but the same logic and can pass a pointer or
Speaker 1the property name. And then I will translate whatever value I get to whatever type is requested. That is the same thing that avoid to do a clone. Now, one thing I might decide later, or as a team, we might decide to say, well, we don't want to do the value take, because value take, what it does is it takes a value and put a value new where it was. So it doesn't remove the property from the parent, from the container value. So perhaps the strategy that we want is find the container. So that would be mute self,
Speaker 1if it's a property name. And if it's not, it's a parent of the path and then just remove the property altogether. So that would be a different implementation strategy. But in our case, we know that we don't really care to know if the property name was there with a value new. It's not something that matters anyway. So that would be an implementation details that none of my other code needs to worry about. And then I have a cool one here, which is the X insert, which allows me to insert in refme itself, obviously, the name, which also can be a path. So that should be
Speaker 1renamed path to be consistent. And then the value, whatever value, whatever serialize into T. I like to use generic in those functions because there are utilities. And yes, it would generate a little bit more code. But if that become an issue, I can move all of that into an inner implementation and then just do the into there. That would be valid as well. But the point is that from an API standpoint, this is very flexible. And then again, I'm supporting the fact that it's just a property name, or it's a path. And then I have the whole
Speaker 1logic here that gets a path, parents, create the parents if they need to be created, and then set the value. That is actually pretty neat. And the ways that I will use that, I create the payload, that I'm going to send to the AI provider. I have some logic to get the messages and so on. And then I say, if streaming, then I can do a payload, X insert, and then streaming true. Or obviously I could say streaming because that is a Boolean. And so in this case,
Speaker 1I decided to put the streaming true only if there's streaming, otherwise I let it by default, but I could also do something like that. And that will work also very nicely. So this little API here, xget and xinsert, make working with JSON so much more convenient while we'll use the full power of the cell API. And so that is what I like with a support name and this kind of things is pragmatic first and then elegant second. Okay, so that we conclude our utils chapter. And now we
Speaker 1can talk about our examples and our test best practices. And in fact, they are relatively related. Let me explain. Okay, so the way that I see it is that the examples, obviously for documentation, but it also gives the opportunity of testing the API ergonomics. And then the tests that are unit tests or integration tests are going to allow us to do the testing of the API implementations, the behavior. So in many ways, this is testing the facade and
Speaker 1this is testing what is behind the scenes. And obviously the example here, one of the key things is documentation. And often it's the entry point of a library. So usually when I learn a new library, I like to go to the examples and then see the code there. And that is often a good entry point of how to use it after the readme. So in my library, what I like to have now, I like to have little storyboard like that, where I have C for chapters, can be other things. And I have a number padding like that. And sometimes I might have a dash and
Speaker 1another number here if I want. This is a readme, so I make sure that my readme that I have over there, the example always compiles. That is a nice way to not have that out of sync. So I just copy paste, but at least I make sure that it compiles and then people can run the readme like that. And then I have the one for conversation to customize the authentication for the adapter kind, the chat options and the model names. And in my readme, if we go down here, we have the examples. I copy paste the
Speaker 1C00 readme.rs. And then I have the more examples where I'm going to describe a little bit each library and their goals. That is the way I do it. Now that doesn't have to be done like that. As long as there is good example for the common use case, and with whatever consistent naming convention you have, you are good to go. Now, the second best practice that I have is that I don't use unwrap. So that again is what I explained in my Rust error. Best practices, I don't use unwrap and I do not use as
Speaker 1well anyhow. And so that is for my example as error, but the same thing for my test errors will be the same thing. I use a result void and then the boxed in error pattern. And that gives me as flexible almost as anyhow, but then it's progressive to my production error way of managing things. And the reason why is because unwrap, if you have one unwrap left in your code when you copy paste or whatever, then your application will actually crash. And the whole point of Rust is to build robust code. And so if naive mistakes
Speaker 1end up by crashing a thread somewhere that we didn't realize for a while, then it can be a big problem. And anyway, it's a bad pattern because it's not the way to do it in a production code. So what I like to do is having the result and I don't do type alias or define anything in this case, I just do main result void of boxed in error. And now I can use that as much as I want. And usually most of the question mark. So if we go there, we have the await and that will work in a seamless fashion. And then even for the OKR,
Speaker 1I can have OKR and give a static string or a formatted string, then the question mark and everything will work. So it's very flexible to use this panel. That is what I have. And my goal is to try to be as instructive as possible and to show the flow. So in this case, I have the flow, which is a question, the chat request, the client, the print option to be able to print something. And my goal is that the user should be able to go to the terminal and do a cargo run dash dash example and
Speaker 1then C00 readme. Yep. And in fact, when I'm coding, I have a cargo watch like that, where I do the quiet, the clear, the exec, and then I do the run, the quiet, my example like that. And that allows me to go very quick and to be able to have a feel again of the ergonomic at the beginning of the development of a library. And in fact, I'm going to say something here that perhaps some will disagree with, is that at the very beginning of a library, I focus more on the ergonomic of an API rather than testing the behavior. that Mac looks a little bit crazy.
Speaker 1But the point is at least to have the core of the flow of the API that I want to have and to have it right. And for me, the examples, because I put myself in the head of a user of the library, it allows me to really focus on the ergonomic of the API. Then I make sure it works. And then eventually, obviously, I write my unit test or my integration test. And for all of this here, I have some alias, the sh alias on Mac, such as I don't have to type this long command line. So for example, here in my ZSH file, I have a
Speaker 1function like that, that I can call and I will just do, for example, in our case, I will do a CWE C00 with me. And that will start everything and we'll do the quiet and then the example and so on. That is what I like to do. And sometimes I just do alias when it works. This one I had to do like that. So it works better as a function, but you use it the same way. And then that's it. So that is my conversation here to have a conversation. with the AI to change the auth resolver, make a custom resolver for a specific adapter,
Speaker 1config here for OpenAI, and then the kind, the chat option, and everything. So I have all of this there, try to add comments, can always be better, but some is better than nothing. Okay, so that will conclude the example side of things. And now we can go to our test best practices. So in Rust, we have two type of tests. The first is a unit test that we put inside our source files, and that allows us to test the public APIs as well as the private APIs
Speaker 1and private modules. And that is very powerful because now we can test the inside of our implementation as deep as we want without making anything public, without doing some weird contortion. And then we have the integration test, which goes to a folder which is named test with an S, and then we put our files inside. And that allows us to test from the outside, so the public APIs in this case. So now the way that I like to have it for the integration test is I like
Speaker 1to have all my files like this, my test. I have my support as well, sometimes, often, which allows me to give some helpers that I can reuse there. So that allows me to have my test code much clearer because now it doesn't have to repeat many of these common things. Then all my test files start with test with a S underscore and what it tests. In this particular library, because I have the concept of providers, I decided to have a prefix like that, which is P
Speaker 1like provider. So like this, all of my providers are organized like that. So that is the same type of test. And that is a provider that I'm testing. Because later I might have other tests that are not really provider related. So if we go, for example, to the OpenAI implementations, We see that we have our test chat symbol. Okay. Test chat JSON. So I prefix with test. Those are Tokyo tests because they need to be async. If you don't need it, don't put Tokyo test. You just put test. And then here I'm calling my common test. Usually I
Speaker 1don't have that for other type of libraries. But in this case, by design, there's the same logic for all of test. the provider, and then the only thing that changes is the model name. So in fact, if you look at this one, we have those guys, and then we have the same thing for Olamar, and so on and so on. And the reason why I'm doing it like that is because when I do the testing, when I do the implementation, I do it provider by provider, and then I check that it works on everybody. So with cargo test, we can do cargo test, and then
Speaker 1we can do a test, and then we can give a first filter, which is going to be the exact file name. So that will be our test P underscore OpenAI. So that will run everything. So usually I do that to develop that. And then we can do a test. You are going to see that later. The prefix of all the function I want to run, all the function exactly. So I do a test chat, and that will test all of the test chat inside this file. or I do a test stream. Or if I want to test
Speaker 1a specific function, usually I have something like that, which is simple, okay. And in fact, what I usually do is something like that where I actually don't do a cargo test and go cargo watch, dash Q, dash C, dash X, and then I do that, then I do the no capture here. That is what I do. And in fact, I don't type that all the time, because that would be silly. I have that SH function that allows me to either say CWT and my test function. So that's good to go across all files or unit
Speaker 1test or whatever. Or I do a CWT with a test file name, which is actually test with S usually. And then is the prefix that I want to test. Or if I just do a CWT, it will do a cargo watch on nothing. And in all of these versions, it's going to do the no capture. And the new capture is very useful because what I do is I do a cargo wash test on something, and then I do the print, print, print. I make sure that everything works fine, and then I change the print to asserts, and then I'm okay. That is the way that I do the whole workflow, and it works like a charm.
Speaker 1And this function, honestly, I change it over time. There's probably a smarter one to write, and I use a lot of chapgpt or llms to help me creating that because I have no clue of what is that, but it works well. This one, I guess, is the one up here. That's the only thing I can see. Okay, so that is the way that is organized. Now, if we go and look at the test here, we're going to have the import and so on. And then here I do the mod support. So the thing which is important in Rust is that every file is their own
Speaker 1crate in a way. So, or their own binary or whatever. So you need to re-import the mod support. And here in this case, only for, again, this library, because I have this common test. That is not something I use for other libraries, but in this case it's fitting. So I put the common test that I'm repeating all the time everywhere, except the model name, inside there. So this way I can just have the function. Then I do the result best practices that I have with a boxed in, all very flexible and everything we need. and then I
Speaker 1have the model. So all of them will look similar to that. Now the implementation, in this case, what I do is to code test because this needs to be async, and my naming convention is test underscore what I want to test, and then the variant of what I want to test. And then am I testing the okay or the error, dash error at the end? So in this case, I do the test chat, simple, just simple. And I do the test chat JSON, and both of them are okay. And in this case over there, I do the test
Speaker 1chat stream, simple, capture content, capture all. And again, I repeated that in all of those because all these libraries, that makes sense. And so that means that all the implementations that I would have put there is actually in this file. So if I go there, I see all my chat test over there, and then my chat stream test. That's the way I organize it. And again, the content of this function should have been there if I didn't have this pattern, but again, for this specific library. So now how do I organize the content of my unit test?
Speaker 1So I have in my comment, I have region, that is a code region of VS code. And then I have section like that with a dash dash. This is just best practice. And then the first section of a test is always a setup and fixtures where I define the variables and I set up things and I seed the data. So often I have a seed underscore that I'm putting in my seeders. When I start the first test, This is just one. I do it inline. And then eventually when I reuse it often, then I put it in my seeders. Then I have a section where I
Speaker 1actually execute whatever I'm saying that I'm executing here, that I'm testing there. And then I do the check. And now it's a cool thing because when we did the cargo watch and so on, what I have at the beginning in my check is I have actually a println. So I use a Rust Nx base code extension. I have a little debug print and I prefix my debug print with that this way I can always search them across everything and see if I have any leftover and then I delete them because I make sure that they're not in the commit. And then I can do a chat res and that was a cargo watch is amazing
Speaker 1because now I can just code over there. I can just do a cargo watch, the CWT of the test, the function, exactly what I want. And then I can go there and just work on the adapters that I want. I press save, save, save. I see the print, print, print. And then when I'm happy, then I put the assert. And obviously when I'm debugging, I either keep the assert or I do a new section and I'm going to say debug print. And then I put my debug print over there. So that is the way I do it. And it's very fast to develop because very iterative. And
Speaker 1then you don't have to worry too much about threadings. And if you develop something, even base and everything, you have the same practice for everything. That is what I do for most of my functions. So this is what we have for test. Simple. For the JSON here, we just test some token. And then we test the content to make sure that it's a JSON content. So that is a mod JSON. Now, sometime in this case, I take another variable because Olamar has a bug right now
Speaker 1where if we do a mod JSON, it don't send back the token. It's no big deal because it doesn't crash anything. But the check that the usage.totals, that would be none. So that is one thing that we have done here to make sure that we can have in our test OpenAI be a little bit more flexible here. So that is basically the practice that I have in this kind of things. The helpers, we have some macro rules That is an ugly one. It's not necessarily a good one, but it's one that allows me to get
Speaker 1the property of a struct, assert that it's not known. And if it is, it's going to throw an error, which is going to be a should have. And when we stringify the field, that's nice, such as we don't have to do always OK or everything. And then again, because we use boxed in, it's super nice because that will work very nicely. And then it will return the value that we took. So that is just ugly, convenient things. And again, it's support is always private. So don't feel
Speaker 1bad about doing pragmatic, ugly code. We can clean it and make it elegant later. And those are convenient helpers. So I'm using helpers here, results and utils and support, because that is just a bag of other things. And then I just extract the stream end, which is something I use a lot in the common test. So that is what I do. And then the last one that I do is kind of the seed pattern. And again, it's more like, it's not a pattern. It's more kind of a convention where I have a seed chat request. Simple. And that I put the seed, all of the seed there.
Speaker 1And I use that a lot also when I do database testing or whatever. I have my seeders such as I don't have to repeat that everywhere. Okay, so I think that that is going to finish this one. Actually, one little last thing that I wanted to say is that sometimes for values, I prefix them. So for example, if we wanted to test the result, I would do an FX return total tokens, for example, something like that. So when I do the FX, that is a fixture and that is something that I can use below here
Speaker 1to either create something and then check it or whatever. Okay, so that was pretty big, but That kind of concludes the integration testing. And now we can go to the unit testing, which is basically the same kind of pattern. We're going to have the setup, the exec, and the check. So that doesn't really change. It's where we put them that changes a little bit. And to be honest here, I focused on the integration test for my Gen AI, and it's still early in the development, and not really on deep unit tests. There's two strategies in unit tests,
Speaker 1or testing in general. It focuses on the percentage of coverage, coverage percentage. And you say, obviously, higher percentage is obviously better. So let's go and write everything. And if you write a function without a unit test, then you're not a good developer. See you later. That is possible. The other option is to say, well, let's be pragmatic, moreover, at the beginning, when we are going to do this smart coverage. It's not about covering all the functions. It's more all of the paths that are likely to break or that needs to be revalidated all the time. Now, that being said,
Speaker 1there is some unit test here that I should be writing, that will be probably one of the code here, so it might be already there, is in the utils. And specifically on this one here, where the take and then the insert, there's opportunity of quite a bug. Like this one cannot really fail. So if I go there, This one is so simple that it could fail here, I guess, and fail here, and eventually it should be tested. I'm not saying it shouldn't be tested, but I wouldn't write
Speaker 1the xget first. I will use shabjpt to write it. But what I want is to test those two, and especially the insert. And so the way that I would do that is I have my literal convention here, so I still have the things. And that is very familiar to us. And I'm going to use my VS code, Rust Nx extension, which is start everything with one. And I have the test module, I think, something like that. I'm going to test the 01. And that basically does a config test. So that is the way in Rust
Speaker 1to do unit test. And that will define inline a modded test with a S. We could put it in a file. It would be the same thing, or we can put it there. If it's too big, might be a good idea to put in a file over there. But usually you inline them inside the code. And I like to put them in a section such as the whole thing get collapsed. But that is just me. Then I still have my best practice here where I have my error type alias and my result. The reason why I don't have that, that would work also very well
Speaker 1because the only thing we care is result of void anyway. We don't care about the box din. But sometimes I like to do the use super star. Then that might include as well the error on the parent. one. And so this way I make sure that everything match very nicely because that will override it. And then when I do the error, the result will match this error and I won't have a bad surprise. So that's why I do it this way. And then as a function, very similar to what we had before. So I'm going to
Speaker 1go there and I have in my Rust 10x VS code extension, I have ATF, which is basically the async function with comment. And that will give me the things I will do a test value insert, okay, for example, and that here will do let I might do a FX value perhaps, and then I will do a JSON. And in this case, I give a JSON and I can say, you know, token, I don't know why I say token, but three, and that will create a value.
Speaker 1And then because we have the JSON probably in super, that's why I didn't complain. And I like to have the use super. So like this, I don't have to think too much on stuff. And then in this one, I would do a FX value. I need to make it mute, obviously. That wouldn't be FX in a way. That would just be value to be correct here. And then I would do an X insert. And that will do that. And I will do a happy world. And then on the value, I will do something like hello.
Speaker 1Press save. that works that is probably because the trait is added above there's a clippy that complains it's probably because we don't have the question mark and in fact what I would do That is how we do the fixture. We do fx, not value, I guess. And I would do yellow. And then I would take that, that there. That should work. And then I would check it. And now the question is, do I use the xget or do I actually get it manually? That is a debate on both sides. But on this one, I'm going to
Speaker 1make it easier. And I'm going to say actual value equal. And I want a string. And I'm going to do value equal. x get, and then happy world. In fact, I'm going to do like that. That should work. And we're testing quite a bit here because we're doing the insert with adjacent paths with that, by the way, won't work like that. And then we're doing this one there. And I will do an assert equal, and then I will do the actual value probably as str. And then
Speaker 1I will have the fx node value there. That should work. Yeah. Then I will probably check the tokens as well to make sure that it didn't get removed, for example. And then the way that I will do that, it will do a cargo watch test. And I'm going to do that. This hopefully will work. This one test that pass and this one. So the problem of CWT is that it will test everything. So you could write something else or another kind of run to test only the unit test,
Speaker 1such as you don't have to test and see everything else outside. So that could be a good one as well. And you can make the CWT better for that. But here, that's it. We passed it. And the cool thing with the cargo watch is if we do this and we want it to say one print value, and then we do the stuff here, press save. That will recompile and restart it. We see there, we're happy. We remove that. We have the assert and everything works fine. So that's it.
Speaker 1That is my process. And it's actually very efficient. And actually this one, I can commit it. So the way that I will commit this one, I have an alias as well. So I have a lot of alias because I don't like to type. I don't like to type things long. I will do a git commit. That will be a git commit dash a dash m. I have a best practice to do dot because it's a minor. I will say unit test. And it will say first xt insert. And then that's it. That will give me that. And then I check that it's there. And then typically
Speaker 1I do git push origin, oranges. And then that's it. So that is we did actually a commit live. And that will conclude this episode. In the next episode, when to async, when to do sync, project files, and versioning. Big thanks to Crabmebula for their sponsorship. Until next one, happy coding.
Building production-ready web applications requires efficient database interaction, flexible query construction, and well-structured data access layers. We examine how these technologies work together to simplify...
In this comprehensive tutorial, we explore how to build database-driven applications using Rust and SQLite, covering essential database concepts and practical implementation techniques. We take a hands-on approach to...
Join Jeremy Chone for a live-stream session dedicated to Rust programming, software development, and practical engineering discussions. This session provides an opportunity to explore Rust development through a live...
In this episode, we explore data access in Rust web application development, focusing on how backend applications interact with databases to store, retrieve, and manage information. Data access is a critical component...