Add initial notebook data structure (#4)

* Add initial notebook data structure

* Move required frields out of metadata
This commit is contained in:
Jonatan Kłosko 2021-01-08 15:24:13 +01:00 committed by GitHub
parent 5cdcb15e3d
commit 464e30fa98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 113 additions and 0 deletions

39
lib/live_book/notebook.ex Normal file
View file

@ -0,0 +1,39 @@
defmodule LiveBook.Notebook do
@moduledoc """
Data structure representing a notebook.
A notebook it's just the representation and roughly
maps to a file that the user can edit.
A notebook *session* is a living process that holds a specific
notebook instance and allows users to collaboratively apply
changes to this notebook.
A notebook is divided into a set of isolated *sections*.
"""
defstruct [:name, :version, :sections, :metadata]
alias LiveBook.Notebook.Section
@type t :: %__MODULE__{
name: String.t(),
version: String.t(),
sections: list(Section.t()),
metadata: %{atom() => term()}
}
@version "1.0"
@doc """
Returns a blank notebook.
"""
@spec new() :: t()
def new() do
%__MODULE__{
name: "Untitled notebook",
version: @version,
sections: [],
metadata: %{}
}
end
end

View file

@ -0,0 +1,39 @@
defmodule LiveBook.Notebook.Cell do
@moduledoc """
Data structure representing a single cell in a notebook.
A cell is the smallest unit of work in a notebook.
It primarly consists of text content that the user can edit
and may potentially produce some output (e.g. during code execution).
"""
defstruct [:id, :type, :source, :outputs, :metadata]
alias LiveBook.Utils
@type cell_id :: Utils.id()
@type cell_type :: :markdown | :elixir
@type t :: %__MODULE__{
id: cell_id(),
type: cell_type(),
source: String.t(),
# TODO: expand on this
outputs: list(),
metadata: %{atom() => term()}
}
@doc """
Returns an empty cell of the given type.
"""
@spec new(cell_type()) :: t()
def new(type) do
%__MODULE__{
id: Utils.random_id(),
type: type,
source: "",
outputs: [],
metadata: %{}
}
end
end

View file

@ -0,0 +1,35 @@
defmodule LiveBook.Notebook.Section do
@moduledoc """
Data structure representing a single section in a notebook.
Each section contains a number of cells and is isolated
in the sense that cells don't interfere with cells in other sections.
"""
defstruct [:id, :name, :cells, :metadata]
alias LiveBook.Notebook.Cell
alias LiveBook.Utils
@type section_id :: Utils.id()
@type t :: %__MODULE__{
id: section_id(),
name: String.t(),
cells: list(Cell.t()),
metadata: %{atom() => term()}
}
@doc """
Returns a blank section.
"""
@spec new() :: t()
def new() do
%__MODULE__{
id: Utils.random_id(),
name: "Section",
cells: [],
metadata: %{}
}
end
end