Skip to content

Instantly share code, notes, and snippets.

@Blacksmoke16
Blacksmoke16 / design.md
Last active December 18, 2025 00:49
Crystal Annotations 2.0

Annotations 2.0 - Design Summary

Problem Statement

Crystal's annotation system lacked tight coupling between annotation types and their runtime representations. Framework authors building validation libraries, ORMs, or serialization tools had to maintain two separate type hierarchies:

  1. An annotation type for compile-time metadata
  2. A corresponding class/struct for runtime logic

This duplication was error-prone and verbose. Users couldn't easily convert annotations to runtime objects or leverage inheritance for annotation hierarchies.

@Blacksmoke16
Blacksmoke16 / .env
Last active November 29, 2025 04:15
Crystal Client code for publishing to a Mercure Hub
MERCURE_URL=http://localhost:8000/.well-known/mercure
MERCURE_JWT_SECRET=eyJhbGciOiJIUzI1NiJ9.eyJtZXJjdXJlIjp7InB1Ymxpc2giOlsiKiJdLCJzdWJzY3JpYmUiOlsiaHR0cHM6Ly9leGFtcGxlLmNvbS9teS1wcml2YXRlLXRvcGljIiwie3NjaGVtZX06Ly97K2hvc3R9L2RlbW8vYm9va3Mve2lkfS5qc29ubGQiLCIvLndlbGwta25vd24vbWVyY3VyZS9zdWJzY3JpcHRpb25zey90b3BpY317L3N1YnNjcmliZXJ9Il0sInBheWxvYWQiOnsidXNlciI6Imh0dHBzOi8vZXhhbXBsZS5jb20vdXNlcnMvZHVuZ2xhcyIsInJlbW90ZUFkZHIiOiIxMjcuMC4wLjEifX19.KKPIikwUzRuB3DTpVw6ajzwSChwFw5omBMmMcWKiDcM
@Blacksmoke16
Blacksmoke16 / lib_c.cr
Last active March 19, 2025 20:39
OS Agnostic way to determine terminal height/width
{% if flag?(:win32) %}
lib LibC
STDOUT_HANDLE = 0xFFFFFFF5
struct Point
x : UInt16
y : UInt16
end
struct SmallRect
@Blacksmoke16
Blacksmoke16 / code.cr
Created June 16, 2022 01:29
XML2 XSD Schema validation
require "xml"
lib LibXML
type XMLSchemaParserCtxtPtr = Void*
type XMLSchemaValidCtxtPtr = Void*
struct XMLError
domain : Int
code : Int
message : UInt8*
@Blacksmoke16
Blacksmoke16 / entity_manager.cr
Last active December 15, 2021 17:58
Simple DB Abstractions
@[ADI::Register]
# Central access point to DB related actions
class Blog::Services::EntityManager
@@connection : DB::Database = DB.open ENV["DATABASE_URL"]
# Each entity has a Repository to store query methods
# Longer term ofc this could be made a bit better but manual overload for each works just fine.
#
# Could prob use a macro loop over entity and add overload if the repo type exists
def repository(entity_class : Blog::Entities::User.class) : Blog::Entities::User::Repository
@Blacksmoke16
Blacksmoke16 / test.cr
Last active July 29, 2024 13:11
Test Crinja + Athena integration
require "crinja"
ADI.configuration_annotation Blog::Annotations::Template, name : String
@[ADI::Register]
class HTMLFormatHandler
include Athena::Framework::View::FormatHandlerInterface
private CRINJA = Crinja.new loader: Crinja::Loader::FileSystemLoader.new "#{__DIR__}/../views"