]> gitweb.factorcode.org Git - factor.git/blob - extra/ulid/ulid-docs.factor
Fixes #2966
[factor.git] / extra / ulid / ulid-docs.factor
1 ! Copyright (C) 2019 Alexander Ilin.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: byte-arrays help.markup help.syntax kernel math strings
4 ulid.private ;
5 IN: ulid
6
7 ABOUT: "ulid"
8
9 ARTICLE: "ulid" "Universally Unique Lexicographically Sortable Identifier"
10 "The " { $vocab-link "ulid" } " vocab implements the Universally Unique Lexicographically Sortable Identifier gereration according to the specification: " { $url "https://github.com/ulid/spec" } ". The main word to call is:"
11 { $subsections ulid }
12 "Binary convertion interface:"
13 { $subsections ulid>bytes bytes>ulid }
14 "Helpers:"
15 { $subsections normalize-ulid }
16 ;
17
18 HELP: bytes>ulid
19 { $values
20     { "byte-array" byte-array }
21     { "ulid" string }
22 }
23 { $description "Convert a binary ULID to its string representation using the Crockford's base32 " { $link encoding } ". The " { $snippet "byte-array" } " must be exactly 16 bytes long, the resulting " { $snippet "ulid" } " string is always 26 characters long." }
24 { $errors { $subsections bytes>ulid-bad-length } } ;
25
26 HELP: bytes>ulid-bad-length
27 { $values
28     { "n" number }
29 }
30 { $description "Throws a " { $link bytes>ulid-bad-length } " error." }
31 { $error-description "This error is thrown if the input array for the " { $link bytes>ulid } " conversion has length " { $snippet "n" } " instead of 16." } ;
32
33 HELP: normalize-ulid
34 { $values
35     { "str" string }
36     { "str'" string }
37 }
38 { $description "Convert the " { $snippet "str" } " to upper-case and substitute some ambiguous characters according to the Crockford's convention: \"L\" and \"I\" -> \"1\", \"O\" -> \"0\". This may be useful to run on a user-provided string to make sure it was typed in correctly. Subsequent " { $link ulid>bytes } " conversion could be used to make sure the decoded contents constitute a valid ULID." } ;
39
40 HELP: ulid
41 { $values
42     { "ulid" string }
43 }
44 { $description "Generate a new 128-bit ULID using and return its string representation in the Crockford's base32 " { $link encoding } ". The current system time is encoded in the high 48 bits as the Unix time in milliseconds, the low 80 bits are random."
45 $nl
46 "At the time of this writing the Factor implementation is not able to produce multiple ULIDs within less than one millisecond of each other, but a provision is made to make sure that if that ever happens in the future, the subsequent ULIDs inside of a millisecond will be an increment of the previous ones to guarentee the sorting order of the identifiers, as per the specification." }
47 { $errors "In case an overflow happens during such incrementing, the " { $link ulid-overflow } " error will be thrown." } ;
48
49 HELP: ulid-overflow
50 { $description "Throws an " { $link ulid-overflow } " error." }
51 { $error-description "This error is thrown if by chance the 80-bit random number generated by the " { $link ulid } " word matches " { $link 80-bits } ", and a new ULID is requested " { $strong "within the same millisecond." } " In this case the specification requires an error to be thrown, because it was not possible to produce two ULIDs, while guarenteeing their sorting order. The best course of action is to retry ULID generation when the next millisecond is on the system clock." } ;
52
53 HELP: ulid>bytes
54 { $values
55     { "ulid" string }
56     { "byte-array" byte-array }
57 }
58 { $description "Convert a string " { $snippet "ulid" } " into its binary representation." }
59 { $errors { $subsections ulid>bytes-bad-length ulid>bytes-bad-character ulid>bytes-overflow } } ;
60
61 HELP: ulid>bytes-bad-character
62 { $values
63     { "ch" "a character" }
64 }
65 { $description "Throws a " { $link ulid>bytes-bad-character } " error." }
66 { $error-description "This error is thrown if during ULID to byte-array conversion a character " { $snippet "ch" } " is encountered in the input string, which is not part of the supported " { $link encoding } ". Try using " { $link normalize-ulid } " before the conversion." } ;
67
68 HELP: ulid>bytes-bad-length
69 { $values
70     { "n" number }
71 }
72 { $description "Throws a " { $link ulid>bytes-bad-length } " error." }
73 { $error-description "This error is thrown if the input string has length " { $snippet "n" } " instead of 26." } ;
74
75 HELP: ulid>bytes-overflow
76 { $description "Throws a " { $link ulid>bytes-overflow } " error." }
77 { $error-description "This error is thrown if the first character of the ULID string is greater than \"7\" in the " { $link encoding } ". This can only mean that the input string is not a valid ULID according to the specification." } ;