Skip to content

Commit

Permalink
start to add functionality to db
Browse files Browse the repository at this point in the history
  • Loading branch information
GlenDC committed Apr 1, 2024
1 parent 2054384 commit 44e5c4a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
38 changes: 31 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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()
}
}

Expand Down
33 changes: 31 additions & 2 deletions venndb-usage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use venndb::VennDB;

#[derive(Debug, VennDB)]
struct Employee {
pub struct Employee {
id: u32,
name: String,
is_manager: bool,
Expand All @@ -12,7 +12,7 @@ struct Employee {
department: Department,
}

#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub enum Department {
Engineering,
Sales,
Expand All @@ -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);
}
}
1 change: 0 additions & 1 deletion www/CNAME

This file was deleted.

0 comments on commit 44e5c4a

Please sign in to comment.