Skip to content

Commit

Permalink
enable the profile to be null on the rust side (still need to improve…
Browse files Browse the repository at this point in the history
… JS and py bindings). Related to #4
  • Loading branch information
vemonet committed Mar 1, 2024
1 parent e2334aa commit b9b3179
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 23 deletions.
6 changes: 4 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
};
if test_server {
println!("🧪 Publishing {np_file} to test server");
let _ = Nanopub::new(&np_rdf)?.publish(&profile, None).await;
let _ = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await;
} else {
let server = get_np_server(true);
println!("📬️ Publishing {np_file} to {server}");
let _ = Nanopub::new(&np_rdf)?.publish(&profile, Some(server)).await;
let _ = Nanopub::new(&np_rdf)?
.publish(Some(&profile), Some(server))
.await;
}
}
Some(("check", sub)) => {
Expand Down
4 changes: 2 additions & 2 deletions js/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl NanopubJs {
}
.to_string();
future_to_promise(async move {
match self.np.publish(&profile, Some(&server_url)).await {
match self.np.publish(Some(&profile), Some(&server_url)).await {
Ok(np) => Ok(JsValue::from(NanopubJs { np })),
Err(e) => Err(JsValue::from_str(&format!(
"Error publishing the Nanopub: {e}"
Expand All @@ -86,7 +86,7 @@ impl NanopubJs {
)))
}
};
match np.publish(&profile, Some(&server_url)).await {
match np.publish(Some(&profile), Some(&server_url)).await {
Ok(np) => Ok(JsValue::from(NanopubJs { np })),
Err(e) => Err(JsValue::from_str(&format!(
"Error publishing Nanopub Introduction: {e}"
Expand Down
4 changes: 2 additions & 2 deletions lib/docs/use_rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let checked_np = Nanopub::new(&signed_np.get_rdf().unwrap()).unwrap().check();
let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");

let published_np = rt.block_on(async {
Nanopub::new(np_rdf).unwrap().publish(&profile, None).await.unwrap()
Nanopub::new(np_rdf).unwrap().publish(Some(&profile), None).await.unwrap()
});
println!("{}", published_np)
```
Expand Down Expand Up @@ -85,7 +85,7 @@ async fn np_from_scratch() -> Result<(), NpError> {

let private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjY1gsFxmak6SOCouJPuEzHNForkqFhgfHE3aAIAx+Y5q6UDEDM9Q0EksheNffJB4iPqsAfiFpY0ARQY92K5r8P4+a78eu9reYrb2WxZb1qPJmvR7XZ6sN1oHD7dd/EyQoJmQsmOKdrqaLRbzR7tZrf52yvKkwNWXcIVhW8uxe7iUgxiojZpW9srKoK/qFRpaUZSKn7Z/zgtDH9FJkYbBsGPDMqp78Kzt+sJb+U2W+wCSSy34jIUxx6QRbzvn6uexc/emFw/1DU5y7zBudhgC7mVk8vX1gUNKyjZBzlOmRcretrANgffqs5fx/TMHN1xtkA/H1u1IKBfKoyk/xThMLAgMBAAECggEAECuG0GZA3HF8OaqFgMG+W+agOvH04h4Pqv4cHjYNxnxpFcNV9nEssTKWSOvCwYy7hrwZBGV3PQzbjFmmrxVFs20+8yCD7KbyKKQZPVC0zf84bj6NTNgvr6DpGtDxINxuGaMjCt7enqhoRyRRuZ0fj2gD3Wqae/Ds8cpDCefkyMg0TvauHSUj244vGq5nt93txUv1Sa+/8tWZ77Dm0s5a3wUYB2IeAMl5WrO2GMvgzwH+zT+4kvNWg5S0Ze4KE+dG3lSIYZjo99h14LcQS9eALC/VBcAJ6pRXaCTT/TULtcLNeOpoc9Fu25f0yTsDt6Ga5ApliYkb7rDhV+OFrw1sYQKBgQDCE9so+dPg7qbp0cV+lbb7rrV43m5s9Klq0riS7u8m71oTwhmvm6gSLfjzqb8GLrmflCK4lKPDSTdwyvd+2SSmOXySw94zr1Pvc7sHdmMRyA7mH3m+zSOOgyCTTKyhDRCNcRIkysoL+DecDhNo4Fumf71tsqDYogfxpAQhn0re8wKBgQDXhMmmT2oXiMnYHhi2k7CJe3HUqkZgmW4W44SWqKHp0V6sjcHm0N0RT5Hz1BFFUd5Y0ZB3JLcah19myD1kKYCj7xz6oVLb8O7LeAZNlb0FsrtD7NU+Hciywo8qESiA7UYDkU6+hsmxaI01DsttMIdG4lSBbEjA7t4IQC5lyr7xiQKBgQCN87YGJ40Y5ZXCSgOZDepz9hqX2KGOIfnUv2HvXsIfiUwqTXs6HbD18xg3KL4myIBOvywSM+4ABYp+foY+Cpcq2btLIeZhiWjsKIrw71+Q/vIe0YDb1PGf6DsoYhmWBpdHzR9HN+hGjvwlsYny2L9Qbfhgxxmsuf7zeFLpQLijjwKBgH7TD28k8IOk5VKec2CNjKd600OYaA3UfCpP/OhDl/RmVtYoHWDcrBrRvkvEEd2/DZ8qw165Zl7gJs3vK+FTYvYVcfIzGPWA1KU7nkntwewmf3i7V8lT8ZTwVRsmObWU60ySJ8qKuwoBQodki2VX12NpMN1wgWe3qUUlr6gLJU4xAoGAet6nD3QKwk6TTmcGVfSWOzvpaDEzGkXjCLaxLKh9GreM/OE+h5aN2gUoFeQapG5rUwI/7Qq0xiLbRXw+OmfAoV2XKv7iI8DjdIh0F06mlEAwQ/B0CpbqkuuxphIbchtdcz/5ra233r3BMNIqBl3VDDVoJlgHPg9msOTRy13lFqc=";
let profile = NpProfile::new(&private_key, "https://orcid.org/0000-0000-0000-0000", "", None)?;
let np = np.publish(&profile, None).await?;
let np = np.publish(Some(&profile), None).await?;
Ok(())
}
```
Expand Down
2 changes: 1 addition & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use error::NpError;
/// let checked_np = Nanopub::new(&signed_np.get_rdf().unwrap()).unwrap().check();
/// let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");
/// let published_np = rt.block_on(async {
/// Nanopub::new(&np_rdf).unwrap().publish(&profile, None).await.unwrap()
/// Nanopub::new(&np_rdf).unwrap().publish(Some(&profile), None).await.unwrap()
/// });
/// println!("{}", published_np)
/// ```
Expand Down
13 changes: 9 additions & 4 deletions lib/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,22 @@ impl Nanopub {
/// let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");
///
/// let np = rt.block_on(async {
/// Nanopub::new(&np_rdf).unwrap().publish(&profile, None).await
/// Nanopub::new(&np_rdf).unwrap().publish(Some(&profile), None).await
/// }).unwrap();
/// ```
pub async fn publish(
mut self,
profile: &NpProfile,
profile: Option<&NpProfile>,
server_url: Option<&str>,
) -> Result<Self, NpError> {
self = if self.info.signature.is_empty() {
self = if let Some(profile) = profile {
println!("Nanopub not signed, signing it before publishing");
self.sign(profile)?
} else if self.info.signature.is_empty() {
return Err(NpError(format!(
"No profile provided and nanopub not signed, could not sign the Nanopublication \n{}",
self
)));
} else {
// If the nanopub is already signed we verify it, then publish it
self.check()?
Expand Down Expand Up @@ -419,7 +424,7 @@ impl Nanopub {
/// let rt = runtime::Runtime::new().expect("Failed to create Tokio runtime");
///
/// let np = rt.block_on(async {
/// Nanopub::new_intro(&profile).unwrap().publish(&profile, None).await.unwrap()
/// Nanopub::new_intro(&profile).unwrap().publish(Some(&profile), None).await.unwrap()
/// });
/// ```
pub fn new_intro(profile: &NpProfile) -> Result<Self, NpError> {
Expand Down
2 changes: 2 additions & 0 deletions lib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn ns(ns: &str) -> Namespace<String> {
}
}

// TODO: improve to extract prefixes from the input RDF
/// Get the prefixes of a Nanopub
pub fn get_prefixes(np_uri: &str, np_ns: &str) -> [(Prefix<String>, Iri<String>); 14] {
[
Expand All @@ -108,6 +109,7 @@ pub fn get_prefixes(np_uri: &str, np_ns: &str) -> [(Prefix<String>, Iri<String>)
Prefix::new_unchecked("xsd".to_string()),
Iri::new_unchecked("http://www.w3.org/2001/XMLSchema#".to_string()),
),
// TODO: use https:// by default?
(
Prefix::new_unchecked("schema".to_string()),
Iri::new_unchecked("http://schema.org/".to_string()),
Expand Down
12 changes: 6 additions & 6 deletions lib/tests/nanopub_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn get_test_key() -> String {
async fn publish_nanopub_simple_rsa() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(&profile, None).await?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
println!("{}", np.get_rdf()?);
assert!(np.info.published);
// Values compiled with the nanopub java lib using the exact same RDF
Expand All @@ -37,7 +37,7 @@ async fn publish_proteinatlas() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("tests/testsuite/valid/plain/proteinatlas-16-1.trig")?;
// let np_rdf = fs::read_to_string("./tests/resources/nanopub_test_blank.trig")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(&profile, None).await?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
assert!(np.info.published);
Ok(())
}
Expand Down Expand Up @@ -84,7 +84,7 @@ async fn publish_fail() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/simple1-rsa.trig")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?
.publish(&profile, Some("failing"))
.publish(Some(&profile), Some("failing"))
.await;
assert!(np.is_err());
Ok(())
Expand Down Expand Up @@ -116,7 +116,7 @@ fn test_get_np_server() -> Result<(), Box<dyn Error>> {
async fn publish_jsonld() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/resources/nanopub.jsonld")?;
let profile = NpProfile::new(&get_test_key(), "", "", None)?;
let np = Nanopub::new(&np_rdf)?.publish(&profile, None).await?;
let np = Nanopub::new(&np_rdf)?.publish(Some(&profile), None).await?;
assert!(np.info.published);
Ok(())
}
Expand All @@ -130,7 +130,7 @@ async fn publish_np_intro() -> Result<(), Box<dyn Error>> {
None,
)?;
let np = Nanopub::new_intro(&profile)?
.publish(&profile, None)
.publish(Some(&profile), None)
.await?;
// println!("{}", np);
assert!(np.info.published);
Expand Down Expand Up @@ -212,7 +212,7 @@ async fn publish_from_scratch() -> Result<(), Box<dyn Error>> {
Iri::new_unchecked("http://dx.doi.org/10.3233/ISU-2010-0613"),
Some(&np.info.prov),
)?;
let np = np.publish(&profile, None).await?;
let np = np.publish(Some(&profile), None).await?;
println!("DEBUG: SCRATCH 2 {}", np.get_rdf()?);
// assert!(res.is_err());
Ok(())
Expand Down
18 changes: 13 additions & 5 deletions lib/tests/testsuite_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ async fn testsuite_publish_valid_plain() -> Result<(), Box<dyn Error>> {
if filename.ends_with("trig\"") {
println!("\n☑️ Testing file publish: {}", filename);
let np_rdf = fs::read_to_string(file.path())?;
let np = Nanopub::new(&np_rdf)?.publish(&get_profile(), None).await?;
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
}
}
Expand Down Expand Up @@ -126,7 +128,7 @@ fn testsuite_publish_invalid_plain() -> Result<(), Box<dyn Error>> {
let result = Nanopub::new(&np_rdf).and_then(|np| {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(np.publish(&get_profile(), None))
.block_on(np.publish(Some(&get_profile()), None))
});
assert!(
result.is_err(),
Expand All @@ -141,7 +143,9 @@ fn testsuite_publish_invalid_plain() -> Result<(), Box<dyn Error>> {
#[tokio::test]
async fn testsuite_publish_transform_signed_simple1() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/testsuite/transform/signed/rsa-key1/simple1.in.trig")?;
let np = Nanopub::new(&np_rdf)?.publish(&get_profile(), None).await?;
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
assert_eq!(
np.info.trusty_hash,
Expand All @@ -156,7 +160,9 @@ async fn testsuite_publish_transform_signed_simple1() -> Result<(), Box<dyn Erro
#[tokio::test]
async fn testsuite_publish_transform_trusty_aida() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/testsuite/transform/trusty/aida1.in.trig")?;
let np = Nanopub::new(&np_rdf)?.publish(&get_profile(), None).await?;
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
// println!("{}", np);
assert!(np.info.published);
// assert_eq!(np.trusty_hash, "RAPpJU5UOB4pavfWyk7FE3WQiam5yBpmIlviAQWtBSC4M");
Expand All @@ -166,7 +172,9 @@ async fn testsuite_publish_transform_trusty_aida() -> Result<(), Box<dyn Error>>
#[tokio::test]
async fn testsuite_publish_transform_trusty_simple1() -> Result<(), Box<dyn Error>> {
let np_rdf = fs::read_to_string("./tests/testsuite/transform/trusty/simple1.in.trig")?;
let np = Nanopub::new(&np_rdf)?.publish(&get_profile(), None).await?;
let np = Nanopub::new(&np_rdf)?
.publish(Some(&get_profile()), None)
.await?;
assert!(np.info.published);
// assert_eq!(np.trusty_hash, "RAtAU6U_xKTH016Eoiu11SswQkBu1elB_3_BoDJWH3arA");
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion python/src/nanopub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl NanopubPy {
let result = rt.block_on(async move {
Nanopub::new(&rdf)
.map_err(|e| PyErr::new::<PyException, _>(format!("Error Publishing: {e}")))?
.publish(&profile, server_url.as_deref())
.publish(Some(&profile), server_url.as_deref())
.await
.map_err(|e| PyErr::new::<PyException, _>(format!("Error Publishing: {e}")))
});
Expand Down

0 comments on commit b9b3179

Please sign in to comment.