From 9b5bc94b5de8c7c1e226887f5e986ecef5967d06 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 21 Feb 2021 21:57:38 -0500 Subject: persist uri match type when editing --- src/db.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 1 deletion(-) (limited to 'src/db.rs') diff --git a/src/db.rs b/src/db.rs index d519f00..067f36f 100644 --- a/src/db.rs +++ b/src/db.rs @@ -18,6 +18,91 @@ pub struct Entry { pub history: Vec, } +#[derive(serde::Serialize, Debug, Clone, Eq, PartialEq)] +pub struct Uri { + pub uri: String, + pub match_type: Option, +} + +// backwards compatibility +impl<'de> serde::Deserialize<'de> for Uri { + fn deserialize(deserializer: D) -> std::result::Result + where + D: serde::Deserializer<'de>, + { + struct StringOrUri; + impl<'de> serde::de::Visitor<'de> for StringOrUri { + type Value = Uri; + + fn expecting( + &self, + formatter: &mut std::fmt::Formatter, + ) -> std::fmt::Result { + formatter.write_str("uri") + } + + fn visit_str( + self, + value: &str, + ) -> std::result::Result + where + E: serde::de::Error, + { + Ok(Uri { + uri: value.to_string(), + match_type: None, + }) + } + + fn visit_map( + self, + mut map: M, + ) -> std::result::Result + where + M: serde::de::MapAccess<'de>, + { + let mut uri = None; + let mut match_type = None; + while let Some(key) = map.next_key()? { + match key { + "uri" => { + if uri.is_some() { + return Err( + serde::de::Error::duplicate_field("uri"), + ); + } + uri = Some(map.next_value()?); + } + "match_type" => { + if match_type.is_some() { + return Err( + serde::de::Error::duplicate_field( + "match_type", + ), + ); + } + match_type = map.next_value()?; + } + _ => { + return Err(serde::de::Error::unknown_field( + key, + &["uri", "match_type"], + )) + } + } + } + + uri.map_or_else( + || Err(serde::de::Error::missing_field("uri")), + |uri| Ok(Self::Value { uri, match_type }), + ) + } + } + + deserializer.deserialize_any(StringOrUri) + } +} + #[derive( serde::Serialize, serde::Deserialize, Debug, Clone, Eq, PartialEq, )] @@ -26,7 +111,7 @@ pub enum EntryData { username: Option, password: Option, totp: Option, - uris: Vec, + uris: Vec, }, Card { cardholder_name: Option, -- cgit v1.2.3-54-g00ecf