podman
108 строк · 2.2 Кб
1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package bsonrw
8
9import (
10"fmt"
11)
12
13type mode int
14
15const (
16_ mode = iota
17mTopLevel
18mDocument
19mArray
20mValue
21mElement
22mCodeWithScope
23mSpacer
24)
25
26func (m mode) String() string {
27var str string
28
29switch m {
30case mTopLevel:
31str = "TopLevel"
32case mDocument:
33str = "DocumentMode"
34case mArray:
35str = "ArrayMode"
36case mValue:
37str = "ValueMode"
38case mElement:
39str = "ElementMode"
40case mCodeWithScope:
41str = "CodeWithScopeMode"
42case mSpacer:
43str = "CodeWithScopeSpacerFrame"
44default:
45str = "UnknownMode"
46}
47
48return str
49}
50
51func (m mode) TypeString() string {
52var str string
53
54switch m {
55case mTopLevel:
56str = "TopLevel"
57case mDocument:
58str = "Document"
59case mArray:
60str = "Array"
61case mValue:
62str = "Value"
63case mElement:
64str = "Element"
65case mCodeWithScope:
66str = "CodeWithScope"
67case mSpacer:
68str = "CodeWithScopeSpacer"
69default:
70str = "Unknown"
71}
72
73return str
74}
75
76// TransitionError is an error returned when an invalid progressing a
77// ValueReader or ValueWriter state machine occurs.
78// If read is false, the error is for writing
79type TransitionError struct {
80name string
81parent mode
82current mode
83destination mode
84modes []mode
85action string
86}
87
88func (te TransitionError) Error() string {
89errString := fmt.Sprintf("%s can only %s", te.name, te.action)
90if te.destination != mode(0) {
91errString = fmt.Sprintf("%s a %s", errString, te.destination.TypeString())
92}
93errString = fmt.Sprintf("%s while positioned on a", errString)
94for ind, m := range te.modes {
95if ind != 0 && len(te.modes) > 2 {
96errString = fmt.Sprintf("%s,", errString)
97}
98if ind == len(te.modes)-1 && len(te.modes) > 1 {
99errString = fmt.Sprintf("%s or", errString)
100}
101errString = fmt.Sprintf("%s %s", errString, m.TypeString())
102}
103errString = fmt.Sprintf("%s but is positioned on a %s", errString, te.current.TypeString())
104if te.parent != mode(0) {
105errString = fmt.Sprintf("%s with parent %s", errString, te.parent.TypeString())
106}
107return errString
108}
109