Skip to content

Commit

Permalink
add profile toml import/export
Browse files Browse the repository at this point in the history
  • Loading branch information
bdbai committed Apr 27, 2024
1 parent 5a19819 commit 3f238e5
Show file tree
Hide file tree
Showing 20 changed files with 1,139 additions and 51 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions ytflow-app-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ serde_bytes = "0.11"
ciborium = "0.2"
chrono = { version = "*", features = ["serde"] }
uuid = { version = "1", features = ["serde"] }
rusqlite = { version = "0.28", default-features = false }
toml_edit = { version = "0.22", default-features = false, features = ["parse"] }
hex = "0.4"
ytflow = { path = "../ytflow" }
9 changes: 9 additions & 0 deletions ytflow-app-util/src/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ pub type CborUtilResult<T> = Result<T, CborUtilError>;

pub use json::{cbor_to_json, json_to_cbor};
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf};

pub(crate) fn to_cbor(
value: Result<ciborium::Value, ciborium::value::Error>,
) -> serde_bytes::ByteBuf {
let mut buf = Vec::with_capacity(128);
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf)
.expect("Cannot serialize proxy");
serde_bytes::ByteBuf::from(buf)
}
27 changes: 26 additions & 1 deletion ytflow-app-util/src/ffi/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use ytflow::data::{
ResourceGitHubRelease, ResourceUrl,
};

use crate::profile::{export_profile_toml, parse_profile_toml};

use super::error::ytflow_result;
use super::interop::serialize_buffer;
use super::interop::{serialize_buffer, serialize_string_buffer};

#[no_mangle]
#[cfg(windows)]
Expand Down Expand Up @@ -141,6 +143,29 @@ pub unsafe extern "C" fn ytflow_profile_delete(
}))
}

#[no_mangle]
pub unsafe extern "C" fn ytflow_profile_export_toml(
profile_id: u32,
conn: *const ytflow_connection,
) -> ytflow_result {
ytflow_result::catch_result_unwind(AssertUnwindSafe(move || {
let conn = unsafe { &*conn };
export_profile_toml(profile_id.into(), conn)
.map(|p| p.map(serialize_string_buffer).unwrap_or((null_mut(), 0)))
}))
}

#[no_mangle]
pub unsafe extern "C" fn ytflow_profile_parse_toml(
toml: *const u8,
toml_len: usize,
) -> ytflow_result {
ytflow_result::catch_result_unwind(AssertUnwindSafe(move || {
let toml = unsafe { std::slice::from_raw_parts(toml, toml_len) };
parse_profile_toml(toml).map(|p| serialize_buffer(&p))
}))
}

#[no_mangle]
pub unsafe extern "C" fn ytflow_plugin_create(
profile_id: u32,
Expand Down
15 changes: 14 additions & 1 deletion ytflow-app-util/src/ffi/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::ptr::null_mut;
use ytflow::config::ConfigError;
use ytflow::data::DataError;

use crate::{cbor, proxy, share_link, subscription};
use crate::{cbor, profile, proxy, share_link, subscription};

#[repr(C)]
#[derive(Clone, Copy)]
Expand Down Expand Up @@ -238,6 +238,19 @@ impl ToFfiError for cbor::CborUtilError {
}
}

impl ToFfiError for profile::ParseTomlProfileError {
fn from(self) -> ErrorDesc {
use profile::ParseTomlProfileError::*;
const BASE_CODE: u32 = 0x8001_1700;
match self {
TomlError(e) => ErrorDesc::e1(BASE_CODE + 1, e.to_string()),
MissingInfo(i) => ErrorDesc::e1(0x8001_1300 + 3, i),
InvalidValue(v) => ErrorDesc::e1(BASE_CODE + 3, v),
InvalidEntryPoint => ErrorDesc::e0(BASE_CODE + 4),
}
}
}

pub(super) struct InvalidCborError;

impl Display for InvalidCborError {
Expand Down
1 change: 1 addition & 0 deletions ytflow-app-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod cbor;
#[cfg(feature = "ffi")]
pub mod ffi;
pub mod profile;
pub mod proxy;
pub mod share_link;
pub mod subscription;
8 changes: 8 additions & 0 deletions ytflow-app-util/src/profile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
mod export;
mod import;

pub use export::export_profile_toml;
pub use import::{
parse_profile_toml, ParseTomlProfileError, ParseTomlProfileResult, ParsedTomlPlugin,
ParsedTomlProfile,
};
Loading

0 comments on commit 3f238e5

Please sign in to comment.