diff --git a/data/user.json b/data/user.json index 39c9488..a51c2a0 100755 --- a/data/user.json +++ b/data/user.json @@ -1 +1 @@ -{"userId":1234,"email":"user@example.com","pendingEmail":null,"birthdate":"2000-01-01T00:00:00.000Z","freshdeskContactId":null,"emailVerified":true,"emailVerifyCanceled":false,"collapseAdultContent":false,"activatedByInviteId":"a0d6f2e0-c499-4ce2-a10c-0ed882f04181","stripeCustomerId":null,"settings":{"collapseLongThreads":true,"gifsStartPaused":false,"pauseProfileGifs":false,"disableEmbeds":false,"externalLinksInNewTab":true,"enableNotificationCount":true,"suggestedFollowsDismissed":true,"enableMobileQuickShare":true,"beatsTimestamps":false,"autoExpandAllCws":false,"disableModalPostComposer":false,"homeView":"dashboard","defaultShow18PlusPostsInSearches":true,"defaultPostBoxTheme":"dark","previewFeatures_lexicalPostEditor":true,"chaosDay2023_showNumbers":true},"silencedCWs":["cw1","cw2","#cw 3"],"autoexpandCWs":["cw4","cw5","#cw6"],"collapsedTags":["tag1","tag2","#ta g3"],"notificationFilters":{"includeLikes":true,"includeShares":true,"includeFollows":true,"includeReplies":true,"includeComments":true},"lastActivityTime":"2024-10-01T12:0:0.123Z"} \ No newline at end of file +{"userId":1234,"email":"user@example.com","pendingEmail":null,"birthdate":"2000-01-01T00:00:00.000Z","freshdeskContactId":null,"emailVerified":true,"emailVerifyCanceled":false,"collapseAdultContent":false,"activatedByInviteId":"a0d6f2e0-c499-4ce2-a10c-0ed882f04181","stripeCustomerId":null,"settings":{"collapseLongThreads":true,"gifsStartPaused":false,"pauseProfileGifs":false,"disableEmbeds":false,"externalLinksInNewTab":true,"enableNotificationCount":true,"suggestedFollowsDismissed":true,"enableMobileQuickShare":true,"beatsTimestamps":false,"autoExpandAllCws":false,"disableModalPostComposer":false,"homeView":"dashboard","defaultShow18PlusPostsInSearches":true,"defaultPostBoxTheme":"dark","previewFeatures_lexicalPostEditor":true,"chaosDay2023_showNumbers":true},"silencedCWs":["cw1","\b\\\t\f\/cw2","#cw 3"],"autoexpandCWs":["cw4","cw5\r\n","#cw6"],"collapsedTags":["tag1","\"tag2","#ta g3"],"notificationFilters":{"includeLikes":true,"includeShares":true,"includeFollows":true,"includeReplies":true,"includeComments":true},"lastActivityTime":"2024-10-01T12:0:0.123Z"} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 838f8f5..8f0d80f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,6 @@ -use nom::{IResult, Parser, branch::alt, bytes::complete::tag, combinator::map_res}; +use nom::{ + IResult, Parser, branch::alt, bytes::complete::tag, combinator::map_res, sequence::delimited, +}; use std::collections::HashMap; #[derive(PartialEq, Debug)] @@ -17,18 +19,23 @@ fn parse_null(input: &str) -> IResult<&str, JsonValue> { Ok((remaining, JsonValue::Null)) } fn parse_bool(input: &str) -> IResult<&str, JsonValue> { - let parsed = - map_res(alt(( - tag("true"), - tag("false") - )), |st: &str| st.parse::()).parse(input)?; + let parsed = map_res(alt((tag("true"), tag("false"))), |st: &str| { + st.parse::() + }) + .parse(input)?; Ok((parsed.0, JsonValue::Boolean(parsed.1))) } +fn parse_string(input: &str) -> IResult<&str, JsonValue> { + delimited(tag("\""), parse_string_inner, tag("\"")).parse(input) +} +fn parse_string_inner(input: &str) -> IResult<&str, JsonValue> { + todo!() +} pub fn deserialize_json(json: &str) -> IResult<&str, JsonValue> { // map_res(); - alt((parse_null, parse_bool)).parse(json) + alt((parse_null, parse_bool, parse_string)).parse(json) } #[cfg(test)] @@ -51,7 +58,9 @@ mod tests { #[ignore] fn deserializes_real_json() { let result = deserialize_json(include_str!("../data/user.json")).unwrap(); - let JsonValue::Object(hm) = result.1 else {panic!()}; + let JsonValue::Object(hm) = result.1 else { + panic!() + }; assert_eq!(hm.get("userId"), Some(&JsonValue::Integer(1234))) } }