I decided to use Rust for this blog post. But why?

code
Rust
Quarto
struggle
Author

George Girton

Published

March 4, 2023

Workflowy-pipeliney

I wanted to automate a new blog post slightly.

I usually know the image I want to use, and put it in a particular place. But then I always forget exactly where it should go. In the root, or in the folder (the folder). And by the way, where did it come from to begin with? Ideal for an environment variable.

Then, I wanted it to write the front-matter yaml for the new blog post. Into the newly named folder it would create. Also, I did not want it to mess things up. So …

specs:

  • create the folder, with a name I give it.
  • Write the header into ‘index.qmd’, with the Title, Date, Name, picture filename, and draft=true
  • Copy the most recent graphics file to the locale
  • BUT don’t do any of this if index.qmd does not already exist
  • AND if you are not in the root folder that has _quarto.yml, don’t do nothin’ either

That’s all!

Rusty

there are a lot of things to like about Rust.

It WAS very quick to put together a dev environment for this little project

IntelliJ and Visual Studio both support Rust, but I just went with BBEdit and the command line.

It’s helpful if you have already installed it.

install w rustup

FIRST

curl –proto ‘=https’ –tlsv1.3 https://sh.rustup.rs -sSf | sh

THAT did not work. I guess SH must be Linux.

Instead went with

```{shell}
rustup self update
rustup updated - 1.25.2 (from 1.24.3)

rustc --version
rustc 1.54.0 (a178d0322 2021-07-26)

rustup --help

rustup update
```
```{shell}
rustup show
stable-x86_64-apple-darwin (default)
rustc 1.67.1 (d5a82bbd2 2023-02-07)
```

OK Rust was already installed due to a Rust DevTalk study group session last year, and is now updated. Thanks, DevTalk!

One of the things I was kind of sad about was Cargo. I loved working with it in this project, but when I got to the end, Cargo sort of let me down.

I had originally picked Rust because I thought it would be pretty easy to install the executable on my system when I was done, in a way that was easy to access.

‘Cargo install’, I thought, was supposed to do that. But, since the time of that original documentation, it doesn’t really do that anymore.

Fortunately I was able to go out, grab the executable, which I had dubbed new_post, and paste it into a folder near me:

‘mkdir new_post_beta’, so I could just call this with new_post_beta/new_post, answer the two questions and Hello, New Post!

“open .” to open the finder in the current folder. On Windows, you would use “start .” to do the same thing.

So, it worked out okay after all.

Super convenient to use.

finally

It would have been thousands of percent faster to mock it up with a shell script:

Scriptname %1FolderName %2”Topic Title”

And that’s what you should do instead of trying to fix this hacky Rust pgm.

One tip I picked up was how fast ‘cargo check’ is. Since it doesn’t produce an executable, you can see what compile time errors are occurring, and fix them right away.

Cargo check, cargo check, cargo check, cargo run. Yeah!

also very nice was rustc –explain E10103 and it would often cajole me to get rid of unreachable code, things that didn’t need to be mutable, etc. etc.

But first ‘cargo new new_post’ ‘New_post’ being my program’s name. Underscores are favored over run-on word combos. line_num instead of linenumber.

I set up druid with cargo, thinking I might use a gui:

druid = “0.7.0” To import Druid into a Rust file, use the following code:

use druid::{AppLauncher, WindowDesc};

I needed some time and date formatting.

cargo add chrono –

NOW WHAT ABOUT A TEST

No idea how to make a unit test for what I had already written. Code first!

One thing I noticed: All the Rust examples have ‘println!(“This is your {result_thing}”, result_thing);’ and that is like, Okay, see, it worked! But there’s almost no “this is how you return something from a function and use it in your program. Which in many cases, due to Rust’s ownership lifetimes, is not that easy to figure out how to do.

I ought to have used GitHub CoPilot to help me on that! Very next time for sure!

Crabby Rustacean!

Why I decided to use Rust for this blog post

R, Powershell, Python, … why not Rust?

R

Thanks to hard-working journalist Sharon Machlis (thanks, Sharon!), I got the idea of creating the Quarto blog folder, and the index.qmd automatically. Sharon wrote an R package to do this, and put it on github. I got this code – after all, i have Hadley Wickham’s book on R packages, how hard could it be? – and soon found that even after reading about it, I still was not able to make it work. I guess one of the thing about R packages is that the documentation requirements of CRAN (the Comprehensive R Archive Network) mean that you have to … document everything correctly!

Too bad, since Sharon had even written exactly how to use the package with R snippets in R Studio, and although I don’t use snippets, I foodled around with that and it did work great..

But I did not want to create folders with the date embedded into the folder name, so … I could not use the package directly. Plus, there was 1 other tiny thing I wanted it to do.

Powershell

Okay, I said. I’ll use Powershell.

I have written a bunch of Powershell scripts in support of the product install system testing at work, and also some partial pipelines, to get candidate release modules from one staging area to another for test. Super convenient, and works like a charm.

I thought Powershell would be good for a tool of this type, since it performed well in the “copy-a-file” test that keeps the files attributes in the copied file. Python could be used for this, as long as you remember to use copy2?

Anyway, I fired up ‘pwsh’ at the command line (and no thank you, autocorrect, I do not mean ‘push’) and got a great offer from Microsoft. “You’re using version 7 dot something or other, 7 dot a little bit higher is available.” S

Hey, “If not now, when”,right? I simply type pwsh upgrade and … no, that is not how it works. I went to the GitHub site to get the arm64 pkg file. That took a little reading, and so I downloaded that package and ran it.

This time the countervailing message from Apple. “Sorry, we could not check the validity of this package, please inform the developer”. Which would be Microsoft.

There’s a web app for that! But …

After you fill out the “What you expected to happen” and “What actually happened” and check the three checkboxes, and write a brief description of the problem (“The documentation is not correct. The downloads found on the github page give an error message when run and do not work”), the “Submit” button does not become active. It is not ungreyed. Nothing is going to be submitted.

Farewell Powershell, or rather ‘au revoir’. I’m sure we will see each other again on the bitstream before too long.

Python

I probably know Python better than Powershell, but … how does the package system work? Like, how do you deliver the code to yourself? See R, above. Note to self: try Python for sure next time. Or better yet, just write a three-line shell script, and get back to work.

Doesn’t this crab look kind of like a ‘C’?

— all photos Copyright © 2022-2024 George D Girton all rights reserved