///|
/// A rule of an instance name.
#derive(Eq, Debug)
pub(all) enum InstanceNameRule {
  /// The name has 1 to 32 characters.
  Length
  /// The first character is a lower-case letter from `a` to `z`.
  FirstCharacter
  /// Each character is a lower-case letter from `a` to `z`, a digit or a hyphen.
  Characters
}

///|
/// Raised when a text is not an instance name.
#derive(Eq, Debug, ToJson, FromJson)
pub(all) suberror InstanceNameError {
  /// The text, and the first rule that it breaks.
  InvalidInstanceName(text~ : String, rule~ : InstanceNameRule)
}

///|
/// The name of one separate copy of Mission Control, such as `stable` or
/// `review-42`. It is safe as the name of a folder.
#derive(Eq, Debug)
struct InstanceName {
  text : String
}

///|
/// Parses an instance name: 1 to 32 lower-case letters, digits and hyphens,
/// with a letter at the start.
pub fn InstanceName::InstanceName(
  text : String,
) -> InstanceName raise InstanceNameError {
  let length = text.iter().count()
  guard length >= 1 && length <= 32 else {
    raise InvalidInstanceName(text~, rule=Length)
  }
  guard text.iter().head() is Some('a'..<='z') else {
    raise InvalidInstanceName(text~, rule=FirstCharacter)
  }
  guard text.iter().all(c => c is ('a'..<='z' | '0'..<='9' | '-')) else {
    raise InvalidInstanceName(text~, rule=Characters)
  }
  { text, }
}

///|
/// The instance that a build of the channel uses, if the user gives no name.
pub fn InstanceName::of_channel(channel : @contracts.Channel) -> InstanceName {
  // A test checks that each channel name is an instance name.
  { text: channel.name(), }
}

///|
/// The instance of a development build, `dev`.
pub fn InstanceName::dev() -> InstanceName {
  { text: "dev", }
}

///|
pub fn InstanceName::to_string(self : InstanceName) -> String {
  self.text
}

///|
pub impl ToJson for InstanceName with fn to_json(self) {
  Json::string(self.text)
}

///|
pub impl @json.FromJson for InstanceName with fn from_json(json, path) {
  let text : String = @json.from_json(json, path~)
  InstanceName::InstanceName(text) catch {
    InvalidInstanceName(text~, rule~) => {
      let problem = match rule {
        Length => "does not have 1 to 32 characters"
        FirstCharacter => "does not start with a lower-case letter"
        Characters =>
          "has a character that is not a lower-case letter, a digit or a hyphen"
      }
      raise path.error("the instance name \"\{text}\" \{problem}")
    }
  }
}

///|
fn InstanceNameRule::json_name(self : InstanceNameRule) -> String {
  match self {
    Length => "length"
    FirstCharacter => "first_character"
    Characters => "characters"
  }
}

///|
pub impl ToJson for InstanceNameRule with fn to_json(self) {
  Json::string(self.json_name())
}

///|
pub impl @json.FromJson for InstanceNameRule with fn from_json(json, path) {
  case_of_name(
    json,
    path,
    [Length, FirstCharacter, Characters],
    InstanceNameRule::json_name,
  )
}

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

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

///|
pub extend InstanceNameRule with ToJson::{to_json}

///|
pub extend InstanceNameRule with @json.FromJson::{from_json}

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

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

///|
pub extend InstanceNameError with ToJson::{to_json}

///|
pub extend InstanceNameError with @json.FromJson::{from_json}

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

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

///|
pub extend InstanceName with ToJson::{to_json}

///|
pub extend InstanceName with @json.FromJson::{from_json}