Skip to content

Commit 5ef5fca

Browse files
committed
feat: Add Semver support
1 parent ac2e162 commit 5ef5fca

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ all = ["uuid", "chrono", "new", "helpers", "rand", "hash"]
4848

4949
uuid = ["geekorm-core/uuid"]
5050
chrono = ["geekorm-derive/chrono", "geekorm-core/chrono"]
51+
semver = ["geekorm-derive/semver", "geekorm-core/semver"]
5152
# Random Generators
5253
rand = ["geekorm-derive/rand", "geekorm-core/rand"]
5354
# Logging
@@ -81,12 +82,15 @@ geekorm-core = { version = "^0.6.6", path = "geekorm-core" }
8182
geekorm-derive = { version = "^0.6.6", path = "geekorm-derive" }
8283

8384
[dev-dependencies]
85+
geekorm = { path = ".", features = ["semver"] }
86+
8487
anyhow = "^1"
8588
env_logger = "^0.11"
8689
log = "^0.4"
8790
serde = { version = "^1.0", features = ["derive"] }
8891
tokio = { version = "^1.38", features = ["full"] }
8992
chrono = { version = "^0.4", features = ["serde"] }
93+
semver = { version = "1.0", features = ["serde"] }
9094

9195
libsql = "^0.5"
9296
rusqlite = "^0.32"

examples/semver.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use anyhow::Result;
2+
use geekorm::{prelude::*, GEEKORM_BANNER, GEEKORM_VERSION};
3+
4+
#[derive(Table, Debug, Clone, serde::Serialize, serde::Deserialize)]
5+
struct Project {
6+
#[geekorm(primary_key, auto_increment)]
7+
pub id: PrimaryKey<i32>,
8+
9+
#[geekorm(unique)]
10+
pub name: String,
11+
12+
pub version: semver::Version,
13+
}
14+
15+
#[tokio::main]
16+
async fn main() -> Result<()> {
17+
println!("{} v{}\n", GEEKORM_BANNER, GEEKORM_VERSION);
18+
19+
let gorm = Project::new("geekorm", semver::Version::parse(GEEKORM_VERSION).unwrap());
20+
println!("Project :: {:?}", gorm);
21+
22+
let req = semver::VersionReq::parse(">=0.4.0, <1.0.0").unwrap();
23+
24+
assert!(req.matches(&gorm.version));
25+
26+
Ok(())
27+
}

geekorm-core/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ default = []
2525

2626
uuid = ["dep:uuid"]
2727
chrono = ["dep:chrono"]
28+
semver = ["dep:semver"]
2829
# Random string / number generation
2930
rand = ["dep:rand"]
3031
# Logging
@@ -52,6 +53,7 @@ log = { version = "^0.4", features = ["std"], optional = true }
5253

5354
uuid = { version = "^1.9", features = ["v4"], optional = true }
5455
chrono = { version = "^0.4", optional = true, features = ["serde"] }
56+
semver = { version = "^1.0", optional = true, features = ["serde"] }
5557
# Random number generation
5658
rand = { version = "^0.8", optional = true }
5759
rand_core = { version = "^0.6", features = ["std"], optional = true }

geekorm-core/src/builder/values/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use serde::{Serialize, Serializer};
44

55
#[cfg(feature = "chrono")]
66
pub(crate) mod valchrono;
7+
#[cfg(feature = "semver")]
8+
pub(crate) mod valsemver;
79
#[cfg(feature = "uuid")]
810
pub(crate) mod valuuid;
911

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
//! # Implementations of `From` trait for semver types.
2+
//!
3+
//! ```rust
4+
//! # #[cfg(feature = "semver")] {
5+
//! use geekorm::prelude::*;
6+
//! use semver::{Version, VersionReq};
7+
//!
8+
//! #[derive(Table, Clone, serde::Serialize, serde::Deserialize)]
9+
//! struct Project {
10+
//! #[geekorm(primary_key, auto_increment)]
11+
//! id: PrimaryKey<i32>,
12+
//! /// Name of the project
13+
//! #[geekorm(unique)]
14+
//! name: String,
15+
//| /// Version
16+
//! version: Version,
17+
//! /// Required Version
18+
//! requirement: VersionReq,
19+
//! }
20+
//!
21+
//! // Create a new Project with a semver Version
22+
//! let project = Project::new(
23+
//! "geekorm",
24+
//! Version::parse("1.0.0").unwrap(),
25+
//! VersionReq::parse(">=0.6, <1.2.3").unwrap(),
26+
//! );
27+
//!
28+
//! # assert!(project.requirement.matches(&project.version));
29+
//!
30+
//! # }
31+
//! ```
32+
33+
use super::Value;
34+
use semver::{Version, VersionReq};
35+
36+
impl From<Version> for Value {
37+
fn from(value: Version) -> Self {
38+
Value::Text(value.to_string())
39+
}
40+
}
41+
42+
impl From<&Version> for Value {
43+
fn from(value: &Version) -> Self {
44+
Value::Text(value.to_string())
45+
}
46+
}
47+
48+
impl From<Value> for Version {
49+
fn from(value: Value) -> Self {
50+
// TODO: This unwrap isn't great...
51+
Version::parse(&value.to_string()).unwrap()
52+
}
53+
}
54+
55+
impl From<VersionReq> for Value {
56+
fn from(value: VersionReq) -> Self {
57+
Value::Text(value.to_string())
58+
}
59+
}
60+
impl From<&VersionReq> for Value {
61+
fn from(value: &VersionReq) -> Self {
62+
Value::Text(value.to_string())
63+
}
64+
}
65+
66+
impl From<Value> for VersionReq {
67+
fn from(value: Value) -> Self {
68+
// TODO: This unwrap isn't great
69+
VersionReq::parse(&value.to_string()).unwrap()
70+
}
71+
}

geekorm-derive/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ uuid = ["geekorm-core/uuid", "dep:uuid"]
2929
chrono = ["geekorm-core/chrono"]
3030
new = []
3131
primary_key = []
32+
semver = ["geekorm-core/semver"]
3233
rand = ["geekorm-core/rand", "dep:rand"]
3334
# Hashing algorithms
3435
hash = ["geekorm-core/hash"]

0 commit comments

Comments
 (0)