Skip to content

Commit

Permalink
fix(profile): use a workaround to parse taggedvalue type
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Nov 25, 2024
1 parent 90d37b0 commit 778a3e4
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions backend/tauri/src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ use chrono::Local;
use log::debug;
use nyanpasu_ipc::api::status::CoreState;
use profile::item_type::ProfileItemType;
use serde_yaml::Mapping;
use serde::Deserialize;
use serde_yaml::{value::TaggedValue, Mapping};
use std::{borrow::Cow, collections::VecDeque, path::PathBuf, result::Result as StdResult};
use storage::{StorageOperationError, WebStorage};
use sysproxy::Sysproxy;
Expand Down Expand Up @@ -118,8 +119,23 @@ pub async fn import_profile(url: String, option: Option<RemoteProfileOptionsBuil
pub async fn create_profile(item: Mapping, file_data: Option<String>) -> Result {
let kind = item
.get("type")
.and_then(|kind| serde_yaml::from_value::<ProfileItemType>(kind.clone()).ok())
.ok_or(anyhow!("the type field is null"))?;
.ok_or(anyhow!("the type field is not found"))?;
// FIXME: a workaround for serde_yaml, and it should be fixed by upstream
let kind = serde_yaml::from_value(match kind {
serde_yaml::Value::String(_) => kind.clone(),
serde_yaml::Value::Mapping(kind) => {
let tag = kind
.keys()
.next()
.ok_or(anyhow!("the type field is not found in mapping"))?;
serde_yaml::Value::Tagged(Box::new(TaggedValue {
tag: serde_yaml::value::Tag::new(tag.as_str().unwrap()),
value: kind.get(tag).unwrap().clone(),
}))
}
_ => return Err(anyhow!("the type field is not a string or mapping").into()),
})
.context("failed to parse the profile type")?;
let item = serde_yaml::Value::Mapping(item);
tracing::trace!("create profile: {kind:?} with {item:?}");
let profile: Profile = match kind {
Expand Down

0 comments on commit 778a3e4

Please sign in to comment.