///|
/// Raised when a number is not a port.
#derive(Eq, Debug)
pub(all) suberror PortError {
  /// The number is less than 1 or more than 65535.
  OutOfRange(Int32)
}

///|
/// A TCP port: a whole number from 1 to 65535.
#derive(Eq, Debug)
struct Port {
  value : Int32
}

///|
/// Makes a port from a number from 1 to 65535.
pub fn Port::Port(value : Int32) -> Port raise PortError {
  guard value >= 1 && value <= 65535 else { raise OutOfRange(value) }
  { value, }
}

///|
pub fn Port::to_int(self : Port) -> Int32 {
  self.value
}

///|
/// The address of a server on this port of the machine. It is also the origin of the UI.
pub fn Port::address(self : Port) -> ServerAddress {
  { port: self, }
}

///|
/// Raised when a text is not the address of a server. It has the text.
#derive(Eq, Debug)
pub(all) suberror AddressError {
  NotServerAddress(String)
}

///|
/// The address of a server of this machine, such as `http://127.0.0.1:3090`.
#derive(Eq, Debug)
struct ServerAddress {
  port : Port
}

///|
/// The part of an address before the port.
let address_start : String = "http://127.0.0.1:"

///|
/// Reads an address in the one form that `to_string` writes.
pub fn ServerAddress::ServerAddress(
  text : String,
) -> ServerAddress raise AddressError {
  guard text.strip_prefix(address_start) is Some(digits) else {
    raise NotServerAddress(text)
  }
  let address = Port(@string.parse_int(digits)).address() catch {
    _ => raise NotServerAddress(text)
  }
  // A number has more than one text, such as `3090` and `03090`.
  guard address.to_string() == text else { raise NotServerAddress(text) }
  address
}

///|
pub fn ServerAddress::to_string(self : ServerAddress) -> String {
  "\{address_start}\{self.port.value}"
}

///|
/// How an instance finds its port.
#derive(Eq, Debug)
pub(all) enum PortChoice {
  /// The instance uses this port, and fails when it is not free.
  Fixed(Port)
  /// The instance tries this port first, and then uses a free port.
  Preferred(Port)
  /// The instance uses any free port.
  Free
}

///|
/// The port that an instance name has by convention, if it has one.
/// Each number is in range, so the value is valid by construction.
fn known_port(name : @app.InstanceName) -> Port? {
  match name.to_string() {
    "stable" => Some({ value: 3090, })
    "insiders" => Some({ value: 3091, })
    "nightly" => Some({ value: 3092, })
    "dev" => Some({ value: 3093, })
    _ => None
  }
}

///|
/// The port of a server that a command starts: `Fixed` for `stable`,
/// `insiders`, `nightly` and `dev`, and `Free` for each other instance.
pub fn PortChoice::for_command(name : @app.InstanceName) -> PortChoice {
  match known_port(name) {
    Some(port) => Fixed(port)
    None => Free
  }
}

///|
/// The port of a server that the desktop app starts: `Preferred` for `stable`,
/// `insiders`, `nightly` and `dev`, and `Free` for each other instance.
pub fn PortChoice::for_desktop(name : @app.InstanceName) -> PortChoice {
  match known_port(name) {
    Some(port) => Preferred(port)
    None => Free
  }
}

///|
pub extend Port with Eq::{not_equal, equal}

///|
pub extend Port with @debug.Debug::{to_repr}