Skip to content

Instantly share code, notes, and snippets.

View rpn3319's full-sized avatar

Raymond Phu Nguyen rpn3319

  • Ho Chi Minh City, Vietnam
  • 13:12 (UTC +07:00)
  • LinkedIn in/rpn19
View GitHub Profile
fn main() {
let abc: (f32, f32, f32) = (0.1, 0.2, 0.3);
let xyz: (f64, f64, f64) = (0.1, 0.2, 0.3);
assert!((abc.0 + abc.1 - abc.2).abs() <= f32::EPSILON); // pass
assert!((xyz.0 + xyz.1 - xyz.2).abs() <= f64::EPSILON); // pass
assert!(abc.0 + abc.1 == abc.2); // pass
assert!(xyz.0 + xyz.1 == xyz.2); // fail
}
use std::rc::Rc;
use std::cell::RefCell;
#[derive(Debug)]
struct GroundStation {
radio_freq: f64 // Mhz
}
fn main() {
let base: Rc<RefCell<GroundStation>> = Rc::new(RefCell::new(
@rpn3319
rpn3319 / rust-validator.rs
Last active September 12, 2023 15:34
rust validation with validator
// Cargo.toml
// validator = { version = "0.16.1", features = ["derive"] }
use validator::Validate;
#[derive(Deserialize, Validate)]
struct CatEndpointPath {
#[validate(range(min = 1, max = 150))]
id: i32,
}
@rpn3319
rpn3319 / rust-db-connection-pool-with-r2d2.rs
Last active September 12, 2023 15:30
rust db connection pool with r2d2
// Cargo.toml
// diesel = { version = "2.1.1", features = ["postgres", "r2d2"] }
async fn some_handler(pool: web::Data<DbPool>) -> Result<HttpResponse, UserError> {
todo!("Logic implemention here")
}
fn setup_database() -> DbPool {
let database_url = env::var("DATABASE_URL").expect("Database url is not set");
let manager = ConnectionManager::<PgConnection>::new(database_url);
@rpn3319
rpn3319 / rust-actix-return-files.rs
Created September 12, 2023 15:15
rust actix return files
use actix_files::{Files, NamedFile};
async fn index() -> Result<NamedFile> {
Ok(NamedFile::open("./static/index.html")?)
}
@rpn3319
rpn3319 / midstring.js
Created September 11, 2023 04:03
js midstring
function midString(prev, next) {
var p, n, pos, str;
for (pos = 0; p == n; pos++) {
// find leftmost non-matching character
p = pos < prev.length ? prev.charCodeAt(pos) : 96;
n = pos < next.length ? next.charCodeAt(pos) : 123;
}
str = prev.slice(0, pos - 1); // copy identical part of string
if (p == 96) {
// prev string equals beginning of next
@rpn3319
rpn3319 / remove-prefix.js
Created September 7, 2023 03:39
nodejs remove prefix
function removePrefix(originalString, prefix) {
if (typeof originalString !== 'string') return originalString;
return originalString.replace(new RegExp(`^${prefix}`), '');
}
/**
* This would throw exception if one of parameters is not string
*/
const removePrefix2 = (originalString, prefixToRemove) => {
if (typeof originalString !== 'string') return originalString;
@rpn3319
rpn3319 / nodejs-for-loop-block-event-loop.js
Created July 24, 2023 04:24
nodejs await for loop block event loop
const express = require('express');
const axios = require('axios');
const Bluebird = require('bluebird');
const { readFile: someOtherFnc } = require('fs');
const app = express();
// This is just a simulation
// real-life functions might not obviously simple for us to realize
async function simulateBlocker() {
@rpn3319
rpn3319 / aws-docker-login-ecr.sh
Last active July 23, 2023 12:09
aws docker login ecr
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account-id>.dkr.ecr.ap-southeast-1.amazonaws.com
@rpn3319
rpn3319 / git-diff-by-message.js
Created July 20, 2023 06:26
git-diff-by-message
const { execSync } = require('child_process');
const sourceBranch = process.argv[2];
const targetBranch = process.argv[3];
const getCommitsInfo = (branch) => {
const command = `git log --format="%h:::%s" ${branch}`;
const commitInfo = execSync(command, { encoding: 'utf8' });
return commitInfo.trim().split('\n');
};