gw2api_rs/v2/
guild.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3
4use crate::{Authentication, ClientExecutor, RequestBuilder};
5
6#[derive(Clone, Debug, Serialize, Deserialize)]
7pub struct Guild {
8    pub id: String,
9    pub name: String,
10    pub tag: String,
11    pub emblem: GuildEmblem,
12    // Avaliable with leader token.
13    pub level: Option<u8>,
14    pub motd: Option<String>,
15    pub influence: Option<u64>,
16    pub aetherium: Option<u64>,
17    pub favor: Option<u64>,
18    pub member_count: Option<u16>,
19    pub member_capacity: Option<u16>,
20}
21
22impl Guild {
23    /// Returns the guild with the given `id`.
24    pub fn get<C>(client: &C, id: &str) -> C::Result
25    where
26        C: ClientExecutor<Self>,
27    {
28        let uri = format!("/v2/guild/{}", id);
29        client.send(RequestBuilder::new(uri))
30    }
31
32    /// Returns a list of guild ids matching the searched `name`. If no matches are found this
33    /// returns an empty [`Vec`].
34    ///
35    /// # Examples
36    ///
37    /// ```no_run
38    /// # use gw2api_rs::{Client, Result};
39    /// # use gw2api_rs::v2::guild::Guild;
40    /// #
41    /// # async fn run() -> Result<()> {
42    /// let client = Client::new();
43    /// let guilds = Guild::search(&client, "Covenant Of The Just").await?;
44    /// println!("{:?}", guilds);
45    /// # Ok(())
46    /// # }
47    /// ```
48    ///
49    /// Using the [`blocking`] client:
50    ///
51    /// ```no_run
52    /// # use gw2api_rs::Result;
53    /// # use gw2api_rs::blocking::Client;
54    /// # use gw2api_rs::v2::guild::Guild;
55    /// #
56    /// # fn run() -> Result<()> {
57    /// let client = Client::new();
58    /// let guilds = Guild::search(&client, "Covenant Of The Just")?;
59    /// println!("{:?}", guilds);
60    /// # Ok(())
61    /// # }
62    /// ```
63    ///
64    /// [`blocking`]: crate::blocking
65    pub fn search<C>(client: &C, name: &str) -> C::Result
66    where
67        C: ClientExecutor<Vec<String>>,
68    {
69        let uri = format!("/v2/guild/search?name={}", name);
70        client.send(RequestBuilder::new(uri))
71    }
72}
73
74#[derive(Clone, Debug, Serialize, Deserialize)]
75pub struct GuildEmblem {
76    pub background: GuildEmblemSection,
77    pub foreground: GuildEmblemSection,
78    pub flags: Vec<GuildEmblemFlag>,
79}
80
81#[derive(Clone, Debug, Serialize, Deserialize)]
82pub struct GuildEmblemSection {
83    pub id: u64,
84    pub colors: Vec<u64>,
85}
86
87#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
88pub enum GuildEmblemFlag {
89    FlipBackgroundHorizontal,
90    FlipBackgroundVertical,
91    FlipForegroundHorizontal,
92    FlipForegroundVertical,
93}
94
95/// A list of [`GuildMember`]s.
96#[derive(Clone, Debug, Serialize, Deserialize)]
97#[serde(transparent)]
98pub struct GuildMembers(pub Vec<GuildMember>);
99
100impl GuildMembers {
101    /// Returns a list of all members in the guild with the provided `guild_id`.
102    ///
103    /// Note that the current access token must be a guild leader of the provided `guild_id`.
104    ///
105    /// # Authentication
106    ///
107    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
108    /// If the account of the current access token is not a guild leader of the guild, an [`Error`]
109    /// is returned.
110    ///
111    /// # Examples
112    ///
113    /// ```no_run
114    /// # use gw2api_rs::{Client, Result};
115    /// # use gw2api_rs::v2::guild::GuildMembers;
116    /// #
117    /// # async fn run() -> Result<()> {
118    /// # let token = "";
119    /// # let guild = "";
120    /// let client: Client = Client::builder().access_token(token).into();
121    /// let members = GuildMembers::get(&client, guild).await?;
122    /// println!("{:?}", members);
123    /// # Ok(())
124    /// # }
125    /// ```
126    ///
127    /// Using the [`blocking`] client:
128    ///
129    /// ```no_run
130    /// # use gw2api_rs::Result;
131    /// # use gw2api_rs::blocking::Client;
132    /// # use gw2api_rs::v2::guild::GuildMembers;
133    /// #
134    /// # fn run() -> Result<()> {
135    /// # let token = "";
136    /// # let guild = "";
137    /// let client: Client = Client::builder().access_token(token).into();
138    /// let members = GuildMembers::get(&client, guild)?;
139    /// println!("{:?}", members);
140    /// # Ok(())
141    /// # }
142    /// ```
143    ///
144    /// [`Error`]: struct@crate::Error
145    /// [`blocking`]: crate::blocking
146    pub fn get<C>(client: &C, guild_id: &str) -> C::Result
147    where
148        C: ClientExecutor<Self>,
149    {
150        let uri = format!("/v2/guild/{}/members", guild_id);
151        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
152    }
153}
154
155/// A member in a guild.
156#[derive(Clone, Debug, Serialize, Deserialize)]
157pub struct GuildMember {
158    /// The account name of the member.
159    pub name: String,
160    /// The rank of the member.
161    pub rank: String,
162    /// The date the member joined the guild.
163    pub joined: DateTime<Utc>,
164}
165
166#[derive(Clone, Debug, Serialize, Deserialize)]
167#[serde(transparent)]
168pub struct GuildRanks(Vec<GuildRank>);
169
170impl GuildRanks {
171    /// Returns a list of ranks in the guild with the provided `guild_id`.
172    ///
173    /// Note that the current access token must be a guild leader of the provided `guild_id`.
174    ///
175    /// # Authentication
176    ///
177    /// This endpoint requires authentication and returns an [`Error`] if no access token is set.
178    /// If the account of the current access token is not a guild leader of the guild, an [`Error`]
179    /// is returned.
180    ///
181    /// # Examples
182    ///
183    /// ```no_run
184    /// # use gw2api_rs::{Client, Result};
185    /// # use gw2api_rs::v2::guild::GuildRanks;
186    /// #
187    /// # async fn run() -> Result<()> {
188    /// # let token = "";
189    /// # let guild = "";
190    /// let client: Client = Client::builder().access_token(token).into();
191    /// let ranks = GuildRanks::get(&client, guild).await?;
192    /// println!("{:?}", ranks);
193    /// # Ok(())
194    /// # }
195    /// ```
196    ///
197    /// Using the [`blocking`] client:
198    ///
199    /// ```no_run
200    /// # use gw2api_rs::Result;
201    /// # use gw2api_rs::blocking::Client;
202    /// # use gw2api_rs::v2::guild::GuildRanks;
203    /// #
204    /// # fn run() -> Result<()> {
205    /// # let token = "";
206    /// # let guild = "";
207    /// let client: Client = Client::builder().access_token(token).into();
208    /// let ranks = GuildRanks::get(&client, guild)?;
209    /// println!("{:?}", ranks);
210    /// # Ok(())
211    /// # }
212    /// ```
213    ///
214    /// [`Error`]: struct@crate::Error
215    /// [`blocking`]: crate::blocking
216    pub fn get<C>(client: &C, guild_id: &str) -> C::Result
217    where
218        C: ClientExecutor<Self>,
219    {
220        let uri = format!("/v2/guild/{}/ranks", guild_id);
221        client.send(RequestBuilder::new(uri).authenticated(Authentication::Required))
222    }
223}
224
225#[derive(Clone, Debug, Serialize, Deserialize)]
226pub struct GuildRank {
227    /// The unique name of the rank.
228    pub id: String,
229    /// The sorting order of the rank. A lower order is a higher rank.
230    pub order: u64,
231    /// A list of permissions granted to this rank.
232    pub permissions: Vec<String>,
233    /// A url pointing to the icon of the rank.
234    pub icon: String,
235}