Skip to content

Instantly share code, notes, and snippets.

View mbuczko's full-sized avatar

Michał Buczko mbuczko

View GitHub Profile
@mbuczko
mbuczko / datetime.rs
Created December 6, 2020 21:57
rust / (de)serialize DateTime to Sql
use rusqlite::{ToSql, types::{FromSql, FromSqlResult, ToSqlOutput, ValueRef}};
use miniserde::{Deserialize, Serialize, de::Visitor, make_place, ser::Fragment};
use miniserde::Result;
make_place!(Place);
#[derive(Serialize, Clone, Deserialize, Debug)]
pub struct DateTime(
pub time::OffsetDateTime
);
@mbuczko
mbuczko / app.js
Created December 1, 2020 16:04
js / lamport lock with promises
function uuidv4() {
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}
function lamportMutex(id) {
const X = '_lock-X',
Y = '_lock-Y';
@mbuczko
mbuczko / flycheck.el
Created September 11, 2020 22:27
flycheck-parse-cargo-rustc
(defun flycheck-parse-cargo-rustc (output checker buffer)
"Parse Cargo errors from OUTPUT and return a list of `flycheck-error'.
CHECKER and BUFFER denote the CHECKER that returned OUTPUT and
the BUFFER that was checked respectively.
The expected format for OUTPUT is a mix of plain text lines and
JSON lines. This function ignores the plain text lines and
parses only JSON lines. Each JSON line is expected to be a JSON
object that represents a message from Cargo. The format of
@mbuczko
mbuczko / visit-github-issue.el
Created November 8, 2019 14:15
emacs / visit github issue or PR
;; requires git-link package
(defun github--goto-issue-or-pr (id type)
"Opens a browser with issue or PR (denoted by TYPE) of given ID."
(let* ((origin-url (car (git-link--exec "config" "--get" "remote.origin.url")))
(repo-match (string-match "^git@github.com:\\([^\\.]+\\)" origin-url))
(repo-url (concat "https://github.com/" (match-string 1 origin-url)))
(sub-path (cond ((eq 'issue type) "/issues")
((eq 'pr type) "/pull"))))
@mbuczko
mbuczko / pretty-hydra-config.el
Created July 29, 2019 11:01
emacs / pretty-hydra config
(require 'all-the-icons)
(defun with-faicon (icon str &optional height v-adjust)
(s-concat (all-the-icons-faicon icon :v-adjust (or v-adjust 0) :height (or height 1)) " " str))
(defun with-fileicon (icon str &optional height v-adjust)
(s-concat (all-the-icons-fileicon icon :v-adjust (or v-adjust 0) :height (or height 1)) " " str))
(defun with-octicon (icon str &optional height v-adjust)
(s-concat (all-the-icons-octicon icon :v-adjust (or v-adjust 0) :height (or height 1)) " " str))
@mbuczko
mbuczko / pathom.clj
Last active October 22, 2019 12:15
clojure / pathom + timeouts
(pc/defresolver res-with-timeout [_ _]
{::pc/output [:foo]}
(async/go
(let [timeout-ch (async/timeout 3000)
[ch res] (async/alts!! [(async/go
(do-my-operation-here))
timeout-ch] :priority true)]
(if (= ch timeout-ch)
(throw (ex-info "Resolver timeout" {:timeout 3000}))
res))))
@mbuczko
mbuczko / rest.org
Created June 28, 2019 21:34
emacs / restclient + org-mode

rest

github api

:query-repos := <<
(graphql-query
 ((viewer
   (repositories
    :arguments
    ((first . 3)
@mbuczko
mbuczko / rest.el
Created June 28, 2019 20:29
emacs / restclient example
:graphql-url = https://api.github.com/graphql
:github-token := (auth-source-pass-get "token" "web/github")
:headers = <<
Authorization: Bearer :github-token
User-Agent: Emacs
#
:query-repos := <<
@mbuczko
mbuczko / equals.clj
Created June 28, 2019 10:34
clojure / equal on type
(deftype Employee [name id]
Object
(equals [a b] (= (.id a) (.id b)))
(hashCode [this] (.hashCode (.id this)))
(toString [this] (.name this)))
(def vince (Employee. "Vince" 42))
(def vincent (Employee. "Vincent" 42))
@mbuczko
mbuczko / parallel-let.clj
Last active October 23, 2020 20:41
clojure / parallel-let
(defn remote-req [result]
(Thread/sleep 1000)
result)
(defmacro plet [bindings & body]
(let [bents (partition 2 (destructure bindings))
smap (into {} (map (fn [[b _]]
[b `(deref ~b)])
bents))
bindings (vec (mapcat (fn [[b v]]