Skip to content

Commit

Permalink
Prepare for release (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjovanc authored Oct 20, 2024
2 parents f1eaae9 + 24ea78c commit 030742e
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Versions currently supported. Will update this file once the project matures.

| Version | Supported |
| ------- | ------------------ |
| 0.1.0 | :white_check_mark: |
|---------| ------------------ |
| 0.4.0 | :white_check_mark: |

## Reporting a Vulnerability

Expand Down
8 changes: 4 additions & 4 deletions njord/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "njord"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Marcus Cvjeticanin <mjovanc@icloud.com>"]
description = "A lightweight ORM library in Rust."
Expand All @@ -13,23 +13,23 @@ homepage = "https://njord.rs"
keywords = ["orm", "database", "sql"]

[dependencies]
njord_derive = { version = "0.3.0", path = "../njord_derive" }
njord_derive = { version = "0.4.0", path = "../njord_derive" }
rusqlite = { version = "0.32.1", features = ["bundled"] }
log = "0.4.22"
rand = "0.8.4"
serde = { version = "1.0.210", features = ["derive"] }
mysql = "25.0.1"

[dev-dependencies]
njord_derive = { version = "0.3.0", path = "../njord_derive" }
njord_derive = { version = "0.4.0", path = "../njord_derive" }

# This cfg cannot be enabled, but it still forces Cargo to keep njord_derive's
# version in lockstep with njord's, even if someone depends on the two crates
# separately with njord's "derive" feature disabled. Every njord_derive release
# is compatible with exactly one serde release because the generated code
# involves nonpublic APIs which are not bound by semver.
[target.'cfg(any())'.dependencies]
njord_derive = { version = "0.3.0", path = "../njord_derive" }
njord_derive = { version = "0.4.0", path = "../njord_derive" }

[features]
default = ["sqlite"]
Expand Down
81 changes: 77 additions & 4 deletions njord/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,29 +35,52 @@ use std::{

use serde::{Deserialize, Deserializer};

/// A simple primary key wrapper.
///
/// The `PrimaryKey` struct wraps a value that acts as a primary key in a database.
/// This struct provides various utility methods and traits for working with primary keys.
///
/// # Type Parameters
///
/// * `T` - The type of the primary key value, which can be any type.
#[derive(Debug)]
pub struct PrimaryKey<T>(T);

#[derive(Debug, Clone)]
pub struct AutoIncrementPrimaryKey<T>(Option<T>);

impl<T> PrimaryKey<T> {
/// Creates a new `PrimaryKey` from the given value.
///
/// # Arguments
///
/// * `value` - The value of the primary key.
///
/// # Returns
///
/// A new `PrimaryKey` instance containing the given value.
pub fn new(value: T) -> Self {
PrimaryKey(value)
}

/// Retrieves a reference to the value inside the `PrimaryKey`.
///
/// # Returns
///
/// A reference to the primary key value.
pub fn get(&self) -> &T {
&self.0
}
}

impl<T: Default> Default for PrimaryKey<T> {
/// Creates a default `PrimaryKey` using the `Default` trait of `T`.
fn default() -> Self {
PrimaryKey(T::default())
}
}

impl<T: Debug + Display> Display for PrimaryKey<T> {
/// Implements the `Display` trait for `PrimaryKey`.
///
/// Formats the primary key using the inner value's `Display` implementation.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
Expand All @@ -66,6 +89,9 @@ impl<T: Debug + Display> Display for PrimaryKey<T> {
impl<T: Debug + FromStr> FromStr for PrimaryKey<T> {
type Err = T::Err;

/// Implements the `FromStr` trait for `PrimaryKey`.
///
/// Tries to parse a string into the inner type `T` and returns a `PrimaryKey`.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<T>() {
Ok(value) => Ok(PrimaryKey(value)),
Expand All @@ -79,6 +105,9 @@ where
T: FromStr + Debug,
<T as FromStr>::Err: Debug + Display,
{
/// Implements the `Deserialize` trait for `PrimaryKey`.
///
/// Deserializes a string into the inner type `T` and wraps it into a `PrimaryKey`.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -89,27 +118,62 @@ where
}
}

/// A wrapper for auto-incremented primary keys.
///
/// The `AutoIncrementPrimaryKey` struct wraps an optional value that can act as an auto-incremented
/// primary key in a database.
///
/// # Type Parameters
///
/// * `T` - The type of the primary key value, which can be any type.
#[derive(Debug, Clone)]
pub struct AutoIncrementPrimaryKey<T>(Option<T>);

impl<T> AutoIncrementPrimaryKey<T> {
/// Creates a new `AutoIncrementPrimaryKey` from the given optional value.
///
/// # Arguments
///
/// * `value` - An optional value of the primary key.
///
/// # Returns
///
/// A new `AutoIncrementPrimaryKey` instance containing the given value.
pub fn new(value: Option<T>) -> Self {
AutoIncrementPrimaryKey(value)
}

/// Retrieves a reference to the value inside the `AutoIncrementPrimaryKey`, if it exists.
///
/// # Returns
///
/// An optional reference to the primary key value.
pub fn get(&self) -> Option<&T> {
self.0.as_ref()
}

/// Sets the value of the `AutoIncrementPrimaryKey`.
///
/// # Arguments
///
/// * `value` - The value to set as the primary key.
pub fn set(&mut self, value: T) {
self.0 = Some(value);
}
}

impl<T: Debug> Default for AutoIncrementPrimaryKey<T> {
/// Creates a default `AutoIncrementPrimaryKey` with no value (i.e., `None`).
fn default() -> Self {
AutoIncrementPrimaryKey(None)
}
}

impl<T: Debug + Display> Display for AutoIncrementPrimaryKey<T> {
/// Implements the `Display` trait for `AutoIncrementPrimaryKey`.
///
/// Formats the primary key using the inner value's `Display` implementation if it exists,
/// otherwise displays "NULL".
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.0 {
Some(value) => write!(f, "{}", value),
Expand All @@ -121,6 +185,9 @@ impl<T: Debug + Display> Display for AutoIncrementPrimaryKey<T> {
impl<T: Debug + FromStr> FromStr for AutoIncrementPrimaryKey<T> {
type Err = T::Err;

/// Implements the `FromStr` trait for `AutoIncrementPrimaryKey`.
///
/// Tries to parse a string into the inner type `T` and wraps it into an `AutoIncrementPrimaryKey`.
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.parse::<T>() {
Ok(value) => Ok(AutoIncrementPrimaryKey(Some(value))),
Expand All @@ -130,6 +197,9 @@ impl<T: Debug + FromStr> FromStr for AutoIncrementPrimaryKey<T> {
}

impl<'de, T: Deserialize<'de>> Deserialize<'de> for AutoIncrementPrimaryKey<T> {
/// Implements the `Deserialize` trait for `AutoIncrementPrimaryKey`.
///
/// Deserializes an optional value into the inner type `T` and wraps it into an `AutoIncrementPrimaryKey`.
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -140,7 +210,10 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for AutoIncrementPrimaryKey<T> {
}

impl<T: PartialEq> PartialEq for AutoIncrementPrimaryKey<T> {
/// Implements the `PartialEq` trait for `AutoIncrementPrimaryKey`.
///
/// Checks equality between two `AutoIncrementPrimaryKey` instances by comparing their inner values.
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
}
2 changes: 1 addition & 1 deletion njord_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "njord_derive"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
authors = ["Marcus Cvjeticanin <mjovanc@icloud.com>"]
description = "You should not use this crate directly, it is internal to Njord."
Expand Down
4 changes: 2 additions & 2 deletions njord_examples/mysql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
rust-version = "1.81.0"

[dependencies]
njord = { version = "0.3.0", path = "../../njord", features = ["mysql"] }
njord_derive = { version = "0.3.0", path = "../../njord_derive" }
njord = { version = "0.4.0", path = "../../njord", features = ["mysql"] }
njord_derive = { version = "0.4.0", path = "../../njord_derive" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0.210", features = ["derive"] }
reqwest = { version = "0.12", features = ["json"] }
Expand Down
4 changes: 2 additions & 2 deletions njord_examples/sqlite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ edition = "2021"
rust-version = "1.81.0"

[dependencies]
njord = { version = "0.3.0", path = "../../njord", features = ["sqlite"] }
njord_derive = { version = "0.3.0", path = "../../njord_derive" }
njord = { version = "0.4.0", path = "../../njord", features = ["sqlite"] }
njord_derive = { version = "0.4.0", path = "../../njord_derive" }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0.210", features = ["derive"] }
reqwest = { version = "0.12", features = ["json"] }
Expand Down

0 comments on commit 030742e

Please sign in to comment.