Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 989d3eb

Browse files
committed
feat(utils): generate_resource_identifier_uri function
1 parent 8f6c3fd commit 989d3eb

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

src/catplus-common/src/graph/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use sophia_api::{
55
term::{bnode_id::BnodeId, SimpleTerm},
66
};
77
use uuid::Uuid;
8+
use crate::graph::namespaces::cat_resource;
89

910
pub fn generate_bnode_term() -> SimpleTerm<'static> {
1011
let identifier = Uuid::new_v4().to_string();
@@ -20,3 +21,9 @@ pub(crate) fn hash_identifier(identifier: &str) -> String {
2021
let result = hasher.finalize();
2122
general_purpose::URL_SAFE_NO_PAD.encode(result)
2223
}
24+
25+
pub fn generate_resource_identifier_uri(resource_id: String) -> SimpleTerm<'static> {
26+
let mut uri = cat_resource::ns.clone().as_str().to_owned();
27+
uri.push_str(&hash_identifier(&resource_id));
28+
IriRef::new_unchecked(uri).try_into_term().expect("Failed to convert to SimpleTerm")
29+
}

src/catplus-common/src/models/agilent.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::{
22
graph::{
33
insert_into::{InsertIntoGraph, Link},
4-
namespaces::{allodc, allores, allorole, cat, cat_resource, obo, purl, qb, qudt},
5-
utils::hash_identifier,
4+
namespaces::{allodc, allores, allorole, cat, obo, purl, qb, qudt},
5+
utils::generate_resource_identifier_uri,
66
},
7-
models::{core::PeakList, enums::Unit},
7+
models::{core::PeakList, enums::Unit}
88
};
99

1010
use serde::{Deserialize, Serialize};
@@ -195,9 +195,7 @@ pub struct DeviceDocument {
195195
impl InsertIntoGraph for DeviceDocument {
196196
fn get_uri(&self) -> SimpleTerm<'static> {
197197
// build URI based on self.device_identifier
198-
let mut uri = cat_resource::ns.clone().as_str().to_owned();
199-
uri.push_str(&hash_identifier(&self.device_identifier));
200-
IriRef::new_unchecked(uri).try_into_term().unwrap()
198+
generate_resource_identifier_uri(self.device_identifier.clone())
201199
}
202200

203201
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
@@ -279,9 +277,7 @@ impl InsertIntoGraph for AgilentProduct {
279277
fn get_uri(&self) -> SimpleTerm<'static> {
280278
//same as in synth.rs set_product_uri function
281279
//same as in bravo.rs get_uri function for BravoProduct
282-
let mut uri = cat_resource::ns.clone().as_str().to_owned();
283-
uri.push_str(&hash_identifier(&self.product_identifier));
284-
IriRef::new_unchecked(uri).try_into_term().unwrap()
280+
generate_resource_identifier_uri(self.product_identifier.clone())
285281
}
286282
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
287283
for (pred, value) in [

src/catplus-common/src/models/bravo.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
graph::{
33
insert_into::{InsertIntoGraph, Link},
4-
namespaces::{alloprop, alloqual, allores, cat, cat_resource, purl},
5-
utils::hash_identifier,
4+
namespaces::{alloprop, alloqual, allores, cat, purl},
5+
utils::generate_resource_identifier_uri,
66
},
77
models::{
88
core::{Chemical, Observation, Plate, Well},
@@ -204,9 +204,7 @@ impl InsertIntoGraph for BravoProduct {
204204
fn get_uri(&self) -> SimpleTerm<'static> {
205205
//same as in synth.rs set_product_uri function
206206
//same as in agilent.rs get_uri function for AgilentProduct
207-
let mut uri = cat_resource::ns.clone().as_str().to_owned();
208-
uri.push_str(&hash_identifier(&self.sample_id));
209-
IriRef::new_unchecked(uri).try_into_term().unwrap()
207+
generate_resource_identifier_uri(self.sample_id.clone())
210208
}
211209
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
212210
for (pred, value) in [

src/catplus-common/src/models/synth.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
graph::{
33
insert_into::{InsertIntoGraph, Link},
4-
namespaces::{alloproc, alloprop, alloqual, allores, cat, cat_resource, purl, qudt},
5-
utils::hash_identifier,
4+
namespaces::{alloproc, alloprop, alloqual, allores, cat, purl, qudt},
5+
utils::generate_resource_identifier_uri,
66
},
77
models::{
88
core::{Chemical, Observation, Plate},
@@ -34,18 +34,14 @@ pub struct SynthBatch {
3434
fn set_product_uri(product_id: String) -> SimpleTerm<'static> {
3535
//same as in agilent.rs get_uri function for AgilentProduct
3636
//same as in bravo.rs get_uri function for BravoProduct
37-
let mut uri = cat_resource::ns.clone().as_str().to_owned();
38-
uri.push_str(&hash_identifier(&product_id));
39-
IriRef::new_unchecked(uri).try_into_term().unwrap()
37+
generate_resource_identifier_uri(product_id.clone())
4038
}
4139

4240
impl InsertIntoGraph for SynthBatch {
4341
fn get_uri(&self) -> SimpleTerm<'static> {
4442
// build URI based on self.batch_id
45-
let mut uri = cat_resource::ns.clone().as_str().to_owned();
46-
uri.push_str(&hash_identifier(&self.batch_id));
47-
IriRef::new_unchecked(uri).try_into_term().unwrap()
48-
}
43+
generate_resource_identifier_uri(self.batch_id.clone())
44+
}
4945

5046
fn insert_into(&self, graph: &mut LightGraph, iri: SimpleTerm) -> anyhow::Result<()> {
5147
for (pred, value) in [

0 commit comments

Comments
 (0)