bc_ur/
multipart_decoder.rs1use dcbor::prelude::*;
2
3use crate::{ URType, UR, Result, Error };
4
5pub struct MultipartDecoder {
6 ur_type: Option<URType>,
7 decoder: ur::Decoder,
8}
9
10impl MultipartDecoder {
11 pub fn new() -> Self {
12 Self {
13 ur_type: None,
14 decoder: ur::Decoder::default(),
15 }
16 }
17}
18
19impl Default for MultipartDecoder {
20 fn default() -> Self {
21 Self::new()
22 }
23}
24
25impl MultipartDecoder {
26 pub fn receive(&mut self, value: &str) -> Result<()> {
27 let decoded_type = Self::decode_type(value)?;
28 if let Some(ur_type) = &self.ur_type {
29 if ur_type != &decoded_type {
30 return Err(
31 Error::UnexpectedType(
32 ur_type.string().to_string(),
33 decoded_type.string().to_string()
34 )
35 );
36 }
37 } else {
38 self.ur_type = Some(decoded_type);
39 }
40 Ok(self.decoder.receive(value)?)
41 }
42
43 pub fn is_complete(&self) -> bool {
44 self.decoder.complete()
45 }
46
47 pub fn message(&self) -> Result<Option<UR>> {
48 let message_data = self.decoder.message()?;
49 if let Some(data) = message_data {
50 let cbor = CBOR::try_from_data(data)?;
51 let ur_type = self.ur_type.as_ref().unwrap();
52 let ur_type_string = ur_type.string();
53 let ur = UR::new(ur_type_string, cbor)?;
54 Ok(Some(ur))
55 } else {
56 Ok(None)
57 }
58 }
59
60 fn decode_type(ur_string: &str) -> Result<URType> {
61 let without_scheme = ur_string.strip_prefix("ur:").ok_or(Error::InvalidScheme)?;
62 let first_component = without_scheme.split('/').next().ok_or(Error::InvalidType)?;
63 URType::new(first_component)
64 }
65}