Debian Codegraph @

versions.go

raw
1 // Copyright (c) 2022 Tulir Asokan
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 package mautrix
8
9 import (
10 "fmt"
11 "regexp"
12 "strconv"
13 )
14
15 // RespVersions is the JSON response for https://spec.matrix.org/v1.2/client-server-api/#get_matrixclientversions
16 type RespVersions struct {
17 Versions []SpecVersion `json:"versions"`
18 UnstableFeatures map[string]bool `json:"unstable_features"`
19 }
20
21 func (versions *RespVersions) ContainsFunc(match func(found SpecVersion) bool) bool {
22 for _, found := range versions.Versions {
23 if match(found) {
24 return true
25 }
26 }
27 return false
28 }
29
30 func (versions *RespVersions) Contains(version SpecVersion) bool {
31 return versions.ContainsFunc(func(found SpecVersion) bool {
32 return found == version
33 })
34 }
35
36 func (versions *RespVersions) ContainsGreaterOrEqual(version SpecVersion) bool {
37 return versions.ContainsFunc(func(found SpecVersion) bool {
38 return found.GreaterThan(version) || found == version
39 })
40 }
41
42 type SpecVersionFormat int
43
44 const (
45 SpecVersionFormatUnknown SpecVersionFormat = iota
46 SpecVersionFormatR
47 SpecVersionFormatV
48 )
49
50 var (
51 SpecR010 = MustParseSpecVersion("r0.1.0")
52 SpecR020 = MustParseSpecVersion("r0.2.0")
53 SpecR030 = MustParseSpecVersion("r0.3.0")
54 SpecR040 = MustParseSpecVersion("r0.4.0")
55 SpecR050 = MustParseSpecVersion("r0.5.0")
56 SpecR060 = MustParseSpecVersion("r0.6.0")
57 SpecR061 = MustParseSpecVersion("r0.6.1")
58 SpecV11 = MustParseSpecVersion("v1.1")
59 SpecV12 = MustParseSpecVersion("v1.2")
60 )
61
62 func (svf SpecVersionFormat) String() string {
63 switch svf {
64 case SpecVersionFormatR:
65 return "r"
66 case SpecVersionFormatV:
67 return "v"
68 default:
69 return ""
70 }
71 }
72
73 type SpecVersion struct {
74 Format SpecVersionFormat
75 Major int
76 Minor int
77 Patch int
78
79 Raw string
80 }
81
82 var legacyVersionRegex = regexp.MustCompile(`^r(\d+)\.(\d+)\.(\d+)$`)
83 var modernVersionRegex = regexp.MustCompile(`^v(\d+)\.(\d+)$`)
84
85 func MustParseSpecVersion(version string) SpecVersion {
86 sv, err := ParseSpecVersion(version)
87 if err != nil {
88 panic(err)
89 }
90 return sv
91 }
92
93 func ParseSpecVersion(version string) (sv SpecVersion, err error) {
94 sv.Raw = version
95 if parts := modernVersionRegex.FindStringSubmatch(version); parts != nil {
96 sv.Major, _ = strconv.Atoi(parts[1])
97 sv.Minor, _ = strconv.Atoi(parts[2])
98 sv.Format = SpecVersionFormatV
99 } else if parts = legacyVersionRegex.FindStringSubmatch(version); parts != nil {
100 sv.Major, _ = strconv.Atoi(parts[1])
101 sv.Minor, _ = strconv.Atoi(parts[2])
102 sv.Patch, _ = strconv.Atoi(parts[3])
103 sv.Format = SpecVersionFormatR
104 } else {
105 err = fmt.Errorf("version '%s' doesn't match either known syntax", version)
106 }
107 return
108 }
109
110 func (sv *SpecVersion) UnmarshalText(version []byte) error {
111 *sv, _ = ParseSpecVersion(string(version))
112 return nil
113 }
114
115 func (sv *SpecVersion) MarshalText() ([]byte, error) {
116 return []byte(sv.String()), nil
117 }
118
119 func (sv *SpecVersion) String() string {
120 switch sv.Format {
121 case SpecVersionFormatR:
122 return fmt.Sprintf("r%d.%d.%d", sv.Major, sv.Minor, sv.Patch)
123 case SpecVersionFormatV:
124 return fmt.Sprintf("v%d.%d", sv.Major, sv.Minor)
125 default:
126 return sv.Raw
127 }
128 }
129
130 func (sv SpecVersion) LessThan(other SpecVersion) bool {
131 return sv != other && !sv.GreaterThan(other)
132 }
133
134 func (sv SpecVersion) GreaterThan(other SpecVersion) bool {
135 return sv.Format > other.Format ||
136 (sv.Format == other.Format && sv.Major > other.Major) ||
137 (sv.Format == other.Format && sv.Major == other.Major && sv.Minor > other.Minor) ||
138 (sv.Format == other.Format && sv.Major == other.Major && sv.Minor == other.Minor && sv.Patch > other.Patch)
139 }