Current section
Files
Jump to
Current section
Files
src/sara.erl
-module(sara).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/sara.gleam").
-export([elapsed/1, main/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/sara.gleam", 109).
-spec elapsed(fun(() -> TLZ)) -> {TLZ, float()}.
elapsed(Fun) ->
sara_ffi:elapsed(Fun).
-file("src/sara.gleam", 16).
?DOC(
" 🦑 Performs serialization code generation for your Gleam project.\n"
"\n"
" The `sara` cli is not configurable by default.\n"
" With the JSON module of ***Sara*** you can with the help of `//@json_encode()` and `//@json_decode()` generates type-safe JSON encoding and decoding functions for your Gleam custom types at build time.\n"
" \n"
" > ⚠️ The generated JSON code relies on :\n"
" > - [`json`](https://hexdocs.pm/gleam_json/) package for the json encoder.\n"
" > - [`stdlib`](https://hexdocs.pm/gleam_stdlib/) package for the json decoder.\n"
" > So make sure to have them inside your project.\n"
).
-spec main() -> nil.
main() ->
gleam_stdlib:println(<<"🦑 Start Sara"/utf8>>),
{_, Duration} = sara_ffi:elapsed(
fun() ->
builder:execute_builders(
[sara@json:json_serializable_builder(
{config,
[{custom_codec,
<<"Timestamp"/utf8>>,
<<"gleam/time/timestamp"/utf8>>,
fun(_, _, Variable, _, _) ->
{generated_code,
<<<<"json.string(timestamp.to_rfc3339("/utf8,
Variable/binary>>/binary,
", calendar.utc_offset))"/utf8>>,
[<<"gleam/time/calendar"/utf8>>,
<<"gleam/time/timestamp"/utf8>>],
[]}
end,
fun(_, _, _, _) ->
{generated_code,
<<"{
use date <- decode.then(decode.string)
case timestamp.parse_rfc3339(date) {
Ok(timestamp) -> decode.success(timestamp)
Error(_) -> decode.failure(timestamp.system_time(), \"Timestamp\")
}
}"/utf8>>,
[<<"gleam/time/calendar"/utf8>>,
<<"gleam/time/timestamp"/utf8>>],
[]}
end,
fun(_, _, _, _) ->
{ok,
{generated_code,
<<"timestamp.system_time()"/utf8>>,
[<<"gleam/time/timestamp"/utf8>>],
[]}}
end},
{custom_codec,
<<"TimeOfDay"/utf8>>,
<<"gleam/time/calendar"/utf8>>,
fun(_, _, Variable@1, _, _) ->
{generated_code,
<<<<"{
let calendar.TimeOfDay(hours:, minutes:, seconds:, nanoseconds:) = "/utf8,
Variable@1/binary>>/binary,
"
json.object([
#(\"hours\", json.int(hours)),
#(\"minutes\", json.int(minutes)),
#(\"seconds\", json.int(seconds)),
#(\"nanoseconds\", json.int(nanoseconds)),
])
}"/utf8>>,
[<<"gleam/time/calendar"/utf8>>],
[]}
end,
fun(_, _, _, _) ->
{generated_code,
<<"{
use hours <- decode.field(\"hours\", decode.int)
use minutes <- decode.field(\"minutes\", decode.int)
use seconds <- decode.field(\"seconds\", decode.int)
use nanoseconds <- decode.field(\"nanoseconds\", decode.int)
decode.success(calendar.TimeOfDay(hours:, minutes:, seconds:, nanoseconds:))
}"/utf8>>,
[<<"gleam/time/calendar"/utf8>>],
[]}
end,
fun(_, _, _, _) ->
{ok,
{generated_code,
<<"calendar.TimeOfDay(hours: 0, minutes: 0, seconds: 0, nanoseconds: 0)"/utf8>>,
[<<"gleam/time/calendar"/utf8>>],
[]}}
end}]}
)]
)
end
),
Duration@1 = Duration / 1000000000.0,
gleam_stdlib:println(
<<<<"Finished in "/utf8,
(begin
_pipe = gleam@float:to_precision(Duration@1, 3),
gleam_stdlib:float_to_string(_pipe)
end)/binary>>/binary,
"s"/utf8>>
),
sara_ffi:exit(0).