Skip to content

Commit d03880d

Browse files
committed
Implement parsing of tickets, hashes or haf for content
1 parent 1575736 commit d03880d

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

iroh-pkarr-naming-system/Cargo.lock

Lines changed: 22 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

iroh-pkarr-naming-system/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77

88
[dependencies]
99
anyhow = "1.0.79"
10+
derive_more = "0.99.17"
1011
iroh-bytes = "0.12.0"
1112
iroh-net = "0.12.0"
1213
pkarr = { version = "1.0.1", features = ["async"] }

iroh-pkarr-naming-system/examples/cli.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,55 @@
1-
use iroh_bytes::HashAndFormat;
1+
use iroh_bytes::{Hash, HashAndFormat};
2+
use iroh_net::ticket::BlobTicket;
23
use iroh_pkarr_naming_system::{Record, IPNS};
3-
use std::{process, str::FromStr};
4+
use std::{fmt::Display, process, str::FromStr};
5+
6+
/// Various ways to specify content.
7+
#[derive(Debug, Clone, derive_more::From)]
8+
pub enum ContentArg {
9+
Hash(Hash),
10+
HashAndFormat(HashAndFormat),
11+
Ticket(BlobTicket),
12+
}
13+
14+
impl ContentArg {
15+
/// Get the hash and format of the content.
16+
pub fn hash_and_format(&self) -> HashAndFormat {
17+
match self {
18+
ContentArg::Hash(hash) => HashAndFormat::raw(*hash),
19+
ContentArg::HashAndFormat(haf) => *haf,
20+
ContentArg::Ticket(ticket) => HashAndFormat {
21+
hash: ticket.hash(),
22+
format: ticket.format(),
23+
},
24+
}
25+
}
26+
}
27+
28+
impl Display for ContentArg {
29+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30+
match self {
31+
ContentArg::Hash(hash) => Display::fmt(hash, f),
32+
ContentArg::HashAndFormat(haf) => Display::fmt(haf, f),
33+
ContentArg::Ticket(ticket) => Display::fmt(ticket, f),
34+
}
35+
}
36+
}
37+
38+
impl FromStr for ContentArg {
39+
type Err = anyhow::Error;
40+
41+
fn from_str(s: &str) -> Result<Self, Self::Err> {
42+
if let Ok(hash) = Hash::from_str(s) {
43+
Ok(hash.into())
44+
} else if let Ok(haf) = HashAndFormat::from_str(s) {
45+
Ok(haf.into())
46+
} else if let Ok(ticket) = BlobTicket::from_str(s) {
47+
Ok(ticket.into())
48+
} else {
49+
anyhow::bail!("invalid hash and format")
50+
}
51+
}
52+
}
453

554
#[tokio::main]
655
async fn main() -> anyhow::Result<()> {
@@ -24,7 +73,7 @@ async fn main() -> anyhow::Result<()> {
2473
let secret_key = iroh_net::key::SecretKey::from_str(&args[0])?;
2574
let public_key = secret_key.public();
2675
let zid = pkarr::PublicKey::try_from(*public_key.as_bytes())?.to_z32();
27-
let content = HashAndFormat::from_str(&args[1])?;
76+
let content = ContentArg::from_str(&args[1])?.hash_and_format();
2877
let record = Record::Content { content };
2978
let ipns = IPNS::default();
3079
println!("Publishing record to: {}", public_key);

0 commit comments

Comments
 (0)