Skip to content

Commit 1af4f80

Browse files
committed
improve documentation
1 parent b63cafe commit 1af4f80

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

src/guild.rs

+42-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
//! A [Guild]'s preferences (aka: configuration)
2+
//!
3+
//! Botanist is shipped with many features, some of which
4+
//! work out of the box (ex: `ping`). Other on the other hand
5+
//! rely on guild-specific datum. This data as a whole is called
6+
//! the [Guild]'s preferences or configuration.
7+
//! It notably includes the privilege (see [`Privilege`]) system
8+
//! but also conviniences such as welcome messages, administration
9+
//! channels or advertisement policy.
10+
//!
11+
//! [Guild]: serenity::model::guild::Guild
12+
113
use crate::as_pg_array;
214
use async_recursion::async_recursion;
315
use serenity::model::id::{ChannelId, GuildId, RoleId};
416
use sqlx::{query, Executor, Postgres, Row};
517
use std::convert::TryFrom;
618
use thiserror::Error;
719

8-
/// Errors originating from the GuildConfig wrapper
20+
/// Errors originating from the `GuildConfig` wrapper
921
#[derive(Error, Debug)]
1022
pub enum GuildConfigError {
1123
#[error("`{field:?}` can't be over 2000 chracters")]
@@ -22,8 +34,27 @@ type Result<Return> = std::result::Result<Return, GuildConfigError>;
2234

2335
/// Wraps around a `guilds` row
2436
///
25-
/// Most methods returning a [`std::result::Result`] do so only because the query to the DB may fail
26-
/// If another reason may cause it to fail, it will be documented
37+
/// [`GuildConfig`] provides an API covering every common use-case. When it doesn't piecing methods
38+
/// together is simple and safe. As much as possible it tries to issue as little queries as possible
39+
/// so you can confidently use the methods.
40+
///
41+
/// # Connection (`conn`) parameter.
42+
///
43+
/// For the sake of flexibility as little constraints as possible were put on the methods.
44+
/// The major example of this is the `conn` parameter which generally accepts anything that
45+
/// implements: [`sqlx::Executor`]. This means both [`sqlx::PgPool`] and
46+
/// [`sqlx::PgConnection`] can be used. However some methods need to issue multiple queries.
47+
/// As such it requires a `conn` that implements [`Copy`]. In those cases simply pass a `&PgPool`
48+
///
49+
/// # Errors
50+
///
51+
/// For simplicty's sake only methods that can give other errors than [`sqlx::Error`] have a section
52+
/// detailing the error.
53+
///
54+
/// All methods provided by [`Self`] return a `Result` which's [`Err`] variant is
55+
/// [`GuildConfigError`]. One of the later's variant wraps around [`sqlx::Error`] which is returned by
56+
/// every [`sqlx`] method that interacts with the database. These are all about database errors, which for the
57+
/// user of the library, should only be caused by incorrect setup (see [`crate`]).
2758
#[derive(Debug)]
2859
pub struct GuildConfig(GuildId);
2960

@@ -56,7 +87,7 @@ impl GuildConfig {
5687
///
5788
/// # Errors
5889
///
59-
/// Errors if a row with the same `id` already exists in the DB
90+
/// Errors with [`GuildConfigError::AlreadyExists`] if a row with the same `id` already exists in the DB
6091
pub async fn new<'a, 'b, PgExec: Executor<'a, Database = Postgres> + Copy>(
6192
conn: PgExec,
6293
builder: GuildConfigBuilder<'b>,
@@ -450,6 +481,13 @@ impl ToString for Privilege {
450481
}
451482
}
452483

484+
/// Builder for new configuration entries
485+
///
486+
/// This should only be used when the bot joins a new [Guild].
487+
/// This builder is used to quickly whip up a new configuration with sensible defaults
488+
/// that can be easilly overriden. For how to use see [`GuildConfig::new()`] and the tests.
489+
///
490+
/// [Guild]: serenity::model::guild::Guild`
453491
#[derive(Debug)]
454492
pub struct GuildConfigBuilder<'a> {
455493
id: GuildId,

src/lib.rs

+41-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,50 @@
1+
//! A wrapper around Botanist database scheme
2+
//!
3+
//! # Objective
4+
//!
5+
//! This crate was built with the intent to provide a safe wrapper around Botanist's
6+
//! database scheme. This way invariants remain true at all times, no other tool can
7+
//! mess the data in an incorrect way.
8+
//!
9+
//! Centralizing the interactions with the database also allows finer control over
10+
//! iterations of its scheme. On that note changes in the scheme are done through migrations scripts
11+
//! (see the `migrations`) folder. Hence the setup of the databse is made very simple with
12+
//! [`sqlx`]'s cli tool. Moreover deviations from the scheme provided by the migration scripts
13+
//! will be detected by the tests (see `tests/framework` and [`mod@sqlx::migrate`]).
14+
//!
15+
//! Finally providing a rust library allows [db_adapter] to provide useful abstractions.
16+
//!
17+
//! # Setup
18+
//! Setup is intended to be as simple as possible so if you find some way to simplify a step please open
19+
//! an issue.
20+
//! First of all make sure you have a posgresql database up and running. Search online for walkthroughs
21+
//! if you don't know how. Then rename `.env-example` to `.env` and enter make sure you place your values
22+
//! in it.
23+
//! Now install [sqlx-cli] and run the migrations using `sqlx migrate run`. If you set up the DB and `.env`
24+
//! correctly you should be good to go!
25+
//! If you're only using the library you don't need to do anuything else but you could still
26+
//! run the tests just in case: `cargo t`.
27+
//!
28+
//! # Developement
29+
//!
30+
//! To contribute to [db_adapter] you should setup the test environement. In addition to the previous section's
31+
//! steps you should setup another DB for the tests and refer it in `.env`. From there you can dive in the code!
32+
//! Just make sure you don't break any invariants and remember to respect semver. You are also expected
33+
//! to document and tests any item added to the public API.
34+
//!
35+
//! [sqlx-cli]: https://github.com/launchbadge/sqlx/tree/master/sqlx-cli
36+
//! [db_adapter]: [`self`]
37+
138
use sqlx::postgres::PgPool;
239
use std::env;
340
use std::fmt::Write;
441

542
pub mod guild;
43+
pub mod slap;
644

45+
/// Creates a [connection pool] to the database
46+
///
47+
/// [connection pool]: sqlx::postgres::PgPool
748
pub async fn establish_connection() -> PgPool {
849
dotenv::dotenv().ok();
950
PgPool::connect(&env::var("DATABASE_URL").expect("DATABASE_URL was not set"))
@@ -25,18 +66,3 @@ pub(crate) fn as_pg_array(ids: &[i64]) -> String {
2566
write!(array, "}}'").unwrap();
2667
array
2768
}
28-
29-
/*
30-
pub(crate) fn to_comma_separated(ids: &[i64]) -> String {
31-
let mut array = String::new();
32-
if ids.is_empty() {
33-
return array;
34-
}
35-
for int in ids {
36-
write!(array, "{},", int).unwrap()
37-
}
38-
array.pop(); //removing the trailing comma
39-
40-
array
41-
}
42-
*/

0 commit comments

Comments
 (0)