From 44e5c4a59c465fd8856c8e2e25439a451b9d67cb Mon Sep 17 00:00:00 2001 From: glendc Date: Mon, 1 Apr 2024 23:17:21 +0200 Subject: [PATCH] start to add functionality to db --- src/lib.rs | 38 +++++++++++++++++++++++++++++++------- venndb-usage/src/main.rs | 33 +++++++++++++++++++++++++++++++-- www/CNAME | 1 - 3 files changed, 62 insertions(+), 10 deletions(-) delete mode 100644 www/CNAME diff --git a/src/lib.rs b/src/lib.rs index 9734663..e1a7184 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,9 +22,14 @@ fn impl_from_args(input: &syn::DeriveInput) -> TokenStream { let errors = &Errors::default(); let type_attrs = &TypeAttrs::parse(errors, input); let mut output_tokens = match &input.data { - syn::Data::Struct(ds) => { - impl_from_args_struct(errors, &input.ident, type_attrs, &input.generics, ds) - } + syn::Data::Struct(ds) => impl_from_args_struct( + errors, + &input.vis, + &input.ident, + type_attrs, + &input.generics, + ds, + ), syn::Data::Enum(_) => { errors.err(input, "`#[derive(VennDB)]` cannot be applied to enums"); TokenStream::new() @@ -41,6 +46,7 @@ fn impl_from_args(input: &syn::DeriveInput) -> TokenStream { /// Implements `VennDB` for a `#[derive(VennDB)]` struct. fn impl_from_args_struct( errors: &Errors, + vis: &syn::Visibility, name: &syn::Ident, type_attrs: &TypeAttrs, _generic_args: &syn::Generics, @@ -69,13 +75,31 @@ fn impl_from_args_struct( None => format_ident!("{}DB", name), }; + let db_doc = format!( + "An in-memory database for storing instances of [`{}`], generated by `#[derive(VennDB)]`.", + name + ); quote! { - #[non_exhaustive] - struct #name_db; + #[doc=#db_doc] + #vis struct #name_db { + rows: Vec<#name>, + } impl #name_db { - fn new() -> Self { - Self + #vis fn new() -> Self { + Self { + rows: Vec::new(), + } + } + + /// Return the number of rows in the database. + #vis fn len(&self) -> usize { + self.rows.len() + } + + /// Return `true` if the database is empty. + #vis fn is_empty(&self) -> bool { + self.rows.is_empty() } } diff --git a/venndb-usage/src/main.rs b/venndb-usage/src/main.rs index 9b437ec..f01de2e 100644 --- a/venndb-usage/src/main.rs +++ b/venndb-usage/src/main.rs @@ -3,7 +3,7 @@ use venndb::VennDB; #[derive(Debug, VennDB)] -struct Employee { +pub struct Employee { id: u32, name: String, is_manager: bool, @@ -12,7 +12,7 @@ struct Employee { department: Department, } -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum Department { Engineering, Sales, @@ -33,3 +33,32 @@ fn main() { let _db = EmployeeDB::new(); } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_employee() { + let employee = Employee { + id: 1, + name: "Alice".to_string(), + is_manager: true, + is_admin: false, + is_active: true, + department: Department::Engineering, + }; + assert_eq!(employee.id, 1); + assert_eq!(employee.name, "Alice"); + assert!(employee.is_manager); + assert!(!employee.is_admin); + assert!(employee.is_active); + assert_eq!(employee.department, Department::Engineering); + } + + #[test] + fn test_employee_db_empty_len() { + let db = EmployeeDB::new(); + assert_eq!(db.len(), 0); + } +} diff --git a/www/CNAME b/www/CNAME deleted file mode 100644 index e6c9052..0000000 --- a/www/CNAME +++ /dev/null @@ -1 +0,0 @@ -venndb.rs \ No newline at end of file