Skip to content

Commit

Permalink
feat: Add Semver support
Browse files Browse the repository at this point in the history
  • Loading branch information
GeekMasher committed Sep 20, 2024
1 parent ac2e162 commit 812cc60
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ all = ["uuid", "chrono", "new", "helpers", "rand", "hash"]

uuid = ["geekorm-core/uuid"]
chrono = ["geekorm-derive/chrono", "geekorm-core/chrono"]
semver = ["geekorm-derive/semver", "geekorm-core/semver"]
# Random Generators
rand = ["geekorm-derive/rand", "geekorm-core/rand"]
# Logging
Expand Down Expand Up @@ -81,12 +82,15 @@ geekorm-core = { version = "^0.6.6", path = "geekorm-core" }
geekorm-derive = { version = "^0.6.6", path = "geekorm-derive" }

[dev-dependencies]
geekorm = { path = ".", features = ["semver"] }

anyhow = "^1"
env_logger = "^0.11"
log = "^0.4"
serde = { version = "^1.0", features = ["derive"] }
tokio = { version = "^1.38", features = ["full"] }
chrono = { version = "^0.4", features = ["serde"] }
semver = { version = "1.0", features = ["serde"] }

libsql = "^0.5"
rusqlite = "^0.32"
Expand Down
27 changes: 27 additions & 0 deletions examples/semver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Result;
use geekorm::{prelude::*, GEEKORM_BANNER, GEEKORM_VERSION};

#[derive(Table, Debug, Clone, serde::Serialize, serde::Deserialize)]
struct Project {
#[geekorm(primary_key, auto_increment)]
pub id: PrimaryKey<i32>,

#[geekorm(unique)]
pub name: String,

pub version: semver::Version,
}

#[tokio::main]
async fn main() -> Result<()> {
println!("{} v{}\n", GEEKORM_BANNER, GEEKORM_VERSION);

let gorm = Project::new("geekorm", semver::Version::parse(GEEKORM_VERSION).unwrap());
println!("Project :: {:?}", gorm);

let req = semver::VersionReq::parse(">=0.4.0, <1.0.0").unwrap();

assert!(req.matches(&gorm.version));

Ok(())
}
2 changes: 2 additions & 0 deletions geekorm-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default = []

uuid = ["dep:uuid"]
chrono = ["dep:chrono"]
semver = ["dep:semver"]
# Random string / number generation
rand = ["dep:rand"]
# Logging
Expand Down Expand Up @@ -52,6 +53,7 @@ log = { version = "^0.4", features = ["std"], optional = true }

uuid = { version = "^1.9", features = ["v4"], optional = true }
chrono = { version = "^0.4", optional = true, features = ["serde"] }
semver = { version = "^1.0", optional = true, features = ["serde"] }
# Random number generation
rand = { version = "^0.8", optional = true }
rand_core = { version = "^0.6", features = ["std"], optional = true }
Expand Down
2 changes: 2 additions & 0 deletions geekorm-core/src/builder/values/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use serde::{Serialize, Serializer};

#[cfg(feature = "chrono")]
pub(crate) mod valchrono;
#[cfg(feature = "semver")]
pub(crate) mod valsemver;
#[cfg(feature = "uuid")]
pub(crate) mod valuuid;

Expand Down
71 changes: 71 additions & 0 deletions geekorm-core/src/builder/values/valsemver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! # Implementations of `From` trait for semver types.
//!
//! ```rust
//! # #[cfg(feature = "semver")] {
//! use geekorm::prelude::*;
//! use semver::{Version, VersionReq};
//!
//! #[derive(Table, Clone, serde::Serialize, serde::Deserialize)]
//! struct Project {
//! #[geekorm(primary_key, auto_increment)]
//! id: PrimaryKey<i32>,
//! /// Name of the project
//! #[geekorm(unique)]
//! name: String,
//| /// Version
//! version: Version,
//! /// Required Version
//! requirement: VersionReq,
//! }
//!
//! // Create a new Project with a semver Version
//! let project = Project::new(
//! "geekorm",
//! Version::parse("1.0.0").unwrap(),
//! VersionReq::parse(">=0.6, <1.2.3").unwrap(),
//! );
//!
//! # assert!(project.requirement.matches(&project.version));
//!
//! # }
//! ```
use super::Value;
use semver::{Version, VersionReq};

impl From<Version> for Value {
fn from(value: Version) -> Self {
Value::Text(value.to_string())
}
}

impl From<&Version> for Value {
fn from(value: &Version) -> Self {
Value::Text(value.to_string())
}
}

impl From<Value> for Version {
fn from(value: Value) -> Self {
// TODO: This unwrap isn't great...
Version::parse(&value.to_string()).unwrap()
}
}

impl From<VersionReq> for Value {
fn from(value: VersionReq) -> Self {
Value::Text(value.to_string())
}
}
impl From<&VersionReq> for Value {
fn from(value: &VersionReq) -> Self {
Value::Text(value.to_string())
}
}

impl From<Value> for VersionReq {
fn from(value: Value) -> Self {
// TODO: This unwrap isn't great
VersionReq::parse(&value.to_string()).unwrap()
}
}
1 change: 1 addition & 0 deletions geekorm-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ uuid = ["geekorm-core/uuid", "dep:uuid"]
chrono = ["geekorm-core/chrono"]
new = []
primary_key = []
semver = ["geekorm-core/semver"]
rand = ["geekorm-core/rand", "dep:rand"]
# Hashing algorithms
hash = ["geekorm-core/hash"]
Expand Down

0 comments on commit 812cc60

Please sign in to comment.