package storage import ( "fmt" "galaxy/model/client" "galaxy/model/order" "galaxy/model/report" "galaxy/util" ) type Storage interface { UIStorage Exists(string) (bool, error) ReadFile(string) ([]byte, error) WriteFile(string, []byte) error } // UIStorage manages Client's data local storing and retrieval. // It performs all I/O operations asynchronously to avoid UI main thread blocking. type UIStorage interface { // StateExists check asynchronously for previously saved [model.State] exists on the filesystem. // Passed callback func will will accept false and non-nil error in case of I/O or decoding errors occuried, // otherwise bool parameter will indicate existence of previously stores state StateExists(func(bool, error)) // LoadState loads Client's [model.State] from filesystem data asynchronously. // Passed callback func will accept non-nil error in case of I/O or decoding errors occuried, // otherwise callback func accepts loaded [model.State]. LoadState(func(client.State, error)) // SaveState stores Client's state at the filesystem asynchronously. // I/O or encoding error may occur, it that case callback func will be called with non-nil error. SaveState(client.State, func(error)) // LoadReport loads a [report.Report] for a given [model.GameID] and turn number from filesystem asynchronously. // Passed callback func will will accept non-nil error in case of I/O or decoding errors occuried, // otherwise callback func accepts loaded [report.Report]. LoadReport(client.GameID, uint, func(report.Report, error)) // PutReport stores given [report.Report] for a given [model.GameID] and turn number at the filesystem asynchronously. // I/O or encoding error may occur, it that case callback func will be called with non-nil error. PutReport(client.GameID, uint, report.Report, func(error)) // LoadOrder loads a [order.Order] for a given [model.GameID] and turn number from filesystem asynchronously. // Passed callback func will will accept non-nil error in case of I/O or decoding errors occuried, // otherwise callback func accepts loaded [order.Order]. LoadOrder(client.GameID, uint, func(order.Order, error)) // PutOrder stores given [order.Order] for a given [model.GameID] and turn number at the filesystem asynchronously. // I/O or encoding error may occur, it that case callback func will be called with non-nil error. PutOrder(client.GameID, uint, order.Order, func(error)) } type storage struct { path string } func NewStorage(path string) (*storage, error) { if ok, err := util.PathExists(path, true); err != nil { return nil, fmt.Errorf("new storage: check path %q exists: %w", path, err) } else if !ok { return nil, fmt.Errorf("new storage: path %q does not exists", path) } if ok, err := util.Writable(path); err != nil { return nil, fmt.Errorf("new storage: check path %q writable: %w", path, err) } else if !ok { return nil, fmt.Errorf("new storage: path %q is not writable", path) } s := &storage{ path: path, } return s, nil }