feat: create local profile with selected file

This commit is contained in:
GyDi
2022-03-19 19:21:55 +08:00
parent 9c62311b56
commit 145f6e2d53
5 changed files with 92 additions and 12 deletions

View File

@@ -43,9 +43,10 @@ pub async fn import_profile(
#[tauri::command]
pub async fn create_profile(
item: PrfItem, // partial
file_data: Option<String>,
profiles_state: State<'_, ProfilesState>,
) -> Result<(), String> {
let item = wrap_err!(PrfItem::from(item).await)?;
let item = wrap_err!(PrfItem::from(item, file_data).await)?;
let mut profiles = profiles_state.0.lock().unwrap();
wrap_err!(profiles.append_item(item))

View File

@@ -119,7 +119,7 @@ impl Default for PrfItem {
impl PrfItem {
/// From partial item
/// must contain `itype`
pub async fn from(item: PrfItem) -> Result<PrfItem> {
pub async fn from(item: PrfItem, file_data: Option<String>) -> Result<PrfItem> {
if item.itype.is_none() {
bail!("type should not be null");
}
@@ -137,7 +137,7 @@ impl PrfItem {
"local" => {
let name = item.name.unwrap_or("Local File".into());
let desc = item.desc.unwrap_or("".into());
PrfItem::from_local(name, desc)
PrfItem::from_local(name, desc, file_data)
}
"merge" => {
let name = item.name.unwrap_or("Merge".into());
@@ -155,7 +155,7 @@ impl PrfItem {
/// ## Local type
/// create a new item from name/desc
pub fn from_local(name: String, desc: String) -> Result<PrfItem> {
pub fn from_local(name: String, desc: String, file_data: Option<String>) -> Result<PrfItem> {
let uid = help::get_uid("l");
let file = format!("{uid}.yaml");
@@ -170,7 +170,7 @@ impl PrfItem {
extra: None,
option: None,
updated: Some(help::get_now()),
file_data: Some(tmpl::ITEM_LOCAL.into()),
file_data: Some(file_data.unwrap_or(tmpl::ITEM_LOCAL.into())),
})
}