From 8d149014e190b9e9fd7aab5fd36d8b27a76bac1f Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 2 Jun 2020 22:29:34 -0700 Subject: [PATCH] Added documentation to current code Explaining why things are being done --- server/src/channels.rs | 31 +++++++++++++++++++++++++++++++ server/src/main.rs | 1 - 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/server/src/channels.rs b/server/src/channels.rs index d14781a..bed4244 100644 --- a/server/src/channels.rs +++ b/server/src/channels.rs @@ -11,6 +11,7 @@ enum ChannelType { } impl ChannelType { + // These funcs are mainly here to help translation from mysql fn from_i32(x: i32) -> ChannelType { match x { 1 => ChannelType::Voice, @@ -28,6 +29,7 @@ impl ChannelType { } } +// Primary way of interpretting sql data on our channels table struct Channel { id: i32, name: String, @@ -36,6 +38,10 @@ struct Channel { } impl Channel { + /* + * When our sql library queries things we generally get back tuples rather reasily + * we can use this method to get something that makes more sense + */ fn from_tup(tup: (i32, String, String, i32)) -> Channel { Channel { id: tup.0, @@ -44,6 +50,16 @@ impl Channel { kind: ChannelType::from_i32(tup.3) } } + /* + * When responding with some channel data to the client we use json + * this itemizes a single struct as the following(without the pretty output) + * { + * "id": id, + * "name": "", + * "description": "", + * "kind": kind + * } + */ fn as_json_str(&self) -> String { let mut base = String::from("{"); base.push_str(&format!("\"id\":{},", self.id)); @@ -55,6 +71,10 @@ impl Channel { } +/* + * Forwarding SQL errors as we can handle those error in caller site for this leaf function + * On success we back a Vec which we can JSON'ify later and use as a response +*/ async fn get_channels_vec(conn: Conn) -> Result, Error> { let rows_db = conn.prep_exec(r"SELECT * FROM channels", ()).await?; let (_, rows) = rows_db.map_and_drop(|row| { @@ -66,6 +86,11 @@ async fn get_channels_vec(conn: Conn) -> Result, Error> { } pub async fn list_channels(pool: &Pool, response: &mut Response) { + /* + * Primary dispatcher for dealing with the CHANNELS_LIST route + * For the most part this function will have a lot more error handling as it + * should know what kind of issues its child functions will have + */ if let Ok(conn) = pool.get_conn().await { match get_channels_vec(conn).await { Ok(chans) => { @@ -73,6 +98,9 @@ pub async fn list_channels(pool: &Pool, response: &mut Response) { response.headers_mut().insert("Content-Type", HeaderValue::from_static("application/json")); + // At this point we build the content of our response body + // which is a json payload hence why there is weird string manipulation + // because we're trying to avoid dependancy issues and serializing things ourselves let mut new_body = String::from("{\"channels\":["); for chan in chans.iter() { let s = format!("{},", chan.as_json_str()); @@ -88,4 +116,7 @@ pub async fn list_channels(pool: &Pool, response: &mut Response) { } } } + else { + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + } } \ No newline at end of file diff --git a/server/src/main.rs b/server/src/main.rs index f8dec17..6560fc6 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -24,7 +24,6 @@ use auth::AuthReason; mod routes; mod invites; -mod sql_traits; mod channels; fn map_qs(query_string_raw: Option<&str>) -> HashMap<&str, &str> {