Skip to content

Instantly share code, notes, and snippets.

@boxdot
boxdot / async.rs
Last active November 9, 2022 12:03
Async stdin reading with thread and futures in Rust
extern crate futures;
use std::io::{self, BufRead};
use std::thread;
use futures::{Future, Sink, Stream};
use futures::stream::BoxStream;
use futures::sync::mpsc::channel;
fn stdin() -> impl Stream<String, io::Error> {
use gitlab::webhooks::WebHook;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Request, Response, Server};
use log::{log_enabled, Level};
async fn gitlab_webhook(req: Request<Body>) -> Result<Response<Body>, hyper::Error> {
log::debug!("< {:?}", req);
let body = hyper::body::to_bytes(req.into_body()).await?;
let event: Result<WebHook, _> = serde_json::from_slice(&body);
match event {
@boxdot
boxdot / num-stable-crates.rs
Created November 7, 2019 20:09
Calculate number of stable crates on crates.io
use std::cmp::Reverse;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use walkdir::WalkDir;
fn get_version(entry: walkdir::Result<walkdir::DirEntry>) -> Option<String> {
let entry = entry.ok()?;
let path = entry.path();
use actix::prelude::*;
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use log::info;
use std::io;
use std::thread;
use std::time::Duration;
fn my_handler() -> HttpResponse {
"Hello".into()
fn some_test -> Result<(), Error> {
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.spawn(microservice_1.map_err(|e| panic!(e)));
rt.spawn(microservice_2.map_err(|e| panic!(e)));
rt.spawn(microservice_3.map_err(|e| panic!(e)));
rt.spawn(microservice_4.map_err(|e| panic!(e)));
// do testing and do not touch rt anymore
}
#[macro_use]
extern crate nom;
use nom::alpha;
use nom::types::CompleteStr;
use nom::ErrorKind;
named!(block<CompleteStr, Vec<(CompleteStr, CompleteStr)>>,
do_parse!(
tag!("BEGIN") >>
@boxdot
boxdot / Cargo.toml
Last active October 9, 2018 09:20
Example of a hyper service/server with rate limiting of requests
[package]
name = "ratelimiting-server"
version = "0.1.0"
[dependencies]
futures = "0.1"
tokio = "0.1"
hyper = "0.12"
@boxdot
boxdot / cli.elm
Last active July 24, 2018 20:47
Minimal console app in Elm
module Cli where
port output : String
port output =
"Sub-orbital table 8-bit realism boat soul-delay face forwards industrial
grade drone. Cartel towards footage tube assault table woman stimulate bridge
claymore mine semiotics human. Post-construct j-pop military-grade stimulate
narrative realism. Shoes convenience store sunglasses realism numinous tanto
long-chain hydrocarbons. Franchise tower render-farm girl wonton soup sprawl
fetishism Kowloon advert semiotics shoes dolphin drugs otaku marketing.
#![allow(dead_code)]
extern crate futures;
extern crate time;
use futures::prelude::*;
use futures::sync::mpsc::{channel, Receiver};
use time::{Tm, now_utc};
use std::{thread};
@boxdot
boxdot / client.rs
Last active February 2, 2018 16:46
impl Client {
pub fn connect(
handle: &Handle,
uri: hyper::Uri,
framework_info: mesos::FrameworkInfo,
) -> Box<Future<Item = Self, Error = failure::Error>> {
// Mesos subscribe essage
let mut call = scheduler::Call::new();
let mut subscribe = scheduler::Call_Subscribe::new();
subscribe.set_framework_info(framework_info);