Skip to content

Commit a225fe6

Browse files
authored
Fix firehose example (#258)
* add compat layer between two cid defs * point back to updated version of atrium-api
1 parent 22c9590 commit a225fe6

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

examples/firehose/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ edition = "2021"
77

88
[dependencies]
99
anyhow = "1.0.80"
10-
atrium-api = { version = "0.18.1", features = ["dag-cbor"] }
10+
atrium-api = { version = "0.24.8" }
1111
chrono = "0.4.34"
12+
cid_old = { package = "cid", version = "0.10.1" }
13+
cid = { package = "cid", version = "0.11.1" }
1214
futures = "0.3.30"
1315
ipld-core = { version = "0.4.0", default-features = false, features = ["std"] }
1416
rs-car = "0.4.1"
15-
serde_ipld_dagcbor = { version = "0.6.0", default-features = false, features = ["std"] }
17+
serde_ipld_dagcbor = { version = "0.6.0", default-features = false, features = [
18+
"std",
19+
] }
1620
tokio = { version = "1.36.0", features = ["full"] }
1721
tokio-tungstenite = { version = "0.21.0", features = ["native-tls"] }
1822
trait-variant = "0.1.1"

examples/firehose/src/cid_compat.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use cid::{multihash::Multihash, Cid};
2+
3+
pub struct CidOld(cid_old::Cid);
4+
5+
impl From<cid_old::Cid> for CidOld {
6+
fn from(value: cid_old::Cid) -> Self {
7+
Self(value)
8+
}
9+
}
10+
impl TryFrom<CidOld> for Cid {
11+
type Error = cid::Error;
12+
fn try_from(value: CidOld) -> Result<Self, Self::Error> {
13+
let version = match value.0.version() {
14+
cid_old::Version::V0 => cid::Version::V0,
15+
cid_old::Version::V1 => cid::Version::V1,
16+
};
17+
18+
let codec = value.0.codec();
19+
let hash = value.0.hash();
20+
let hash = Multihash::from_bytes(&hash.to_bytes())?;
21+
22+
Self::new(version, codec, hash)
23+
}
24+
}

examples/firehose/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
pub mod cid_compat;
12
pub mod stream;
23
pub mod subscription;

examples/firehose/src/main.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use atrium_api::app::bsky::feed::post::Record;
33
use atrium_api::com::atproto::sync::subscribe_repos::{Commit, NSID};
44
use atrium_api::types::{CidLink, Collection};
55
use chrono::Local;
6+
use firehose::cid_compat::CidOld;
67
use firehose::stream::frames::Frame;
78
use firehose::subscription::{CommitHandler, Subscription};
89
use futures::StreamExt;
@@ -54,7 +55,12 @@ impl CommitHandler for Firehose {
5455
continue;
5556
}
5657
let (items, _) = rs_car::car_read_all(&mut commit.blocks.as_slice(), true).await?;
57-
if let Some((_, item)) = items.iter().find(|(cid, _)| Some(CidLink(*cid)) == op.cid) {
58+
if let Some((_, item)) = items.iter().find(|(cid, _)| {
59+
//
60+
// convert cid from v0.10.1 to v0.11.1
61+
let cid = CidOld::from(*cid).try_into().expect("couldn't convert old to new cid");
62+
Some(CidLink(cid)) == op.cid
63+
}) {
5864
let record = serde_ipld_dagcbor::from_reader::<Record, _>(&mut item.as_slice())?;
5965
println!(
6066
"{} - {}",
@@ -78,8 +84,5 @@ impl CommitHandler for Firehose {
7884

7985
#[tokio::main]
8086
async fn main() -> Result<(), Box<dyn std::error::Error>> {
81-
RepoSubscription::new("bsky.network")
82-
.await?
83-
.run(Firehose)
84-
.await
87+
RepoSubscription::new("bsky.network").await?.run(Firehose).await
8588
}

0 commit comments

Comments
 (0)