// Package tasks keeps a to-do list and its file format. It knows nothing about // the command line: main.go does the arguments and the printing. // // The tests in tasks_test.go describe what every function must do. Run them // from the project folder with: go test ./... package tasks import ( "errors" "os" "time" ) // dateLayout is how a creation date is written in the file. const dateLayout = "2006-01-02" // ErrEmptyText means a task had no text. var ErrEmptyText = errors.New("a task needs some text") // ErrNoTask means a task number did not match any task. var ErrNoTask = errors.New("no such task") // Task is one thing to do. type Task struct { Text string Done bool Created time.Time } // List holds the tasks in order. Task numbers start at 1. type List struct { tasks []Task } // Tasks returns a copy of the tasks, so a caller cannot change the list by // accident. func (l *List) Tasks() []Task { copied := make([]Task, len(l.tasks)) copy(copied, l.tasks) return copied } // Add puts a new task at the end and returns its number. now is the time to // record as its creation date. func (l *List) Add(text string, now time.Time) (int, error) { // TODO: trim the text. Return ErrEmptyText if nothing is left, and an // error if it holds a tab or a line break (strings.ContainsAny). Append a // Task with Created set to calendarDay(now), and return its number. return 0, errors.New("TODO: Add is not written yet") } // Done marks task n as done and returns it. func (l *List) Done(n int) (Task, error) { // TODO: check n with l.check. A task that is already done is an error. // Otherwise set Done to true and return the task. return Task{}, errors.New("TODO: Done is not written yet") } // Remove takes task n out of the list and returns it. The tasks after it // move up by one number. func (l *List) Remove(n int) (Task, error) { // TODO: check n, keep the task, take it out with slices.Delete, and // return it. return Task{}, errors.New("TODO: Remove is not written yet") } // check returns an error that wraps ErrNoTask if n is not a task number. func (l *List) check(n int) error { // TODO: n must be from 1 to len(l.tasks). Wrap ErrNoTask with %w and the // number, so the message reads "task 9: no such task". return nil } // Age describes how long ago a task was created, as seen from now: "today", // "yesterday" or "3 days ago". func Age(created time.Time, now time.Time) string { // TODO: count the calendar days between the two, as in Chapter 35. return "TODO" } // calendarDay keeps only the year, month and day of t, at midnight UTC. A day // in UTC is always exactly 24 hours long, so days can be counted by dividing. func calendarDay(t time.Time) time.Time { return time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.UTC) } // Load reads a list from a file. A file that does not exist yet gives an // empty list. func Load(filename string) (*List, error) { // TODO: open the file. os.ErrNotExist gives an empty list. Read it line // by line with a bufio.Scanner, skip empty lines, and turn each line into // a Task with parseLine. Wrap a bad line's error with the file name and // the line number: "todo.txt line 3: ...". Check scanner.Err at the end. return &List{}, nil } // Save writes the list to a file, one task per line. func (l *List) Save(filename string) error { text := "" for _, task := range l.tasks { text += formatLine(task) + "\n" } return os.WriteFile(filename, []byte(text), 0644) } // formatLine turns a task into one line of the file: the state, the creation // date and the text, separated by tabs. // // todo 2025-08-17 buy milk func formatLine(task Task) string { state := "todo" if task.Done { state = "done" } return state + "\t" + task.Created.Format(dateLayout) + "\t" + task.Text } // parseLine is the opposite of formatLine. func parseLine(line string) (Task, error) { // TODO: split the line at the tabs. It needs exactly 3 parts. The state is // "todo" or "done", the date parses with dateLayout, and the text is not // empty. Return a clear error for each problem. return Task{}, errors.New("TODO: parseLine is not written yet") }