Skip to content

Commit dab4cdd

Browse files
authored
ffi: Add MediaSource::{from_json, to_json}
1 parent d610091 commit dab4cdd

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

bindings/matrix-sdk-ffi/src/api.udl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,15 @@ interface PaginationOptions {
154154

155155
interface RoomMessageEventContent {};
156156

157+
[Error]
158+
interface ClientError {
159+
Generic(string msg);
160+
};
161+
157162
interface MediaSource {
163+
[Name=from_json, Throws=ClientError]
164+
constructor(string json);
165+
string to_json();
158166
string url();
159167
};
160168

bindings/matrix-sdk-ffi/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use matrix_sdk::{self, encryption::CryptoStoreError, HttpError, IdParseError, StoreError};
22

3-
#[derive(Debug, thiserror::Error, uniffi::Error)]
3+
#[derive(Debug, thiserror::Error)]
44
pub enum ClientError {
55
#[error("client error: {msg}")]
66
Generic { msg: String },

bindings/matrix-sdk-ffi/src/timeline.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use matrix_sdk::{
1111
use ruma::UInt;
1212
use tracing::warn;
1313

14-
use crate::{error::TimelineError, helpers::unwrap_or_clone_arc};
14+
use crate::{
15+
error::{ClientError, TimelineError},
16+
helpers::unwrap_or_clone_arc,
17+
};
1518

1619
#[uniffi::export]
1720
pub fn media_source_from_url(url: String) -> Arc<MediaSource> {
@@ -934,6 +937,15 @@ pub enum VirtualTimelineItem {
934937

935938
#[extension_trait]
936939
pub impl MediaSourceExt for MediaSource {
940+
fn from_json(json: String) -> Result<MediaSource, ClientError> {
941+
let res = serde_json::from_str(&json)?;
942+
Ok(res)
943+
}
944+
945+
fn to_json(&self) -> String {
946+
serde_json::to_string(self).expect("Media source should always be serializable ")
947+
}
948+
937949
fn url(&self) -> String {
938950
match self {
939951
MediaSource::Plain(url) => url.to_string(),

0 commit comments

Comments
 (0)