Skip to content

Commit

Permalink
chore(git): merge main into dev (#743)
Browse files Browse the repository at this point in the history
mateusfg7 authored Dec 22, 2023

Verified

This commit was signed with the committer’s verified signature.
Eliulm Elias Wahl
2 parents 4f54115 + da77491 commit 853a4ba
Showing 4 changed files with 135 additions and 2 deletions.
24 changes: 24 additions & 0 deletions content/til/2023_12_11-random_strings_in_rust.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
title: 'Random Strings in Rust'
description: 'How to generate a random fixed size string in Rust'
date: '2023-12-11'
tags: [rust,rand,string,generate]
---

For generate a random string with fixed size in Rust, we can use `Alphanumeric` trait from [rand]() crate:

```rust
use rand::{distributions::Alphanumeric, Rng}; // 0.8

fn main() {
let s: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
println!("{}", s);
}

```

ref: https://stackoverflow.com/questions/54275459/how-do-i-create-a-random-string-by-sampling-from-alphanumeric-characters
40 changes: 40 additions & 0 deletions content/til/2023_12_11-rust__sqlite.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: 'Rust + SQLite'
description: 'Connect SQLite in a Rust app'
date: '2023-12-11'
tags: [rust,sqlite,db,sql]
---

For run SQL queries on SQLite database from Rust, we can use the [**sqlite3**](https://docs.rs/sqlite3/latest/sqlite3/) crate:

```rust caption="Open a connection, create a table, and insert some rows."
let connection = sqlite::open(":memory:").unwrap();

connection
.execute(
"
CREATE TABLE users (name TEXT, age INTEGER);
INSERT INTO users (name, age) VALUES ('Alice', 42);
INSERT INTO users (name, age) VALUES ('Bob', 69);
",
)
.unwrap();
```

<br/>


```rust caption="Select some rows and process them one by one as plain text:"
connection
.iterate("SELECT * FROM users WHERE age > 50", |pairs| {
for &(column, value) in pairs.iter() {
println!("{} = {}", column, value.unwrap());
}
true
})
.unwrap();
```

[_And more..._](https://docs.rs/sqlite3/latest/sqlite3/)

**ref**: https://docs.rs/sqlite3/latest/sqlite3/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -11,7 +11,8 @@
"start": "next start",
"lint": "next lint",
"bump-tag": "tsx tools/bump-tag-version.ts",
"new-post": "tsx tools/new-post.mts",
"new:post": "tsx tools/new-post.mts",
"new:til": "tsx tools/new-til.mts",
"prepare": "husky install",
"check-unused": "npx depcheck"
},
@@ -87,4 +88,4 @@
"typescript": "5.3.3",
"vercel": "^33.0.0"
}
}
}
68 changes: 68 additions & 0 deletions tools/new-til.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { intro, outro, text, spinner } from '@clack/prompts'
import { slug } from 'github-slugger'
import fs from 'fs'
import c from 'picocolors'

intro(c.bgGreen(c.bold(' Today I Learned... ')))

const title = await text({
message: 'Title',
placeholder: 'Advantages of eating',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})
const description = await text({
message: 'Description',
placeholder: 'Concise resume of the T.I.L',
validate(value) {
if (value.length === 0) return `Value is required!`
}
})

const tags = await text({
message: 'Tags (separated by ",")',
placeholder: 'javascript, rust, css, life'
})

const s = spinner()

s.start(c.italic('Creating T.I.L'))

const padZero = (n: string | number) => (Number(n) < 10 ? `0${n}` : n)

const currDate = new Date()

const TIL_DIR_PATH = 'content/til'
const SLUGGED_TITLE = slug(title.toString()).replaceAll('-', '_')
const DATE_STR = `${padZero(currDate.getFullYear())}-${padZero(
currDate.getMonth() + 1
)}-${padZero(currDate.getDate())}`

const tilPath = `${TIL_DIR_PATH}/${DATE_STR.replaceAll(
'-',
'_'
)}-${SLUGGED_TITLE}.mdx`

const parsedTags = tags
? tags
.toString()
.split(',')
.map(tag => tag.trim())
.toString()
: ''

fs.writeFile(
tilPath,
`---
title: '${title.toString()}'
description: '${description.toString()}'
date: '${DATE_STR}'
tags: [${parsedTags}]
---`,
() => {}
)

s.stop(c.bold('T.I.L created!'))

outro(`📝 The T.I.L was saved to ${c.bold(c.green(tilPath))}`)

0 comments on commit 853a4ba

Please sign in to comment.