From fe5ab3bc659d9325eeee88d77c652881cda49de3 Mon Sep 17 00:00:00 2001 From: Devon Sawatsky Date: Tue, 4 Aug 2026 01:17:27 +0000 Subject: [PATCH] json is the new markdown --- Cargo.lock | 18 +++++++++++++++++ Cargo.toml | 1 + data/user.json | 1 + src/lib.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100755 data/user.json diff --git a/Cargo.lock b/Cargo.lock index 4d77bc5..adcb332 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,3 +5,21 @@ version = 4 [[package]] name = "markdowner" version = "0.1.0" +dependencies = [ + "nom", +] + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "nom" +version = "8.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df9761775871bdef83bee530e60050f7e54b1105350d6884eb0fb4f46c2f9405" +dependencies = [ + "memchr", +] diff --git a/Cargo.toml b/Cargo.toml index 5a4adbc..e765675 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2024" [dependencies] +nom = "8.0.0" diff --git a/data/user.json b/data/user.json new file mode 100755 index 0000000..39c9488 --- /dev/null +++ b/data/user.json @@ -0,0 +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 diff --git a/src/lib.rs b/src/lib.rs index b93cf3f..838f8f5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,57 @@ -pub fn add(left: u64, right: u64) -> u64 { - left + right +use nom::{IResult, Parser, branch::alt, bytes::complete::tag, combinator::map_res}; +use std::collections::HashMap; + +#[derive(PartialEq, Debug)] +pub enum JsonValue { + Object(HashMap), + Array(Vec), + String(String), + Float(f64), + Integer(i64), + Boolean(bool), + Null, +} + +fn parse_null(input: &str) -> IResult<&str, JsonValue> { + let remaining = tag("null")(input)?.1; + 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)?; + + Ok((parsed.0, JsonValue::Boolean(parsed.1))) +} + +pub fn deserialize_json(json: &str) -> IResult<&str, JsonValue> { + // map_res(); + alt((parse_null, parse_bool)).parse(json) } #[cfg(test)] mod tests { use super::*; + #[test] + fn deserializes_null() { + let null = deserialize_json("null").unwrap(); + assert_eq!(null.1, JsonValue::Null) + } + #[test] + fn deserializes_booleans() { + let t = deserialize_json("true").unwrap(); + let f = deserialize_json("false").unwrap(); + assert_eq!(t.1, JsonValue::Boolean(true)); + assert_eq!(f.1, JsonValue::Boolean(false)); + } #[test] - fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + #[ignore] + fn deserializes_real_json() { + let result = deserialize_json(include_str!("../data/user.json")).unwrap(); + let JsonValue::Object(hm) = result.1 else {panic!()}; + assert_eq!(hm.get("userId"), Some(&JsonValue::Integer(1234))) } } -- 2.51.2