Skip to content

Commit 3948414

Browse files
committed
Replace serde with new decoder crate
1 parent 49501b8 commit 3948414

File tree

11 files changed

+579
-388
lines changed

11 files changed

+579
-388
lines changed

Cargo.lock

+35
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ repository = "https://github.com/hecrj/icebreaker"
1010
[dependencies]
1111
icebreaker_core.workspace = true
1212
itertools.workspace = true
13+
log.workspace = true
1314
open.workspace = true
1415
rand.workspace = true
1516
tokio.workspace = true
@@ -31,6 +32,7 @@ iced.git = "https://github.com/iced-rs/iced.git"
3132
iced.rev = "cc8b326dfc84aaea3a570c9ad129eb8aaedfcb8c"
3233

3334
chrono = "0.4"
35+
decoder = "0.0.1"
3436
dirs = "6.0"
3537
futures = "0.3"
3638
itertools = "0.13"

core/Cargo.toml

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ version = "0.1.0"
44
edition = "2021"
55

66
[dependencies]
7-
tokio.workspace = true
8-
tokio.features = ["fs", "io-util", "process", "time"]
7+
chrono.workspace = true
8+
chrono.features = ["serde"]
99

10-
tokio-stream.workspace = true
11-
tokio-stream.features = ["io-util"]
10+
decoder.workspace = true
11+
decoder.features = ["json"]
1212

1313
reqwest.workspace = true
1414
reqwest.features = ["json", "socks", "rustls-tls", "gzip"]
1515

1616
serde.workspace = true
1717
serde.features = ["derive"]
1818

19-
chrono.workspace = true
20-
chrono.features = ["serde"]
19+
tokio.workspace = true
20+
tokio.features = ["fs", "io-util", "process", "time"]
21+
22+
tokio-stream.workspace = true
23+
tokio-stream.features = ["io-util"]
2124

2225
url.workspace = true
2326
url.features = ["serde"]

core/src/chat.rs

+17-51
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
mod schema;
22

33
use crate::assistant::{self, Assistant, Reply, Token};
4-
use crate::chat::schema::Schema;
54
use crate::model;
65
use crate::plan::{self, Plan};
76
use crate::Error;
@@ -43,27 +42,11 @@ impl Chat {
4342
}
4443

4544
pub async fn fetch(id: Id) -> Result<Self, Error> {
46-
let bytes = fs::read(Self::path(&id).await?).await?;
45+
let json = fs::read_to_string(Self::path(&id).await?).await?;
4746

4847
let _ = LastOpened::update(id).await;
4948

50-
task::spawn_blocking(move || {
51-
let schema: Schema = serde_json::from_slice(&bytes)?;
52-
53-
let history = schema
54-
.history
55-
.into_iter()
56-
.map(schema::Message::to_data)
57-
.collect();
58-
59-
Ok(Self {
60-
id,
61-
file: schema.file,
62-
title: schema.title,
63-
history,
64-
})
65-
})
66-
.await?
49+
task::spawn_blocking(move || schema::decode(&json)).await?
6750
}
6851

6952
pub async fn fetch_last_opened() -> Result<Self, Error> {
@@ -78,7 +61,14 @@ impl Chat {
7861
history: Vec<Item>,
7962
) -> Result<Self, Error> {
8063
let id = Id(Uuid::new_v4());
81-
let chat = Self::save(id, file, title, history).await?;
64+
let chat = Self {
65+
id,
66+
file,
67+
title,
68+
history,
69+
}
70+
.save()
71+
.await?;
8272

8373
LastOpened::update(chat.id).await?;
8474

@@ -92,48 +82,24 @@ impl Chat {
9282
Ok(chat)
9383
}
9484

95-
pub async fn save(
96-
id: Id,
97-
file: model::File,
98-
title: Option<String>,
99-
history: Vec<Item>,
100-
) -> Result<Self, Error> {
101-
if let Ok(current) = Self::fetch(id).await {
102-
if current.title != title {
85+
pub async fn save(self) -> Result<Self, Error> {
86+
if let Ok(current) = Self::fetch(self.id).await {
87+
if current.title != self.title {
10388
let mut list = List::fetch().await?;
10489

105-
if let Some(entry) = list.entries.iter_mut().find(|entry| entry.id == id) {
106-
entry.title = title.clone();
90+
if let Some(entry) = list.entries.iter_mut().find(|entry| entry.id == self.id) {
91+
entry.title = self.title.clone();
10792
}
10893

10994
list.save().await?;
11095
}
11196
}
11297

113-
let (bytes, chat, history) = task::spawn_blocking(move || {
114-
let chat = Schema {
115-
id,
116-
file,
117-
title,
118-
history: history
119-
.iter()
120-
.cloned()
121-
.map(schema::Message::from_data)
122-
.collect(),
123-
};
124-
125-
(serde_json::to_vec_pretty(&chat), chat, history)
126-
})
127-
.await?;
98+
let (bytes, chat) = task::spawn_blocking(move || (schema::encode(&self), self)).await?;
12899

129100
fs::write(Self::path(&chat.id).await?, bytes?).await?;
130101

131-
Ok(Self {
132-
id: chat.id,
133-
file: chat.file,
134-
title: chat.title,
135-
history,
136-
})
102+
Ok(chat)
137103
}
138104

139105
pub async fn delete(id: Id) -> Result<(), Error> {

0 commit comments

Comments
 (0)