Documentation

LeanUri.Basic

High-level API for RFC 3986 URIs #

This module exposes a small, convenient surface over the internal parsers, normalizers, and resolvers:

It delegates to the Internal parser/normalizer/resolver implemented in LeanUri.Parsers, LeanUri.Normalization, and related modules.

@[inline]

Parse an absolute URI from a string without applying normalization.

Returns .ok uri on success or .error msg if parsing fails.

Examples #

open LeanUri
match URI.parse "https://example.com/a?b#c" with
| .ok u   => (u.scheme, u.authority.isSome, u.path) = ("https", true, "/a")
| .error _ => False
Equations
Instances For
    @[inline]

    Parse a RelativeRef (relative reference) from a string without normalization.

    Returns .ok ref on success or .error msg on parse failure.

    Examples #

    open LeanUri
    match RelativeRef.parse "../x?y#z" with
    | .ok r   => (r.authority, r.path, r.query, r.fragment) = (none, "../x", some "y", some "z")
    | .error _ => False
    
    Equations
    Instances For
      @[inline]

      Parse either an absolute URI or a RelativeRef from a string.

      Returns Sum.inl uri for an absolute URI, or Sum.inr ref for a relative reference.

      Examples #

      open LeanUri
      ( parseReference "mailto:user@example.com" ).isOk -- absolute → inl
      ( parseReference "./a/b" ).isOk                  -- relative → inr
      
      Equations
      Instances For
        @[inline]

        Serialize a URI to a string without applying any normalization.

        This is a convenience wrapper around the ToString instance.

        Examples #

        open LeanUri
        let u : URI := { scheme := "https", authority := some "example.com", path := "/a", query := some "q=1", fragment := none }
        URI.toString u = "https://example.com/a?q=1"
        
        Equations
        Instances For
          @[inline]

          Serialize a RelativeRef to a string without applying any normalization.

          This is a convenience wrapper around the ToString instance.

          Examples #

          open LeanUri
          let r : RelativeRef := { authority := some "example.com", path := "/p", query := none, fragment := some "frag" }
          RelativeRef.toString r = "//example.com/p#frag"
          
          Equations
          Instances For
            @[inline]

            Normalize all components of a URI according to the RFC 3986 rules implemented by this library (e.g., case normalization where applicable, removal of dot segments, etc.).

            Examples #

            open LeanUri
            let u : URI := { scheme := "http", authority := some "EXAMPLE.com", path := "/a/./b/../c", query := none, fragment := none }
            URI.normalize u |>.toString = "http://example.com/a/c"
            
            Equations
            Instances For
              def LeanUri.URI.resolveURIorRef (baseUri : URI) (reference : URI RelativeRef) :

              Resolve a reference or relative ref against a base URI.

              • If reference is an absolute URI, it is returned as-is.
              • If reference is a relative reference, it is resolved against baseUri using Internal.resolve.
              • Returns .error msg when reference cannot be parsed as a URI or relative reference.

              See URI.resolve or URI.resolveRef as well.

              Equations
              Instances For
                def LeanUri.URI.resolve (baseUri : URI) (reference : String) :

                Resolve a string reference against a base URI.

                • If reference is an absolute URI, it is returned as-is.
                • If reference is a relative reference, it is resolved against baseUri using Internal.resolve.
                • Returns .error msg when reference cannot be parsed as a URI or relative reference.

                Examples #

                open LeanUri
                let base : URI := { scheme := "https", authority := some "example.com", path := "/dir/index.html", query := none, fragment := none }
                URI.resolve base "../x" = .ok { scheme := "https", authority := some "example.com", path := "/x", query := none, fragment := none }
                
                Equations
                Instances For
                  @[inline]

                  Resolve a pre-parsed RelativeRef against a base URI.

                  Examples #

                  open LeanUri
                  let base : URI := { scheme := "https", authority := some "example.com", path := "/a/b/", query := none, fragment := none }
                  let rr   : RelativeRef := { authority := none, path := "c/../d", query := none, fragment := none }
                  URI.resolveRef base rr |>.toString = "https://example.com/a/d"
                  
                  Equations
                  Instances For
                    @[inline]

                    Return true when the URI has an authority component.

                    Note: This checks for the presence of an authority (i.e., //…), which is not the same as checking for the presence of a scheme.

                    Examples #

                    open LeanUri
                    let a : URI := { scheme := "http",  authority := some "example.com", path := "/", query := none, fragment := none }
                    let b : URI := { scheme := "mailto", authority := none,               path := "user@example.com", query := none, fragment := none }
                    (URI.isAbsolute a, URI.isAbsolute b) = (true, false)
                    
                    Equations
                    Instances For
                      @[inline]
                      def LeanUri.URI.equivalent (uri1 uri2 : URI) :

                      Check whether two URIs are equivalent after normalization.

                      Examples #

                      open LeanUri
                      let u1 : URI := { scheme := "http", authority := some "EXAMPLE.com", path := "/a/./b/../c", query := some "Q=1", fragment := none }
                      let u2 : URI := { scheme := "http", authority := some "example.COM", path := "/a/c",         query := some "q=1", fragment := none }
                      URI.equivalent u1 u2 = true
                      
                      Equations
                      Instances For
                        @[inline]
                        def LeanUri.URI.encode (scheme : String) (authority : Option String := none) (path : String := "") (query fragment : Option String := none) :

                        Construct a URI from raw (unencoded) components, automatically percent-encoding as required by RFC 3986. This ensures the resulting URI is valid and safe for serialization.

                        Equations
                        • One or more equations did not get rendered due to their size.
                        Instances For
                          @[inline]
                          def LeanUri.RelativeRef.encode (authority : Option String := none) (path : String := "") (query fragment : Option String := none) :

                          Construct a RelativeRef from raw (unencoded) components, automatically percent-encoding as required by RFC 3986. This ensures the resulting RelativeRef is valid and safe for serialization.

                          Equations
                          • One or more equations did not get rendered due to their size.
                          Instances For
                            @[inline]

                            Get the decoded (unescaped) authority component of a RelativeRef, if present.

                            Equations
                            Instances For
                              @[inline]

                              Get the decoded (unescaped) path component of a RelativeRef.

                              Equations
                              Instances For
                                @[inline]

                                Get the decoded (unescaped) query component of a RelativeRef, if present.

                                Equations
                                Instances For
                                  @[inline]

                                  Get the decoded (unescaped) fragment component of a RelativeRef, if present.

                                  Equations
                                  Instances For
                                    @[inline]

                                    Get the decoded (unescaped) authority component of a URI, if present.

                                    Equations
                                    Instances For
                                      @[inline]

                                      Get the decoded (unescaped) path component of a URI.

                                      Equations
                                      Instances For
                                        @[inline]

                                        Get the decoded (unescaped) query component of a URI, if present.

                                        Equations
                                        Instances For
                                          @[inline]

                                          Get the decoded (unescaped) fragment component of a URI, if present.

                                          Equations
                                          Instances For