OCaml implementation of the Mozilla Public Suffix service
0
fork

Configure Feed

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

publicsuffix: fix README — API takes t + string returning result, not Domain_name.t option

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