Skip to content

Commit 3f238e5

Browse files
committed
add profile toml import/export
1 parent 5a19819 commit 3f238e5

File tree

20 files changed

+1139
-51
lines changed

20 files changed

+1139
-51
lines changed

Cargo.lock

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

ytflow-app-util/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ serde_bytes = "0.11"
2323
ciborium = "0.2"
2424
chrono = { version = "*", features = ["serde"] }
2525
uuid = { version = "1", features = ["serde"] }
26+
rusqlite = { version = "0.28", default-features = false }
27+
toml_edit = { version = "0.22", default-features = false, features = ["parse"] }
28+
hex = "0.4"
2629
ytflow = { path = "../ytflow" }

ytflow-app-util/src/cbor.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ pub type CborUtilResult<T> = Result<T, CborUtilError>;
2121

2222
pub use json::{cbor_to_json, json_to_cbor};
2323
pub use raw_bytes::{escape_cbor_buf, unescape_cbor_buf};
24+
25+
pub(crate) fn to_cbor(
26+
value: Result<ciborium::Value, ciborium::value::Error>,
27+
) -> serde_bytes::ByteBuf {
28+
let mut buf = Vec::with_capacity(128);
29+
ciborium::ser::into_writer(&value.expect("cannot encode cbor"), &mut buf)
30+
.expect("Cannot serialize proxy");
31+
serde_bytes::ByteBuf::from(buf)
32+
}

ytflow-app-util/src/ffi/data.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ use ytflow::data::{
1010
ResourceGitHubRelease, ResourceUrl,
1111
};
1212

13+
use crate::profile::{export_profile_toml, parse_profile_toml};
14+
1315
use super::error::ytflow_result;
14-
use super::interop::serialize_buffer;
16+
use super::interop::{serialize_buffer, serialize_string_buffer};
1517

1618
#[no_mangle]
1719
#[cfg(windows)]
@@ -141,6 +143,29 @@ pub unsafe extern "C" fn ytflow_profile_delete(
141143
}))
142144
}
143145

146+
#[no_mangle]
147+
pub unsafe extern "C" fn ytflow_profile_export_toml(
148+
profile_id: u32,
149+
conn: *const ytflow_connection,
150+
) -> ytflow_result {
151+
ytflow_result::catch_result_unwind(AssertUnwindSafe(move || {
152+
let conn = unsafe { &*conn };
153+
export_profile_toml(profile_id.into(), conn)
154+
.map(|p| p.map(serialize_string_buffer).unwrap_or((null_mut(), 0)))
155+
}))
156+
}
157+
158+
#[no_mangle]
159+
pub unsafe extern "C" fn ytflow_profile_parse_toml(
160+
toml: *const u8,
161+
toml_len: usize,
162+
) -> ytflow_result {
163+
ytflow_result::catch_result_unwind(AssertUnwindSafe(move || {
164+
let toml = unsafe { std::slice::from_raw_parts(toml, toml_len) };
165+
parse_profile_toml(toml).map(|p| serialize_buffer(&p))
166+
}))
167+
}
168+
144169
#[no_mangle]
145170
pub unsafe extern "C" fn ytflow_plugin_create(
146171
profile_id: u32,

ytflow-app-util/src/ffi/error.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::ptr::null_mut;
88
use ytflow::config::ConfigError;
99
use ytflow::data::DataError;
1010

11-
use crate::{cbor, proxy, share_link, subscription};
11+
use crate::{cbor, profile, proxy, share_link, subscription};
1212

1313
#[repr(C)]
1414
#[derive(Clone, Copy)]
@@ -238,6 +238,19 @@ impl ToFfiError for cbor::CborUtilError {
238238
}
239239
}
240240

241+
impl ToFfiError for profile::ParseTomlProfileError {
242+
fn from(self) -> ErrorDesc {
243+
use profile::ParseTomlProfileError::*;
244+
const BASE_CODE: u32 = 0x8001_1700;
245+
match self {
246+
TomlError(e) => ErrorDesc::e1(BASE_CODE + 1, e.to_string()),
247+
MissingInfo(i) => ErrorDesc::e1(0x8001_1300 + 3, i),
248+
InvalidValue(v) => ErrorDesc::e1(BASE_CODE + 3, v),
249+
InvalidEntryPoint => ErrorDesc::e0(BASE_CODE + 4),
250+
}
251+
}
252+
}
253+
241254
pub(super) struct InvalidCborError;
242255

243256
impl Display for InvalidCborError {

ytflow-app-util/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
pub mod cbor;
44
#[cfg(feature = "ffi")]
55
pub mod ffi;
6+
pub mod profile;
67
pub mod proxy;
78
pub mod share_link;
89
pub mod subscription;

ytflow-app-util/src/profile.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
mod export;
2+
mod import;
3+
4+
pub use export::export_profile_toml;
5+
pub use import::{
6+
parse_profile_toml, ParseTomlProfileError, ParseTomlProfileResult, ParsedTomlPlugin,
7+
ParsedTomlProfile,
8+
};

0 commit comments

Comments
 (0)