@@ -9,20 +9,32 @@ defmodule BSON.Binary do
9
9
}
10
10
defstruct [ binary: nil , subtype: :generic ]
11
11
12
- defimpl Inspect do
13
- def inspect ( % BSON.Binary { binary: value , subtype: :generic } , _opts ) do
14
- "#BSON.Binary<#{ Base . encode16 ( value , case: :lower ) } >"
15
- end
16
- def inspect ( % BSON.Binary { binary: value , subtype: :uuid } , _opts ) do
12
+ defimpl String.Chars , for: BSON.Binary do
13
+ def to_string ( % BSON.Binary { binary: value , subtype: subtype } ) when subtype in [ :uuid , :uuid_old ] do
17
14
p1 = binary_part ( value , 0 , 4 )
18
15
p2 = binary_part ( value , 4 , 2 )
19
16
p3 = binary_part ( value , 6 , 2 )
20
17
p4 = binary_part ( value , 8 , 2 )
21
18
p5 = binary_part ( value , 10 , 6 )
22
- "#BSON.UUID<#{ Base . encode16 ( p1 , case: :lower ) } -#{ Base . encode16 ( p2 , case: :lower ) } -#{ Base . encode16 ( p3 , case: :lower ) } -#{ Base . encode16 ( p4 , case: :lower ) } -#{ Base . encode16 ( p5 , case: :lower ) } >"
19
+ "#{ Base . encode16 ( p1 , case: :lower ) } -#{ Base . encode16 ( p2 , case: :lower ) } -#{ Base . encode16 ( p3 , case: :lower ) } -#{ Base . encode16 ( p4 , case: :lower ) } -#{ Base . encode16 ( p5 , case: :lower ) } "
20
+ end
21
+ def to_string ( % BSON.Binary { binary: value } ) do
22
+ Base . encode16 ( value , case: :lower )
23
+ end
24
+ end
25
+
26
+ defimpl Inspect do
27
+ def inspect ( % BSON.Binary { subtype: :generic } = binary , _opts ) do
28
+ "#BSON.Binary<#{ to_string ( binary ) } >"
29
+ end
30
+ def inspect ( % BSON.Binary { subtype: :uuid } = binary , _opts ) do
31
+ "#BSON.UUID<#{ to_string ( binary ) } >"
32
+ end
33
+ def inspect ( % BSON.Binary { subtype: :uuid_old } = binary , _opts ) do
34
+ "#BSON.LUUID<#{ to_string ( binary ) } >"
23
35
end
24
- def inspect ( % BSON.Binary { binary: value , subtype: subtype } , _opts ) do
25
- "#BSON.Binary<#{ Base . encode16 ( value , case: :lower ) } , #{ subtype } >"
36
+ def inspect ( % BSON.Binary { subtype: subtype } = binary , _opts ) do
37
+ "#BSON.Binary<#{ to_string ( binary ) } , #{ subtype } >"
26
38
end
27
39
end
28
40
end
0 commit comments