Skip to content

Commit 0e4d033

Browse files
authored
Merge pull request #27 from akashsoni01/main
rc support added
2 parents 85f56ba + e4d5523 commit 0e4d033

13 files changed

Lines changed: 270 additions & 361 deletions

File tree

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-tagged"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2024"
55
authors = ["Codefonsi <info@codefonsi.com>"]
66
license = "MPL-2.0"
@@ -18,17 +18,16 @@ include = ["src/**/*", "Cargo.toml", "../../README.md", "LICENSE"]
1818
resolver = "3" # or "3"
1919
members = [
2020
"tagged-core",
21-
"tagged-macros",
2221
]
2322

2423
[patch.crates-io]
2524
tagged-core = { path = "tagged-core" }
2625

2726
[dependencies]
28-
tagged-core = { path = "tagged-core", version = "0.4.0", features = ["serde"] }
27+
tagged-core = { path = "tagged-core", version = "0.5.0", features = ["serde"] }
2928

3029
[dev-dependencies]
31-
serde = { version = "1.0.219", features = ["derive"] }
30+
serde = { version = "1.0.210", features = ["derive", "rc"] }
3231
serde_json = {version = "1.0.140"}
3332

3433

examples/Into_iterators.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use tagged_core::Tagged;
2+
3+
#[derive(Debug)]
4+
struct Org;
5+
6+
type EmployeeNames = Tagged<Vec<String>, Org>;
7+
8+
fn main() {
9+
let names: EmployeeNames = Tagged::new(vec!["Alice".into(), "Bob".into()]);
10+
names.into_iter().for_each(|name| println!("Name: {}", name));
11+
}
12+
13+
/*
14+
Name: Alice
15+
Name: Bob
16+
*/

examples/Iter_example.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,10 @@ type EmployeeNames = Tagged<Vec<String>, Org>;
77

88
fn main() {
99
let names: EmployeeNames = Tagged::new(vec!["Alice".into(), "Bob".into()]);
10-
11-
for name in &names {
12-
println!("Name: {name}");
13-
}
14-
15-
// Consuming iterator
16-
for name in names {
17-
println!("Owned: {name}");
18-
}
10+
names.iter().for_each(|name| println!("Name: {}", name));
1911
}
2012

2113
/*
2214
Name: Alice
2315
Name: Bob
24-
Owned: Alice
25-
Owned: Bob
2616
*/

examples/RC_example.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use rust_tagged::Tagged;
2+
use std::rc::Rc;
3+
use serde::{Serialize, Deserialize};
4+
5+
#[derive(Debug, Serialize, Deserialize)]
6+
struct Org;
7+
8+
type OrgRef = Tagged<Rc<String>, Org>;
9+
10+
#[derive(Debug, Serialize, Deserialize)]
11+
struct Project {
12+
name: String,
13+
org: OrgRef,
14+
}
15+
16+
fn main() {
17+
let shared = Rc::new("codefonsi.com".to_string());
18+
19+
let project = Project {
20+
name: "Open Source".into(),
21+
org: shared.clone().into(),
22+
};
23+
24+
let json = serde_json::to_string(&project).unwrap();
25+
println!("Serialized: {json}");
26+
}

examples/Serde_example.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
use serde::{Deserialize, Serialize};
2+
use tagged_core::Tagged;
3+
4+
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
5+
struct SomeCustomType {
6+
some_id: String
7+
}
8+
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
9+
struct SomeCustomType2(String);
10+
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
11+
struct User {
12+
id: Tagged<String, Self>,
13+
id2: SomeCustomType,
14+
id3: SomeCustomType2,
15+
}
216

