OCaml implementation of the Mozilla Public Suffix service
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

ocaml-publicsuffix: rewrite README examples to typecheck

The blocks chained top-level [match ... with] expressions, which
doesn't parse, and used [Format.printf] commentary alongside trailing
[(* Output: ... *)] comments to assert behaviour. Wrap each call in
[let () = match ... with], replace the comments with [assert]s, and
drop [Format.printf] for [Fmt.failwith] / silent OK.

+26 -26
+26 -26
README.md
··· 13 13 ## Usage 14 14 15 15 ```ocaml 16 - (* Create a PSL instance *) 17 16 let psl = Publicsuffix.v () 18 - (* Determine the registrable domain (public suffix + one label) *) 19 - match Publicsuffix.registrable_domain psl "www.example.com" with 20 - | Ok reg_domain -> Format.printf "Registrable: %s\n" reg_domain 21 - | Error e -> Format.printf "Error: %a\n" Publicsuffix.pp_error e 22 - (* Output: Registrable: example.com *) 23 17 24 - (* Find the public suffix *) 25 - match Publicsuffix.public_suffix psl "www.example.com" with 26 - | Ok suffix -> Format.printf "Suffix: %s\n" suffix 27 - | Error e -> Format.printf "Error: %a\n" Publicsuffix.pp_error e 28 - (* Output: Suffix: com *) 18 + (* Determine the registrable domain (public suffix + one label). *) 19 + let () = 20 + match Publicsuffix.registrable_domain psl "www.example.com" with 21 + | Ok r -> assert (r = "example.com") 22 + | Error _ -> failwith "expected registrable" 29 23 30 - (* Check if a domain is itself a public suffix *) 31 - match Publicsuffix.is_public_suffix psl "com" with 32 - | Ok is_suffix -> Format.printf "Is public suffix: %b\n" is_suffix 33 - | Error e -> Format.printf "Error: %a\n" Publicsuffix.pp_error e 34 - (* Output: Is public suffix: true *) 24 + (* Find the public suffix. *) 25 + let () = 26 + match Publicsuffix.public_suffix psl "www.example.com" with 27 + | Ok s -> assert (s = "com") 28 + | Error _ -> failwith "expected suffix" 29 + 30 + (* Check if a domain is itself a public suffix. *) 31 + let () = 32 + match Publicsuffix.is_public_suffix psl "com" with 33 + | Ok b -> assert b 34 + | Error _ -> failwith "expected ok" 35 35 ``` 36 36 37 37 For domains with wildcards and exceptions: 38 38 39 39 ```ocaml 40 - (* Example with wildcard rule: *.uk *) 41 - match Publicsuffix.public_suffix psl "example.uk" with 42 - | Ok suffix -> Format.printf "Suffix: %s\n" suffix 43 - | Error _ -> () 44 - (* Output: Suffix: uk *) 40 + (* Wildcard rule: *.uk. *) 41 + let () = 42 + match Publicsuffix.public_suffix psl "example.uk" with 43 + | Ok s -> assert (s = "uk") 44 + | Error _ -> () 45 45 46 - (* Example with exception rule: !parliament.uk *) 47 - match Publicsuffix.registrable_domain psl "parliament.uk" with 48 - | Ok reg_domain -> Format.printf "Registrable: %s\n" reg_domain 49 - | Error _ -> () 50 - (* Output: Registrable: parliament.uk *) 46 + (* Exception rule: !parliament.uk. *) 47 + let () = 48 + match Publicsuffix.registrable_domain psl "parliament.uk" with 49 + | Ok r -> assert (r = "parliament.uk") 50 + | Error _ -> () 51 51 ``` 52 52 53 53 ## Installation