DEV Community

Nivethan
Nivethan

Posted on • Updated on • Originally published at nivethan.dev

A Gemini Client in Rust - 11 Saving Gemini Pages

Hello! Our journey is coming to an end! We are almost done now and I hope you'll build on our client as you use it. I think there is something very powerful about being able to modify your software to fit your very specific needs.

One such need for myself is to e-mail what I find in Gemini space. I can't exactly give someone a URL! So in this chapter we will implement a save function that will simply save the current page to a text file. If we wanted to get fancy, we could have the mailing happen right in our client!

Saving a Gemini Page

...
            "save" => {
                if cache.len() > 0 {
                    let page = cache.last().unwrap();
                    let file_name = format!("/home/nivethan/gemini/{}.txt", 
                        page.url.request().trim().replace("/", "-"));
                    let mut f = File::create(&file_name).unwrap();
                    for line in &page.lines {
                        writeln!(f, "{}", line).unwrap();
                    }
                    println!("Saved {}!", file_name);

                } else {
                    println!("Nothing to save.");
                }
            },
...
Enter fullscreen mode Exit fullscreen mode

Here we have a very simple function to save the current page we're on. We get the page we're on from the cache and we create a file naming it the page's url (thought with the / replaced with -). We then write out the lines of the Gemini page into our newly created file.

Once again, you'll need to make sure the path exists!

Now that we have our client working the way we want it to, we still have one glaring piece we need to sort out. We have currently hard coded the mime types to be text/gemini. We did all our page processing and caching logic on that premise which may not always be true!

Let's fix that!

Top comments (0)