Typesafe representations of network concepts
We need to work with a variety of different types of entity when working with networking technologies. These include URLs, IP addresses, email address and MAC addresses. Parsing and serializing these entities without loss of generality is typically nontrivial, and it is for this reason that Nettlesome exists.
- Represent URLs, MAC addresses and IP addresses with immutable datatypes
- IPv4 addresses and MAC addresses are represented efficiently as opaque primitives
- Compile-time parsing of URLs, MAC addresses and IP addresses with string interpolators
- Runtime parsing and serialization, with parsing errors handled using capabilities
Nettlesome is capable of representing all of the following:
- IPv4 addresses as
Ipv4
s - IPv6 addresses as
Ipv6
s - URLs as
Url
s - hostnames as
Hostname
s - MAC addresses as
MacAddress
s
Email addresses will be added later.
To create an IP address value, either an Ipv4
or Ipv6
, from a known string, such as 192.168.1.12
or 2000:abcd:1234::5:12
, simply write it inside quotes prefixed with ip
, like so:
val address1: Ipv4 = ip"192.168.1.12"
val address2: Ipv6 = ip"2000:abcd:1234::5:12"
The same ip
interpolator is used for both types, and the return type will adapt to the content.
A mistake in the content will result in a compile error, for example ip"192.168.1.2.3"
will fail to
compile because there are five .
-separated integers, rather than four.
In particular, for IPv4 addresses, the parser checks for,
- exactly four groups
- only integers in the range 0-255
- no non-integer characters, other than
.
and for IPv6 addresses,
- a maximum of eight
:
-separated groups - at most one
::
separator - between one and four hexadecimal characters for each group
The same parser can be used at runtime, throwing an IpAddressError
in the event of an incorrect address.
To parse an IP address from a Text
value, use the parse
method of either the Ipv4
or Ipv6
objects,
for example,
val address1: Ipv4 = Ipv4.parse(t"192.168.1.12")
val address2: Ipv6 = Ipv6.parse(ipAddressText)
Both Ipv4
and Ipv6
addresses have Show
instances, which will serialize
these addresse to Text
values. IPv4 addresses will be serialized,
unsurprisingly, as four integers interspersed with .
characters, and IPv6
addresses will be serialized according to the
IETF's recommended canonical representation,
which includes various simplifications.
A URL instance can be defined using the url""
interpolator, which will check
the URL's syntax at compiletime, reporting any parsing issues as compile errors. For example:
val url = url"https://github.com/propensive/nettlesom/"
It is also possible to construct a Url
instance directly using the various
immutable datatypes from which it is composed, but it's rare that this would be
preferable.
If the URL is not known at compiletime, but is available at runtime as a Text
string, it can be parsed. For example,
val url = Url.parse(urlText)
If an invalid value is passed to the parse
method, then an error will be
raised. If using Contingency then
there are a variety of ways in which such errors can be handled.
Nettlesome is classified as maturescent. For reference, Soundness projects are categorized into one of the following five stability levels:
- embryonic: for experimental or demonstrative purposes only, without any guarantees of longevity
- fledgling: of proven utility, seeking contributions, but liable to significant redesigns
- maturescent: major design decisions broady settled, seeking probatory adoption and refinement
- dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version
1.0.0
or later - adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated
Projects at any stability level, even embryonic projects, can still be used, as long as caution is taken to avoid a mismatch between the project's stability level and the required stability and maintainability of your own project.
Nettlesome is designed to be small. Its entire source code currently consists of 1116 lines of code.
Nettlesome will ultimately be built by Fury, when it is published. In the meantime, two possibilities are offered, however they are acknowledged to be fragile, inadequately tested, and unsuitable for anything more than experimentation. They are provided only for the necessity of providing some answer to the question, "how can I try Nettlesome?".
-
Copy the sources into your own project
Read the
fury
file in the repository root to understand Nettlesome's build structure, dependencies and source location; the file format should be short and quite intuitive. Copy the sources into a source directory in your own project, then repeat (recursively) for each of the dependencies.The sources are compiled against the latest nightly release of Scala 3. There should be no problem to compile the project together with all of its dependencies in a single compilation.
-
Build with Wrath
Wrath is a bootstrapping script for building Nettlesome and other projects in the absence of a fully-featured build tool. It is designed to read the
fury
file in the project directory, and produce a collection of JAR files which can be added to a classpath, by compiling the project and all of its dependencies, including the Scala compiler itself.Download the latest version of
wrath
, make it executable, and add it to your path, for example by copying it to/usr/local/bin/
.Clone this repository inside an empty directory, so that the build can safely make clones of repositories it depends on as peers of
nettlesome
. Runwrath -F
in the repository root. This will download and compile the latest version of Scala, as well as all of Nettlesome's dependencies.If the build was successful, the compiled JAR files can be found in the
.wrath/dist
directory.
Contributors to Nettlesome are welcome and encouraged. New contributors may like to look for issues marked beginner.
We suggest that all contributors read the Contributing Guide to make the process of contributing to Nettlesome easier.
Please do not contact project maintainers privately with questions unless there is a good reason to keep them private. While it can be tempting to repsond to such questions, private answers cannot be shared with a wider audience, and it can result in duplication of effort.
Nettlesome was designed and developed by Jon Pretty, and commercial support and training on all aspects of Scala 3 is available from Propensive OÜ.
Nettlesome provides the slightly irritating (or nettlesome) task of working with network addresses, which it alludes to in its first three letters.
In general, Soundness project names are always chosen with some rationale, however it is usually frivolous. Each name is chosen for more for its uniqueness and intrigue than its concision or catchiness, and there is no bias towards names with positive or "nice" meanings—since many of the libraries perform some quite unpleasant tasks.
Names should be English words, though many are obscure or archaic, and it should be noted how willingly English adopts foreign words. Names are generally of Greek or Latin origin, and have often arrived in English via a romance language.
The logo shows a nettle leaf.
Nettlesome is copyright © 2024 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.