go-mail/reader.go
Winni Neessen b2f735854d
Refactor variable names for readability in reader.go
Variable names in reader.go have been changed to be more expressive for improved readability. 'buf' and 'off' were renamed to 'buffer' and 'offset' respectively throughout the file. The changes also include corresponding adjustments in code comments and related test cases, contributing to consistent and understandable code.
2024-02-26 00:56:29 +01:00

48 lines
1.2 KiB
Go

// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors
//
// SPDX-License-Identifier: MIT
package mail
import (
"io"
)
// Reader is a type that implements the io.Reader interface for a Msg
type Reader struct {
buffer []byte // contents are the bytes buffer[offset : len(buffer)]
offset int // read at &buffer[offset], write at &buffer[len(buffer)]
err error // initialization error
}
// Error returns an error if the Reader err field is not nil
func (r *Reader) Error() error {
return r.err
}
// Read reads the length of p of the Msg buffer to satisfy the io.Reader interface
func (r *Reader) Read(payload []byte) (n int, err error) {
if r.err != nil {
return 0, r.err
}
if r.empty() || r.buffer == nil {
r.Reset()
if len(payload) == 0 {
return 0, nil
}
return 0, io.EOF
}
n = copy(payload, r.buffer[r.offset:])
r.offset += n
return n, err
}
// Reset resets the Reader buffer to be empty, but it retains the underlying storage
// for use by future writes.
func (r *Reader) Reset() {
r.buffer = r.buffer[:0]
r.offset = 0
}
// empty reports whether the unread portion of the Reader buffer is empty.
func (r *Reader) empty() bool { return len(r.buffer) <= r.offset }