Skip to content

Commit

Permalink
Added JSON support for multiple signatures for the same platform! #13
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBinitGhimire committed Jan 8, 2022
1 parent c4fa797 commit 9cc70f9
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/platforms.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use platform_dirs::AppDirs;

use serde::Deserialize;
use serde::{de, Deserialize, Deserializer};

use std::error::Error;
use std::fmt;
use std::fs::{self, File};
use std::io::Read;

Expand All @@ -14,7 +15,39 @@ struct Response {
#[derive(Deserialize)]
struct Definition {
platform: String,
content: String,
#[serde(deserialize_with = "deserializeStringOrSequence")]
content: Vec<String>,
}

fn deserializeStringOrSequence<'de, D>(deserializer: D) -> Result<Vec<String>, D::Error>
where
D: Deserializer<'de>,
{
struct StringOrVec;

impl<'de> de::Visitor<'de> for StringOrVec {
type Value = Vec<String>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("string or list of strings")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
Ok(vec![value.to_owned()])
}

fn visit_seq<S>(self, visitor: S) -> Result<Self::Value, S::Error>
where
S: de::SeqAccess<'de>,
{
Deserialize::deserialize(de::value::SeqAccessDeserializer::new(visitor))
}
}

deserializer.deserialize_any(StringOrVec)
}

pub fn _get_signatures_from_repo() -> Result<String, Box<dyn Error>> {
Expand All @@ -35,7 +68,8 @@ pub fn _get_signatures() -> String {
let cache_file_path = app_dirs.cache_dir.join("signatures.json");
let mut signatures = String::new();
let mut cacheFile = File::open(cache_file_path).expect("Unable to open cache file!");
cacheFile.read_to_string(&mut signatures)
cacheFile
.read_to_string(&mut signatures)
.expect("Unable to read the cache file!");
return signatures;
}
Expand All @@ -45,9 +79,11 @@ pub fn _platforms(response: String) -> String {
let data: Response = serde_json::from_str(&_definitions).unwrap();
let mut platformName: String = "None".to_string();
for platform in data.platforms {
if response.contains(&platform.content) {
platformName = platform.platform;
break;
for respText in platform.content {
if response.contains(&respText) {
platformName = platform.platform;
break;
}
}
}
return platformName;
Expand Down

0 comments on commit 9cc70f9

Please sign in to comment.