3-
fn main() {
4-
use tagged_core::Tagged;
5-
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
6-
struct SomeCustomType {
7-
some_id: String
8-
}
9-
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
10-
struct SomeCustomType2(String);
11-
#[derive(Clone, Hash, Debug, PartialEq, Eq, Serialize, Deserialize)]
12-
struct User {
13-
id: Tagged<String, Self>,
14-
id2: SomeCustomType,
15-
id3: SomeCustomType2,
16-
}
1717

18+
fn main() {
1819
let user = User { id: "1".into() , id2: SomeCustomType { some_id: "2".into() }, id3: SomeCustomType2("3".into())};
1920
let j = serde_json::to_string(&user).unwrap();
21+
let converted_user = serde_json::from_str::<User>(&j).unwrap();
2022
println!("{}", j);
23+
println!("{:?}", converted_user);
2124
}
25+
/*
26+
Running `target/debug/examples/Serde_example`
27+
{"id":"1","id2":{"some_id":"2"},"id3":"3"}
28+
User { id: "1", id2: SomeCustomType { some_id: "2" }, id3: SomeCustomType2("3") }
29+
30+
Process finished with exit code 0
31+
*/
2232

2333
/*
2434
Problem with normal types

examples/for_loop_example.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use tagged_core::Tagged;
2+
3+
#[derive(Debug)]
4+
struct Org;
5+
6+
type EmployeeNames = Tagged<Vec<String>, Org>;
7+
8+
fn main() {
9+
let names: EmployeeNames = Tagged::new(vec!["Alice".into(), "Bob".into()]);
10+
11+
for name in &names {
12+
println!("Name: {name}");
13+
}
14+
15+
// Consuming iterator
16+
for name in names {
17+
println!("Owned: {name}");
18+
}
19+
}
20+
21+
/*
22+
Name: Alice
23+
Name: Bob
24+
Owned: Alice
25+
Owned: Bob
26+
*/

src/lib.rs

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1 @@
1-
/// rust-tagged provides a simple way to define strongly typed wrappers over primitive types like String, i32, Uuid, chrono::DateTime, etc. It helps eliminate bugs caused by misusing raw primitives for conceptually distinct fields such as UserId, Email, ProductId, and more.
2-
///
3-
/// Eliminate accidental mixups between similar types (e.g. OrgId vs UserId)
4-
/// Enforce domain modeling in code via the type system
5-
/// Ergonomic .into() support for primitive conversions
6-
///
7-
/// # Example - Simple
8-
///
9-
/// ```
10-
/// use tagged_core::{Tagged};
11-
///
12-
/// #[derive(Debug)]
13-
/// struct EmailTag;
14-
///
15-
/// type Email = Tagged<String, EmailTag>;
16-
///
17-
/// fn main() {
18-
/// let email: Email = "test@example.com".into();
19-
/// println!("Email inner value: {}", email.value());
20-
///
21-
/// // Convert back to String
22-
/// let raw: String = email.into();
23-
/// println!("Raw String: {raw}");
24-
/// }
25-
/// ```
26-
///
27-
/// # Example - Debug
28-
/// ```
29-
/// use tagged_core::Tagged;
30-
///
31-
///
32-
/// #[derive(Debug)]
33-
/// struct UserIdTag {
34-
/// a: Tagged<u32, Self>,
35-
/// b: Tagged<u32, Self>,
36-
/// }
37-
///
38-
///
39-
/// fn main() {
40-
/// let instance = UserIdTag{a: 1.into(), b: 2.into()};
41-
///
42-
/// println!("{}", instance.a);
43-
/// println!("{:?}", instance.b);
44-
/// }
45-
/// ```
46-
///
47-
/// # Example - Hash
48-
/// ```
49-
/// fn main() {
50-
/// use tagged_core::Tagged;
51-
/// use std::collections::HashSet;
52-
///
53-
/// #[derive(Clone, Hash, Debug, PartialEq, Eq)]
54-
/// struct User {
55-
/// id: Tagged<String, Self>
56-
/// }
57-
/// let mut s: HashSet<User> = HashSet::new();
58-
/// let user = User{id: "me@example.com".into()};
59-
/// s.insert(user.clone());
60-
///
61-
/// assert!(s.contains(&user));
62-
/// }
63-
/// ```
64-
///
65-
/// # Example - Iter
66-
/// ```
67-
/// use tagged_core::Tagged;
68-
///
69-
/// #[derive(Debug)]
70-
/// struct Org;
71-
///
72-
/// type EmployeeNames = Tagged<Vec<String>, Org>;
73-
///
74-
/// fn main() {
75-
/// let names: EmployeeNames = Tagged::new(vec!["Alice".into(), "Bob".into()]);
76-
///
77-
/// for name in &names {
78-
/// println!("Name: {name}");
79-
/// }
80-
///
81-
/// // Consuming iterator
82-
/// for name in names {
83-
/// println!("Owned: {name}");
84-
/// }
85-
/// }
86-
///
87-
/// /*
88-
/// Name: Alice
89-
/// Name: Bob
90-
/// Owned: Alice
91-
/// Owned: Bob
92-
/// */
93-
/// ```
94-
///
95-
/// # Example - Mutation
96-
/// ```
97-
/// use tagged_core::Tagged;
98-
///
99-
/// #[derive(Debug)]
100-
/// struct Org;
101-
///
102-
/// type OrgName = Tagged<String, Org>;
103-
///
104-
/// fn main() {
105-
/// let mut name = OrgName::new("Codefonsi".into());
106-
///
107-
/// name.set("New Org Name".into());
108-
///
109-
/// println!("Updated Org Name: {}", name.value());
110-
/// }
111-
/// ```
112-
1131
pub use tagged_core::*;

tagged-core/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tagged-core"
3-
version = "0.4.0"
3+
version = "0.5.0"
44
edition = "2024"
55
description = "A lightweight tagged type abstraction for type-safe IDs, etc."
66
license = "MPL-2.0"
@@ -14,9 +14,10 @@ readme = "../README.md"
1414
include = ["src/**/*", "Cargo.toml", "../../README.md", "LICENSE"]
1515

1616
[dependencies]
17-
serde = { version = "1.0.219", features = ["derive"], optional = true }
17+
serde = { version = "1.0.210", features = ["derive", "rc"], optional = true }
1818

1919
[dev-dependencies]
20+
serde_json = "1.0.140"
2021
uuid = { version = "1.6" , features = ["v4"]}
2122
chrono = "0.4.41"
2223

0 commit comments

Comments
 (0)