generated from mateusfg7/nextjs-setup
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))}`) |