-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathimporter.rs
More file actions
27 lines (23 loc) · 952 Bytes
/
importer.rs
File metadata and controls
27 lines (23 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use std::{fmt::Debug, fs::read_to_string, path::PathBuf};
use color_eyre::eyre::{Result, eyre};
use serde::Deserialize;
use crate::otp::otp_element::OTPElement;
/// Common flow for all the importers
pub fn import_from_path<T>(path: PathBuf) -> Result<Vec<OTPElement>>
where
T: for<'a> Deserialize<'a> + TryInto<Vec<OTPElement>>,
<T as TryInto<Vec<OTPElement>>>::Error: Debug,
{
let json = read_to_string(path)?;
let deserialized: T = serde_json::from_str(json.as_str()).map_err(|e| {
eyre!(
"Invalid JSON import format.
Please check the file you are trying to import. For further information please check these guidelines:
https://github.com/replydev/cotp?tab=readme-ov-file#migration-from-other-apps
Specific error: {:?}",
e
)
})?;
let mapped: Vec<OTPElement> = deserialized.try_into().map_err(|e| eyre!("{:?}", e))?;
Ok(mapped)
}