Skip to content

Commit

Permalink
Merge pull request #52 from toastxc/restructuring
Browse files Browse the repository at this point in the history
mass restructuring
  • Loading branch information
toastxc authored Dec 13, 2023
2 parents 996430c + 40bdbce commit 5070e53
Show file tree
Hide file tree
Showing 34 changed files with 1,495 additions and 1,451 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust-clippy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
- name: Run rust-clippy
run:
cargo clippy
--all-features
--features standard
--message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
continue-on-error: true

Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Build Standard
run: cargo build --features standard --verbose
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ futures-util = "0.3.28"
# http
tokio-tungstenite = { version = "0.20.0", features = ["native-tls"] }
reywen_http = { version = "0.1.18", features = ["serde"]}


native-tls = "0.2.11"

[features]
# engine option | mandatory
Expand Down
160 changes: 39 additions & 121 deletions src/client/methods/bot.rs
Original file line number Diff line number Diff line change
@@ -1,151 +1,69 @@
use crate::reywen_http::{driver::Method, results::DeltaError};
use serde::{Deserialize, Serialize};

use crate::{
client::Client,
json,
structures::users::{
bot::{Bot, FieldsBot, PublicBot},
User,
},
client::{Client, Result},
reywen_http::driver::Method,
structures::users::bot::{Bot, PublicBot},
};

use crate::structures::users::bot::{
BotResponse, DataBotInvite, DataCreateBot, DataEditBot, OwnedBotsResponse,
};

