ドキュメントを英語に修正、または追加。

This commit is contained in:
taknb2nch
2014-02-15 16:13:55 +09:00
parent fcb8d98049
commit 86c1140e65
2 changed files with 55 additions and 29 deletions

View File

@@ -9,18 +9,21 @@ import (
"strings"
)
// A ResponseError describes a protocol violation such as an invalid response or a hung-up connection.
type ResponseError string
func (r ResponseError) Error() string {
return string(r)
}
// A Conn represents a textual network protocol connection for POP3.
type Conn struct {
Reader
Writer
conn io.ReadWriteCloser
}
// NewConn returns a new Conn using conn for I/O.
func NewConn(conn io.ReadWriteCloser) *Conn {
return &Conn{
Reader: Reader{R: textproto.NewReader(bufio.NewReader(conn))},
@@ -29,18 +32,25 @@ func NewConn(conn io.ReadWriteCloser) *Conn {
}
}
// Close closes the connection.
func (c *Conn) Close() error {
return c.conn.Close()
}
// A Reader implements convenience methods
// for reading requests or responses from a text protocol network connection.
type Reader struct {
R *textproto.Reader
}
// NewReader returns a new Reader reading from r.
func NewReader(r *bufio.Reader) *Reader {
return &Reader{R: textproto.NewReader(r)}
}
// ReadLine reads a single line from r,
// eliding the final \n or \r\n from the returned string.
// This calls textproto.Reader.ReadLine simply.
func (r *Reader) ReadLine() (string, error) {
return r.R.ReadLine()
// for debug
@@ -49,6 +59,9 @@ func (r *Reader) ReadLine() (string, error) {
// return l, err
}
// ReadLines reads a multiline until the last line of the only period,
// and returns a each line at slice.
// it does not contain last period.
func (r *Reader) ReadLines() ([]string, error) {
var lines []string
var line string
@@ -69,6 +82,9 @@ func (r *Reader) ReadLines() ([]string, error) {
}
}
// ReadToPeriod reads a multiline until the last line of the only period,
// and returns as a string.
// it does not contain last period.
func (r *Reader) ReadToPeriod() (string, error) {
lines, err := r.ReadLines()
@@ -79,6 +95,10 @@ func (r *Reader) ReadToPeriod() (string, error) {
return strings.Join(lines, "\r\n"), nil
}
// ReadResponse reads a single line from r,
// and parses reponse.
// if the response is -ERR or has some other errors,
// it returns error.
func (r *Reader) ReadResponse() (string, error) {
line, err := r.ReadLine()
@@ -93,7 +113,7 @@ func (r *Reader) parseResponse(line string) (string, error) {
var index int
if index = strings.Index(line, " "); index < 0 {
return "", ResponseError(fmt.Sprintf("レスポンスのフォーマットが不正です。: %s", line))
return "", ResponseError(fmt.Sprintf("invalid response format: %s", line))
}
switch strings.ToUpper(line[:index]) {
@@ -102,20 +122,24 @@ func (r *Reader) parseResponse(line string) (string, error) {
case "-ERR":
return "", ResponseError(line[index+1:])
default:
return "", ResponseError(fmt.Sprintf("レスポンスの内容が不明です。: %s", line))
return "", ResponseError(fmt.Sprintf("unknown response: %s", line))
}
}
var crnl = []byte{'\r', '\n'}
// A Writer implements convenience methods
// for writing requests or responses to a text protocol network connection.
type Writer struct {
W *bufio.Writer
}
// NewWriter returns a new Writer writing to w.
func NewWriter(w *bufio.Writer) *Writer {
return &Writer{W: w}
}
// WriteLine writes the formatted output followed by \r\n.
func (w *Writer) WriteLine(format string, args ...interface{}) error {
var err error