aboutsummaryrefslogtreecommitdiff
path: root/lib/virtuals
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2025-06-01 17:12:06 +0200
committerSébastien Dailly <sebastien@dailly.me>2025-06-14 10:58:01 +0200
commit2f18b8a33cabd0ea666781ba048d0174b4dc5031 (patch)
treeaacda421b89e8133e3c73942e9ede61283a5005c /lib/virtuals
Initial commit
Diffstat (limited to 'lib/virtuals')
-rw-r--r--lib/virtuals/_readme.rst5
-rw-r--r--lib/virtuals/v_string/brr_string/dune4
-rw-r--r--lib/virtuals/v_string/brr_string/v_string.ml10
-rw-r--r--lib/virtuals/v_string/dream_string/dune4
-rw-r--r--lib/virtuals/v_string/dream_string/v_string.ml6
-rw-r--r--lib/virtuals/v_string/dune4
-rw-r--r--lib/virtuals/v_string/v_string.mli14
7 files changed, 47 insertions, 0 deletions
diff --git a/lib/virtuals/_readme.rst b/lib/virtuals/_readme.rst
new file mode 100644
index 0000000..4c31089
--- /dev/null
+++ b/lib/virtuals/_readme.rst
@@ -0,0 +1,5 @@
+This directory contains hold the `virtual libraries` used in the application.
+They allow to share common code between the javascript and server side with
+differents implementations (for exemple in the string representation).
+
+.. _`virtual libraries`: https://dune.readthedocs.io/en/stable/variants.html#virtual-library
diff --git a/lib/virtuals/v_string/brr_string/dune b/lib/virtuals/v_string/brr_string/dune
new file mode 100644
index 0000000..0e3f942
--- /dev/null
+++ b/lib/virtuals/v_string/brr_string/dune
@@ -0,0 +1,4 @@
+(library
+ (name brr_string)
+ (libraries brr)
+ (implements v_string))
diff --git a/lib/virtuals/v_string/brr_string/v_string.ml b/lib/virtuals/v_string/brr_string/v_string.ml
new file mode 100644
index 0000000..9aeb01f
--- /dev/null
+++ b/lib/virtuals/v_string/brr_string/v_string.ml
@@ -0,0 +1,10 @@
+type t = Jstr.t
+
+let v : string -> t = Jstr.v
+let s = Jstr.to_string
+let concat : ?sep:t -> t list -> t = Jstr.concat
+
+let encode v =
+ match Brr.Uri.encode_component v with
+ | Ok s -> s
+ | Error _ -> Jstr.empty
diff --git a/lib/virtuals/v_string/dream_string/dune b/lib/virtuals/v_string/dream_string/dune
new file mode 100644
index 0000000..ff6c9b2
--- /dev/null
+++ b/lib/virtuals/v_string/dream_string/dune
@@ -0,0 +1,4 @@
+(library
+ (name dream_string)
+ (libraries dream)
+ (implements v_string))
diff --git a/lib/virtuals/v_string/dream_string/v_string.ml b/lib/virtuals/v_string/dream_string/v_string.ml
new file mode 100644
index 0000000..14a4e33
--- /dev/null
+++ b/lib/virtuals/v_string/dream_string/v_string.ml
@@ -0,0 +1,6 @@
+type t = string
+
+let v : string -> t = Fun.id
+let s : t -> string = Fun.id
+let concat : ?sep:t -> t list -> t = fun ?(sep = "") -> StringLabels.concat ~sep
+let encode = Dream.to_percent_encoded ?international:None
diff --git a/lib/virtuals/v_string/dune b/lib/virtuals/v_string/dune
new file mode 100644
index 0000000..badb7d3
--- /dev/null
+++ b/lib/virtuals/v_string/dune
@@ -0,0 +1,4 @@
+(library
+ (name v_string)
+ (virtual_modules v_string)
+ (default_implementation dream_string))
diff --git a/lib/virtuals/v_string/v_string.mli b/lib/virtuals/v_string/v_string.mli
new file mode 100644
index 0000000..267dbf2
--- /dev/null
+++ b/lib/virtuals/v_string/v_string.mli
@@ -0,0 +1,14 @@
+type t
+(**
+ This module provide a common signature between the server and javascript
+ side in order to create URL in the same way.
+*)
+
+val v : string -> t
+val s : t -> string
+val concat : ?sep:t -> t list -> t
+
+val encode : t -> t
+(** Encode an element of the url by percent-encoding. The function is
+ expected to encode "/" as well.
+ *)