impl Client {
pub async fn bot_create(&self, data: &DataCreateBot) -> Result<Bot, DeltaError> {
pub async fn bot_create(&self, data: impl Into<&DataCreateBot>) -> Result<Bot> {
self.http
.request(Method::POST, "/bots/create", json!(data))
.request(Method::POST, "/bots/create", data.into())
.await
}
pub async fn bot_delete(&self, bot_id: &str) -> Result<(), DeltaError> {
pub async fn bot_delete(&self, bot_id: impl Into<String> + std::fmt::Display) -> Result<()> {
self.http
.request(Method::DELETE, &format!("/bots/{bot_id}"), None)
.request(Method::DELETE, format!("/bots/{bot_id}"), None)
.await
}

pub async fn bot_edit(&self, bot_id: &str, data: &DataEditBot) -> Result<Bot, DeltaError> {
pub async fn bot_edit(
&self,
bot_id: impl Into<String> + std::fmt::Display,
data: impl Into<&DataEditBot>,
) -> Result<Bot> {
self.http
.request(Method::PATCH, &format!("/bots/{bot_id}"), json!(data))
.request(Method::PATCH, format!("/bots/{bot_id}"), data.into())
.await
}
pub async fn bot_fetch(&self, bot_id: &str) -> Result<BotResponse, DeltaError> {
pub async fn bot_fetch(
&self,
bot_id: impl Into<String> + std::fmt::Display,
) -> Result<BotResponse> {
self.http
.request(Method::GET, &format!("/bots/{bot_id}"), None)
.request(Method::GET, format!("/bots/{bot_id}"), None)
.await
}
pub async fn bot_fetch_owned(&self) -> Result<OwnedBotsResponse, DeltaError> {
pub async fn bot_fetch_owned(&self) -> Result<OwnedBotsResponse> {
self.http.request(Method::GET, "/bots/@me", None).await
}
pub async fn bot_fetch_public(&self, bot_id: &str) -> Result<PublicBot, DeltaError> {
pub async fn bot_fetch_public(
&self,
bot_id: impl Into<String> + std::fmt::Display,
) -> Result<PublicBot> {
self.http
.request(Method::GET, &format!("/bots/{bot_id}/invite"), None)
.request(Method::GET, format!("/bots/{bot_id}/invite"), None)
.await
}

pub async fn bot_invite(
&self,
bot_id: &str,
server_or_group: &str,
bot_id: impl Into<String> + std::fmt::Display,
server_or_group: impl Into<String> + std::fmt::Display,
is_server: bool,
) -> Result<(), DeltaError> {
let data = if is_server {
DataBotInvite::Server {
server: server_or_group.to_string(),
}
} else {
DataBotInvite::Group {
group: server_or_group.to_string(),
}
};

) -> Result<()> {
self.http
.request(Method::POST, &format!("/bots/{bot_id}/invite"), json!(data))
.request(
Method::POST,
format!("/bots/{bot_id}/invite"),
&(match (is_server, server_or_group.into()) {
(true, server) => DataBotInvite::Server { server },
(false, group) => DataBotInvite::Group { group },
}),
)
.await
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum DataBotInvite {
Server { server: String },
Group { group: String },
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct OwnedBotsResponse {
pub bots: Vec<Bot>,
pub users: Vec<User>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct BotResponse {
pub bot: Bot,
pub user: User,
}

/// # Bot Details
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataCreateBot {
/// Bot username
pub name: String,
}

impl DataCreateBot {
pub fn new(name: &str) -> Self {
Self {
name: String::from(name),
}
}
}

/// # Bot Details
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DataEditBot {
/// Bot username
pub name: Option<String>,
/// Whether the bot can be added by anyone
pub public: Option<bool>,
/// Whether analytics should be gathered for this bot
///
/// Must be enabled in order to show up on [Revolt Discover](https://rvlt.gg).
pub analytics: Option<bool>,
/// Interactions URL
pub interactions_url: Option<String>,
/// Fields to remove from bot object
pub remove: Option<Vec<FieldsBot>>,
}

impl DataEditBot {
pub fn new() -> Self {
Self {
..Default::default()
}
}
pub fn set_name(&mut self, name: &str) -> Self {
self.name = Some(String::from(name));
self.to_owned()
}
pub fn set_public(&mut self, public: bool) -> Self {
self.public = Some(public);
self.to_owned()
}
pub fn set_analytics(&mut self, analytics: bool) -> Self {
self.analytics = Some(analytics);
self.to_owned()
}
pub fn set_interactions_url(&mut self, interactions_url: &str) -> Self {
self.interactions_url = Some(String::from(interactions_url));
self.to_owned()
}

pub fn add_remove(&mut self, field: FieldsBot) -> Self {
match self.remove.clone() {
Some(mut old) => old.push(field),
None => self.remove = Some(vec![field]),
}
self.to_owned()
}
pub fn set_remove(&mut self, fields: Vec<FieldsBot>) -> Self {
self.remove = Some(fields);
self.to_owned()
}
}
103 changes: 22 additions & 81 deletions src/client/methods/channel.rs
Original file line number Diff line number Diff line change
@@ -1,102 +1,43 @@
use crate::reywen_http::{driver::Method, results::DeltaError};
use serde::{Deserialize, Serialize};

use crate::{
client::Client,
json,
structures::channels::{invite::Invite, Channel, FieldsChannel},
client::Result,
reywen_http::driver::Method,
structures::channels::{invite::Invite, Channel, DataEditChannel},
};

impl Client {
pub async fn channel_delete(&self, channel: &str) -> Result<(), DeltaError> {
pub async fn channel_delete(
&self,
channel: impl Into<String> + std::fmt::Display,
) -> Result<()> {
self.http
.request(Method::DELETE, &format!("/channels/{channel}"), None)
.request(Method::DELETE, format!("/channels/{channel}"), None)
.await
}
pub async fn channel_edit(
&self,
channel: &str,
data: &DataEditChannel,
) -> Result<Channel, DeltaError> {
channel: impl Into<String> + std::fmt::Display,
data: impl Into<&DataEditChannel>,
) -> Result<Channel> {
self.http
.request(Method::PATCH, &format!("/channels/{channel}"), json!(data))
.request(Method::PATCH, format!("/channels/{channel}"), data.into())
.await
}
pub async fn channel_fetch(&self, channel: &str) -> Result<Channel, DeltaError> {
println!("URI {}{}", self.http.url, format!("/channels/{channel}"));
pub async fn channel_fetch(
&self,
channel: impl Into<String> + std::fmt::Display,
) -> Result<Channel> {
self.http
.request(Method::GET, &format!("/channels/{channel}"), None)
.request(Method::GET, format!("/channels/{channel}"), None)
.await
}

pub async fn channel_invite_create(&self, channel: &str) -> Result<Invite, DeltaError> {
pub async fn channel_invite_create(
&self,
channel: impl Into<String> + std::fmt::Display,
) -> Result<Invite> {
self.http
.request(Method::POST, &format!("/channels/{channel}/invites"), None)
.request(Method::POST, format!("/channels/{channel}/invites"), None)
.await
}
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct DataEditChannel {
/// Channel name
/// length min: 1, max: 32
pub name: Option<String>,
/// Channel description
/// length min: 0, max: 1024
pub description: Option<String>,
/// Group owner
pub owner: Option<String>,
/// Icon
///
/// Provide an Autumn attachment Id.
/// length min: 1, max: 128
pub icon: Option<String>,
/// Whether this channel is age-restricted
pub nsfw: Option<bool>,
/// Whether this channel is archived
pub archived: Option<bool>,
/// length min: 1
pub remove: Option<Vec<FieldsChannel>>,
}

impl DataEditChannel {
pub fn new() -> Self {
Default::default()
}

pub fn set_name(&mut self, name: &str) -> Self {
self.name = Some(String::from(name));
self.to_owned()
}
pub fn set_description(&mut self, description: &str) -> Self {
self.description = Some(String::from(description));
self.to_owned()
}
pub fn set_owner(&mut self, owner: &str) -> Self {
self.owner = Some(String::from(owner));
self.to_owned()
}
pub fn set_icon(&mut self, icon: &str) -> Self {
self.icon = Some(String::from(icon));
self.to_owned()
}
pub fn set_nsfw(&mut self, nsfw: bool) -> Self {
self.nsfw = Some(nsfw);
self.to_owned()
}
pub fn set_archived(&mut self, archived: bool) -> Self {
self.archived = Some(archived);
self.to_owned()
}

pub fn add_remove(&mut self, channel: FieldsChannel) -> Self {
match self.remove.clone() {
Some(mut channel_vec) => {
channel_vec.push(channel);
self.remove = Some(channel_vec);
}
None => self.remove = Some(vec![channel]),
}
self.to_owned()
}
}
Loading

0 comments on commit 5070e53

Please sign in to comment.