1 /*
2  * hunt-proton: AMQP Protocol library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 
13 module hunt.proton.codec.messaging.RejectedType;
14 
15 import hunt.collection.AbstractList;
16 import hunt.collection.List;
17 import hunt.Object;
18 import hunt.Exceptions;
19 import hunt.proton.amqp.Symbol;
20 import hunt.proton.amqp.UnsignedLong;
21 import hunt.proton.amqp.messaging.Rejected;
22 import hunt.proton.codec.AbstractDescribedType;
23 import hunt.proton.codec.Decoder;
24 import hunt.proton.codec.DescribedTypeConstructor;
25 import hunt.proton.codec.EncoderImpl;
26 import hunt.proton.amqp.transport.ErrorCondition;
27 
28 import std.concurrency : initOnce;
29 import std.conv:to;
30 import hunt.Exceptions;
31 
32 class RejectedWrapper : AbstractList!Object
33 {
34     private Rejected _impl;
35 
36     this(Rejected impl)
37     {
38         _impl = impl;
39     }
40 
41     override
42     public Object get(int index)
43     {
44 
45         switch(index)
46         {
47             case 0:
48             return _impl.getError();
49             default:
50             return null;
51         }
52 
53         //  throw new IllegalStateException("Unknown index " ~ to!string(index));
54 
55     }
56 
57     override
58     public int size()
59     {
60         return _impl.getError() !is null
61         ? 1
62         : 0;
63 
64     }
65 }
66 
67 class RejectedType  : AbstractDescribedType!(Rejected,List!Object) , DescribedTypeConstructor!(Rejected)
68 {
69     //private static Object[] DESCRIPTORS =
70     //{
71     //    UnsignedLong.valueOf(0x0000000000000025L), Symbol.valueOf("amqp:rejected:list"),
72     //};
73 
74    // private static UnsignedLong DESCRIPTOR = UnsignedLong.valueOf(0x0000000000000025L);
75 
76 
77     static Object[]  DESCRIPTORS() {
78         __gshared Object[]  inst;
79         return initOnce!inst([UnsignedLong.valueOf(0x0000000000000025L), Symbol.valueOf("amqp:rejected:list")]);
80     }
81 
82     static UnsignedLong  DESCRIPTOR() {
83         __gshared UnsignedLong  inst;
84         return initOnce!inst(UnsignedLong.valueOf(0x0000000000000025L));
85     }
86 
87     this(EncoderImpl encoder)
88     {
89         super(encoder);
90     }
91 
92     override
93     public UnsignedLong getDescriptor()
94     {
95         return DESCRIPTOR;
96     }
97 
98     override
99     protected List!Object wrap(Rejected val)
100     {
101         return new RejectedWrapper(val);
102     }
103 
104 
105 
106 
107     public Rejected newInstance(Object described)
108     {
109         List!Object l = cast(List!Object) described;
110 
111         Rejected o = new Rejected();
112 
113         switch(1 - l.size())
114         {
115             case 0:
116                 o.setError( cast(ErrorCondition) l.get( 0 ) );
117                 break;
118             default:
119                 break;
120         }
121 
122 
123         return o;
124     }
125 
126     public TypeInfo getTypeClass()
127     {
128         return typeid(Rejected);
129     }
130 
131 
132     public static void register(Decoder decoder, EncoderImpl encoder)
133     {
134         RejectedType type = new RejectedType(encoder);
135         //implementationMissing(false);
136         foreach(Object descriptor ; DESCRIPTORS)
137         {
138             decoder.registerDynamic(descriptor, type);
139         }
140         encoder.register(type);
141     }
142 }
143