BlueGit

ict rusty-teams

Files

rusty-teams / crates / session / src / scope.rs

scope.rs

scope.rs 2.0 KiB Download Raw
1use std::borrow::Cow;
2
3use crate::app::App;
4
5pub const GRAPH: &str = "https://graph.microsoft.com";
6pub const IC3: &str = "https://ic3.teams.office.com";
7pub const PRESENCE: &str = "https://presence.teams.microsoft.com";
8pub const OUTLOOK: &str = "https://outlook.office.com";
9pub const SPACES: &str = "https://api.spaces.skype.com";
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Scope {
13 pub resource: Cow<'static, str>,
14 pub name: Cow<'static, str>,
15}
16
17impl Scope {
18 pub fn new(resource: impl Into<Cow<'static, str>>, name: impl Into<Cow<'static, str>>) -> Self {
19 Scope {
20 resource: resource.into(),
21 name: name.into(),
22 }
23 }
24
25 pub fn graph(name: impl Into<Cow<'static, str>>) -> Self {
26 Scope::new(GRAPH, name)
27 }
28
29 /// Tabs to try, best first. The other tab is the fallback, as in the Python client.
30 pub fn tab_order(&self) -> [App; 2] {
31 if self.resource == GRAPH && self.name.starts_with("Chat.") {
32 [App::Outlook, App::Teams]
33 } else {
34 [App::Teams, App::Outlook]
35 }
36 }
37}
38
39impl std::fmt::Display for Scope {
40 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41 write!(formatter, "{}/{}", self.resource, self.name)
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48
49 #[test]
50 fn chat_scopes_prefer_outlook() {
51 assert_eq!(Scope::graph("Chat.Read").tab_order(), [App::Outlook, App::Teams]);
52 assert_eq!(Scope::graph("Chat.ReadWrite").tab_order(), [App::Outlook, App::Teams]);
53 }
54
55 #[test]
56 fn other_scopes_prefer_teams() {
57 assert_eq!(Scope::graph("ChannelMessage.Read.All").tab_order(), [App::Teams, App::Outlook]);
58 assert_eq!(Scope::new(IC3, "Teams.AccessAsUser.All").tab_order(), [App::Teams, App::Outlook]);
59 assert_eq!(Scope::new(PRESENCE, "Chat.Read").tab_order(), [App::Teams, App::Outlook]);
60 }
61
62 #[test]
63 fn displays_resource_and_name() {
64 assert_eq!(Scope::graph("Chat.Read").to_string(), "https://graph.microsoft.com/Chat.Read");
65 }
66}