1// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Package xmpp implements the XMPP IM protocol, as specified in RFC 6120 and
6// 6121.
7package xmpp
8
9import "github.com/coyim/coyim/xmpp/data"
10
11type xep0115Sorter struct{ s []interface{} }
12
13func (s *xep0115Sorter) add(c interface{})  { s.s = append(s.s, c) }
14func (s *xep0115Sorter) Len() int           { return len(s.s) }
15func (s *xep0115Sorter) Swap(i, j int)      { s.s[i], s.s[j] = s.s[j], s.s[i] }
16func (s *xep0115Sorter) Less(i, j int) bool { return xep0115Less(s.s[i], s.s[j]) }
17
18func xep0115Less(a interface{}, other interface{}) bool {
19	switch v := a.(type) {
20	case *data.DiscoveryIdentity:
21		return xep0115LessDI(v, other)
22	case *data.DiscoveryFeature:
23		return xep0115LessDF(v, other)
24	case *data.FormFieldX:
25		return xep0115LessFF(v, other)
26	}
27	return false
28}
29
30func xep0115LessDI(a *data.DiscoveryIdentity, other interface{}) bool {
31	b := other.(*data.DiscoveryIdentity)
32	if a.Category != b.Category {
33		return a.Category < b.Category
34	}
35	if a.Type != b.Type {
36		return a.Type < b.Type
37	}
38	return a.Lang < b.Lang
39}
40
41func xep0115LessDF(a *data.DiscoveryFeature, other interface{}) bool {
42	b := other.(*data.DiscoveryFeature)
43	return a.Var < b.Var
44}
45
46func xep0115LessFF(a *data.FormFieldX, other interface{}) bool {
47	b := other.(*data.FormFieldX)
48	if a.Var == "FORM_TYPE" {
49		return true
50	} else if b.Var == "FORM_TYPE" {
51		return false
52	}
53	return a.Var < b.Var
54}
55