hanki

19. Worked example: a Todo CLI

The full v0.1-runnable form is in examples/v0_1/todo/. hanki run examples/v0_1/todo/ drives the REPL, and hanki test examples/v0_1/todo/ runs the parse-layer assertions. The condensed listings below mirror those files. One v0.1 detail needs flagging:

# === types.hk ===

@derive(Eq)
struct Todo
  id: i32
  text: string
  done: bool
end

@derive(Eq)
type Command
  Add(string)
  Done(i32)
  List
  Quit
end

@derive(Eq)
type CommandError
  NotFound(i32)
  Parse(string)
end

impl Display<Todo>
  def to_string(self) -> string
    marker = if self.done then "[x]" else "[ ]"
    "#{marker} ##{self.id} #{self.text}"
  end
end

impl Display<CommandError>
  def to_string(self) -> string
    match self
      NotFound(id) -> "todo ##{id} not found"
      Parse(msg)   -> "parse error: #{msg}"
    end
  end
end
# === parse.hk ===
open result
open types

def parse_command(line: string) -> Result<Command, CommandError>
  if line == "quit"
    Ok(Quit)
  elif line == "list"
    Ok(List)
  elif line.starts_with?("add ")
    Ok(Add(line.slice_from(4)))
  elif line.starts_with?("done ")
    match i32.parse(line.slice_from(5))
      Ok(id)  -> Ok(Done(id))
      Err(_)  -> Err(Parse(line))
    end
  else
    Err(Parse(line))
  end
end
# === todos.hk ===
# `open types` claims bare `List` for the `Command::List` variant (a bare
# use through a second open exporting it would be ambiguous, H0306), so
# the container type is reached qualified as `list.List` - which, list
# being core, needs no import at all.
open option
open result
open types

actor Todos
  state items: list.List<Todo> = []
  state next_id: i32 = 1i32

  on add(text: string) -> ()
    items = items.append(Todo(id=next_id, text=text, done=false))
    next_id = next_id + 1i32
  end

  on mark_done(id: i32) -> Result<(), CommandError>
    found = match items.find(|t| t.id == id)
      Some(_) -> true
      None    -> false
    end
    if not found
      return Err(NotFound(id))
    end
    items = items.map |t|
      if t.id == id
        Todo(id=t.id, text=t.text, done=true)
      else
        t
      end
    end
    Ok(())
  end

  on list_all() -> list.List<Todo>
    items
  end
end
# === main.hk ===
use parse
use todos

open io
open result
# `open` collision note: both `list` and `types` export a bare `List`
# (parametric collection vs the Command::List variant). Opening both and
# using the name bare would be ambiguous (H0306); here only `types` is
# opened, so it claims `List` for the variant, and the `list.List<string>`
# type for argv is reached qualified - core, so no import needed.
open types

def main!(args: list.List<string>) -> () [io, Crash]
  todos_actor = spawn todos.Todos

  loop
    print!("> ")
    match read_line!()
      Eof        -> break
      Line(line) ->
        match parse.parse_command(line)
          Err(e)       -> print!("#{e}\n")
          Ok(Quit)     -> break
          Ok(Add(t))   -> todos_actor.add(t).or_crash!("todo store died")
          Ok(Done(id)) ->
            match todos_actor.mark_done(id).or_crash!("todo store died")
              Ok(_)  -> ()
              Err(e) -> print!("#{e}\n")
            end
          Ok(List)     ->
            todos_actor.list_all().or_crash!("todo store died").each! |t|
              print!("#{t}\n")
            end
        end
    end
  end
end