Skip to content

Instantly share code, notes, and snippets.

View juliarose's full-sized avatar

Julia juliarose

  • United States
View GitHub Profile
@juliarose
juliarose / next_friday.rs
Last active February 3, 2023 19:50
Next friday at 12pm
use chrono::{Duration, NaiveDate, NaiveDateTime, NaiveTime, Weekday, Datelike};
/// Gets the next day of the week at the given time. If the current date is already the given
/// weekday **and** after the given time, it will return the next one.
fn get_next_day_of_week_at_time(
weekday: Weekday,
at_time: NaiveTime,
) -> Option<NaiveDateTime> {
let mut date = chrono::offset::Local::now();
let one_day = Duration::days(1);
@juliarose
juliarose / retryable.rs
Last active January 16, 2023 04:09
A method for retrying futures which result in any error that implements the trait Retryable to determine whether the request is retryable
use futures_retry::{ErrorHandler, RetryPolicy, FutureRetry};
use rand::Rng;
use std::time::Duration;
#[macro_export]
macro_rules! retry {
( $x:expr, $d:expr, $n:expr ) => {
{
FutureRetry::new(move || $x, RetryHandler::new($d, $n))
.await
fn expensive() -> bool {
println!("expensive");
true
}
fn main() {
// expensive is not executed when previous statement is false
if false && expensive() {
println!("Hello, world!");
}
@juliarose
juliarose / table_from_dump.sh
Created July 30, 2022 01:28
Exctract a single table out of a mysqldump file.
sed -n -e '/CREATE TABLE.*`table_name`/,/Table structure for table/p' db.sql > table.sql
CREATE TABLE colors (
id serial NOT NULL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
hex VARCHAR(6) NOT NULL
);
INSERT INTO colors
(name, hex)
VALUES
('Hot Pink', 'FF0066'),
('Orange', 'FF9900'),
@juliarose
juliarose / query_paints.sql
Created May 11, 2022 03:51
Sort paints by highest to lowest value
SELECT paints.rgb1
FROM paints
INNER JOIN bptf_prices
ON bptf_prices.defindex = paints.defindex
WHERE
bptf_prices.craftable = true
ORDER BY
bptf_prices.currency ASC,
bptf_prices.value DESC;
@juliarose
juliarose / table-sizes-mb.sql
Created May 8, 2022 20:44
mysql database table sizes
SELECT
TABLE_NAME AS `Table`,
ROUND((DATA_LENGTH + INDEX_LENGTH) / 1024 / 1024) AS `Size (MB)`
FROM
information_schema.TABLES
WHERE
TABLE_SCHEMA = "name_of_database"
ORDER BY
(DATA_LENGTH + INDEX_LENGTH)
DESC;
@juliarose
juliarose / generator.js
Created March 28, 2022 19:42
Rust macro generator for protobuf messages
const fs = require('fs');
const filepath = process.argv[2];
const filename = filepath.match(/([a-z_0-9]+)\.rs$/)[1];
const data = fs.readFileSync(filepath, 'utf8');
const structs = data
.split('\n')
.filter(line => /^pub struct /.test(line))
.map(line => line.replace(/(pub struct | \{)/g, ''));
const tab = ' ';
use std::time::{SystemTime, UNIX_EPOCH};
use sha1::Sha1;
use hmac::{Hmac, Mac, NewMac};
use std::io::Cursor;
use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use base64;
const CHARS: &'static [char] = &['2', '3', '4', '5', '6', '7', '8', '9', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M',
'N', 'P', 'Q', 'R', 'T', 'V', 'W', 'X', 'Y'];
@juliarose
juliarose / oneshot.rs
Created March 14, 2022 13:24
Send request to main to respond back with a message.
use tokio::sync::{mpsc, oneshot};
use async_std::task::sleep;
use std::time::Duration;
#[tokio::main]
async fn main() {
let (main_tx, mut main_rx) = mpsc::channel(16);
tokio::spawn(async move {
println!("Hello?");