Started documenting rust.

This commit is contained in:
Johannes Randerath
2024-08-30 11:02:32 +02:00
parent 0f6e2e3d99
commit d422a4b31c
10 changed files with 127 additions and 12 deletions

View File

@@ -12,10 +12,13 @@
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* along with this program. If not, see <https://www.gnu.org/
*/
//! Database API using the table style system provided by this crate
//! Library providing a spreadsheet-like table system.
//! Adapter to a Mysql DB.
//! This file exposes public methods and datastructures to interface the system.
mod models;
mod schema;
mod jrtables;
@@ -36,7 +39,9 @@ use diesel::backend::Backend;
use diesel::sql_types::Integer;
use diesel::expression::Expression;
/// Every column has a type.
/// The default is text, currently number (isize) exists too.
/// Types are mainly used for sorting and can be helpful in client applications.
#[derive(PartialEq, Clone, Copy, diesel::FromSqlRow)]
#[repr(i32)]
pub enum FIELDTYPE {
@@ -175,6 +180,10 @@ pub fn get_table(conn: &mut MysqlConnection, tblid: i32, uid: i32) -> Option<Tbl
Some(Tbl { tblid: tbl.id, name: tbl.name, column_names: clmn_nms, column_types: clmn_tps,rows: rows })
}
/// Take a Tbl object, sort it according to parameters and return a sorted Tbl object.
/// sort_field is the index of the column to sort by.
/// 0 is the position of the row in the table.
/// then 1-indexed column index
pub fn sort_table(tbl: Tbl, sort_field: usize, sort_dir: u8) -> Tbl {
let mut rows = tbl.rows;
if sort_field == 0 {
@@ -196,6 +205,10 @@ pub fn sort_table(tbl: Tbl, sort_field: usize, sort_dir: u8) -> Tbl {
Tbl { tblid: tbl.tblid, name: tbl.name, column_names: tbl.column_names, column_types: tbl.column_types, rows: rows.clone() }
}
/// Take a Tbl object, find all rows matching the search queries and return a new Tbl object containing only those.
/// search_fields contains a Vec of (1-indexed) column positions to be included in the search
/// search_value is a string to search for in the search_fields.
/// Returned Tbl is not sorted and should be according to user preferences before being displayed.
pub fn search_table(tbl: Tbl, search_fields: Vec<i32>, search_value: String) -> Tbl {
let mut rows = tbl.rows;
let mut field_sets = HashSet::new();
@@ -209,7 +222,7 @@ pub fn search_table(tbl: Tbl, search_fields: Vec<i32>, search_value: String) ->
Tbl { tblid: tbl.tblid, name: tbl.name, column_names: tbl.column_names, column_types: tbl.column_types, rows: Vec::from_iter(field_sets) }
}
/// For a Vec a database ids of tables, return a Vec of their names in the same order.
pub fn get_tblnames(conn: &mut MysqlConnection, tblids: Vec<i32>) -> Option<Vec<String>> {
let mut tblnames = Vec::new();
for tblid in tblids {
@@ -308,6 +321,9 @@ pub fn register_or_login(conn: &mut MysqlConnection, uname: String, mail: String
Some(User { uid: uid, uname: uname, email: mail })
}
/// Take a user id (database representation, not username) and delete it from the database.
/// All its tables are cascadingly deleted.
/// Return Some(true) if successful, None otherwise
pub fn delete_user(conn: &mut MysqlConnection, uid: i32) -> Option<bool> {
if users::delete_user(conn, uid).is_err() {
return None;
@@ -315,6 +331,9 @@ pub fn delete_user(conn: &mut MysqlConnection, uid: i32) -> Option<bool> {
Some(true)
}
/// For a given id of a table and a vec of string values, create a new jrentry (table row) and all its jrcells (table cells) filled with the string values.
/// Only works if uid corresponds to the user id of the table's owner.
/// Return the id of the newly created row if successful, None otherwise.
pub fn add_row(conn: &mut MysqlConnection, tblid: i32, values: Vec<String>, uid: i32) -> Option<i32> {
let owner = jrtables::get_owner_id(conn, tblid);
let nrows = jrtables::get_nrows(conn, tblid);
@@ -343,6 +362,8 @@ pub fn add_row(conn: &mut MysqlConnection, tblid: i32, values: Vec<String>, uid:
Some(entryid)
}
/// Delete a jrentry (table row) identified by the id of its table and the current position wihthin it
/// Return Some(true) if successful, None otherwise
pub fn delete_row(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() || owner.unwrap() != uid || jrentries::delete_jrentry_relative(conn, tblid, row_pos).is_err() {
@@ -351,6 +372,9 @@ pub fn delete_row(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, uid: i32
Some(true)
}
/// Change a jrentry's (table row's) current position within its table, given the table's id, the current position and the new position it should be moved to.
/// Only works if uid equals to the owner's user id.
/// Returns Some(true) if successful, None otherwise.
pub fn move_row(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, newpos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() || owner.unwrap() != uid || jrentries::move_jrentry(conn, tblid, rowpos, newpos).is_err() {
@@ -359,6 +383,9 @@ pub fn move_row(conn: &mut MysqlConnection, tblid: i32, rowpos: i32, newpos: i32
Some(true)
}
/// Update a jrentry (table row) with a new set of string values, given those new values and the row identified by its table and its position within the table.
/// Only works if uid equals to the table's owner's user id.
/// Returns Some(true) if successful, None otherwise.
pub fn edit_row(conn: &mut MysqlConnection, tblid: i32, cells: Vec<String>, row_pos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() || owner.unwrap() != uid {
@@ -372,7 +399,9 @@ pub fn edit_row(conn: &mut MysqlConnection, tblid: i32, cells: Vec<String>, row_
Some(true)
}
/// Edit the content of a single jrcell (table cell), given its table id, the position of its row and column within the table and the new value.
/// Only works if uid equals to the table's owner's user id.
/// Returns Some(true) if successful, None otherwise.
pub fn edit_cell(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, column_pos: i32, new_value: &String, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
@@ -384,6 +413,9 @@ pub fn edit_cell(conn: &mut MysqlConnection, tblid: i32, row_pos: i32, column_po
Some(true)
}
/// Change a jrcolumn's (table column's) current position within its table, given the table's id, the current position and the new position it should be moved to.
/// Only works if uid equals to the owner's user id.
/// Returns Some(true) if successful, None otherwise.
pub fn move_column(conn: &mut MysqlConnection, tblid:i32, column_pos: i32, new_column_pos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
@@ -395,6 +427,10 @@ pub fn move_column(conn: &mut MysqlConnection, tblid:i32, column_pos: i32, new_c
Some(true)
}
/// Add a new jrcolumn (table column) to an existing table.
/// Required are the table's id, the column's name, the column's FIELDTYPE and the id of the logged in user.
/// Only works if uid equals to the table's owner's user id.
/// Returns the id of the newly created column if successful, None otherwise.
pub fn add_column(conn: &mut MysqlConnection, tblid: i32, name: String, clmtype: FIELDTYPE, uid: i32) -> Option<i32> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
@@ -410,6 +446,9 @@ pub fn add_column(conn: &mut MysqlConnection, tblid: i32, name: String, clmtype:
Some(clmid.unwrap())
}
/// Delete a column identified by its table's id and its position within the table.
/// Only works if uid equals to the table's owner's id.
/// Returns Some(true) if successful, None otherwise.
pub fn delete_column(conn: &mut MysqlConnection, tblid: i32, column_pos: i32, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||
@@ -421,6 +460,9 @@ pub fn delete_column(conn: &mut MysqlConnection, tblid: i32, column_pos: i32, ui
Some(true)
}
/// Update a jrcolumn (table column) with a given new name, and new type. The column is identified by its table's id and its position within it.
/// Only works if uid equals to the table's owner's id.
/// Returns Some(true) if successful, None otherwise.
pub fn edit_column(conn: &mut MysqlConnection, tblid: i32, column_pos: i32, new_name: &String, new_type: FIELDTYPE, uid: i32) -> Option<bool> {
let owner = jrtables::get_owner_id(conn, tblid);
if owner.is_err() ||