@@ -13,7 +13,8 @@ use serde::{Deserialize, Serialize};
13
13
use thiserror:: Error ;
14
14
15
15
static GENERATED_DEVICE_ID_LENGTH : usize = 10 ;
16
- static DEVICE_SCOPE_PREFIX : & str = "urn:matrix:org.matrix.msc2967.client:device:" ;
16
+ static UNSTABLE_DEVICE_SCOPE_PREFIX : & str = "urn:matrix:org.matrix.msc2967.client:device:" ;
17
+ static STABLE_DEVICE_SCOPE_PREFIX : & str = "urn:matrix:client:device:" ;
17
18
18
19
#[ derive( Debug , Clone , PartialEq , Eq , Serialize , Deserialize ) ]
19
20
#[ serde( transparent) ]
@@ -28,24 +29,31 @@ pub enum ToScopeTokenError {
28
29
}
29
30
30
31
impl Device {
31
- /// Get the corresponding [`ScopeToken`] for that device
32
+ /// Get the corresponding stable and unstable [`ScopeToken`] for that device
32
33
///
33
34
/// # Errors
34
35
///
35
36
/// Returns an error if the device ID contains characters that can't be
36
37
/// encoded in a scope
37
- pub fn to_scope_token ( & self ) -> Result < ScopeToken , ToScopeTokenError > {
38
- format ! ( "{DEVICE_SCOPE_PREFIX}{}" , self . id)
39
- . parse ( )
40
- . map_err ( |_| ToScopeTokenError :: InvalidCharacters )
38
+ pub fn to_scope_token ( & self ) -> Result < [ ScopeToken ; 2 ] , ToScopeTokenError > {
39
+ Ok ( [
40
+ format ! ( "{STABLE_DEVICE_SCOPE_PREFIX}{}" , self . id)
41
+ . parse ( )
42
+ . map_err ( |_| ToScopeTokenError :: InvalidCharacters ) ?,
43
+ format ! ( "{UNSTABLE_DEVICE_SCOPE_PREFIX}{}" , self . id)
44
+ . parse ( )
45
+ . map_err ( |_| ToScopeTokenError :: InvalidCharacters ) ?,
46
+ ] )
41
47
}
42
48
43
49
/// Get the corresponding [`Device`] from a [`ScopeToken`]
44
50
///
45
51
/// Returns `None` if the [`ScopeToken`] is not a device scope
46
52
#[ must_use]
47
53
pub fn from_scope_token ( token : & ScopeToken ) -> Option < Self > {
48
- let id = token. as_str ( ) . strip_prefix ( DEVICE_SCOPE_PREFIX ) ?;
54
+ let stable = token. as_str ( ) . strip_prefix ( STABLE_DEVICE_SCOPE_PREFIX ) ;
55
+ let unstable = token. as_str ( ) . strip_prefix ( UNSTABLE_DEVICE_SCOPE_PREFIX ) ;
56
+ let id = stable. or ( unstable) ?;
49
57
Some ( Device :: from ( id. to_owned ( ) ) )
50
58
}
51
59
@@ -89,12 +97,23 @@ mod test {
89
97
#[ test]
90
98
fn test_device_id_to_from_scope_token ( ) {
91
99
let device = Device :: from ( "AABBCCDDEE" . to_owned ( ) ) ;
92
- let scope_token = device. to_scope_token ( ) . unwrap ( ) ;
100
+ let [ stable_scope_token , unstable_scope_token ] = device. to_scope_token ( ) . unwrap ( ) ;
93
101
assert_eq ! (
94
- scope_token. as_str( ) ,
102
+ stable_scope_token. as_str( ) ,
103
+ "urn:matrix:client:device:AABBCCDDEE"
104
+ ) ;
105
+ assert_eq ! (
106
+ unstable_scope_token. as_str( ) ,
95
107
"urn:matrix:org.matrix.msc2967.client:device:AABBCCDDEE"
96
108
) ;
97
- assert_eq ! ( Device :: from_scope_token( & scope_token) , Some ( device) ) ;
109
+ assert_eq ! (
110
+ Device :: from_scope_token( & unstable_scope_token) . as_ref( ) ,
111
+ Some ( & device)
112
+ ) ;
113
+ assert_eq ! (
114
+ Device :: from_scope_token( & stable_scope_token) . as_ref( ) ,
115
+ Some ( & device)
116
+ ) ;
98
117
assert_eq ! ( Device :: from_scope_token( & OPENID ) , None ) ;
99
118
}
100
119
}
0 commit comments