From 56c847df6c23f7f8fb84f171d2a9ec19db4d1449 Mon Sep 17 00:00:00 2001 From: Able Date: Wed, 3 May 2023 03:39:34 -0500 Subject: [PATCH] add in URI and xml --- Cargo.lock | 68 +++++++++++++++++++++++++++++++++------ Cargo.toml | 1 + libraries/uri/Cargo.toml | 9 ++++++ libraries/uri/src/lib.rs | 48 +++++++++++++++++++++++++++ libraries/uri/src/main.rs | 12 +++++++ libraries/xml/src/lib.rs | 8 ++++- 6 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 libraries/uri/Cargo.toml create mode 100644 libraries/uri/src/lib.rs create mode 100644 libraries/uri/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 777cb02..e179cfe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -224,7 +224,30 @@ version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf8b031682c67a8e3d5446840f9573eb7fe26efe7ec8d195c9ac4c0647c502f1" dependencies = [ - "logos-derive", + "logos-derive 0.12.1", +] + +[[package]] +name = "logos" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c000ca4d908ff18ac99b93a062cb8958d331c3220719c52e77cb19cc6ac5d2c1" +dependencies = [ + "logos-derive 0.13.0", +] + +[[package]] +name = "logos-codegen" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc487311295e0002e452025d6b580b77bb17286de87b57138f3b5db711cded68" +dependencies = [ + "beef", + "fnv", + "proc-macro2", + "quote", + "regex-syntax", + "syn 2.0.15", ] [[package]] @@ -238,7 +261,16 @@ dependencies = [ "proc-macro2", "quote", "regex-syntax", - "syn", + "syn 1.0.105", +] + +[[package]] +name = "logos-derive" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbfc0d229f1f42d790440136d941afd806bc9e949e2bcb8faa813b0f00d1267e" +dependencies = [ + "logos-codegen", ] [[package]] @@ -282,9 +314,9 @@ version = "0.1.0" [[package]] name = "proc-macro2" -version = "1.0.47" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" +checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435" dependencies = [ "unicode-ident", ] @@ -311,9 +343,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.21" +version = "1.0.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc" dependencies = [ "proc-macro2", ] @@ -360,7 +392,7 @@ checksum = "a55492425aa53521babf6137309e7d34c20bbfbbfcfe2c7f3a047fd1f6b92c0c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.105", ] [[package]] @@ -406,6 +438,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "syn" +version = "2.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a34fcf3e8b60f57e6a14301a2e916d323af98b0ea63c599441eec8558660c822" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "system_config" version = "0.1.0" @@ -448,7 +491,7 @@ checksum = "982d17546b47146b28f7c22e3d08465f6b8903d0ea13c1660d9d84a6e7adcdbb" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 1.0.105", ] [[package]] @@ -536,6 +579,13 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3" +[[package]] +name = "uri" +version = "0.1.0" +dependencies = [ + "logos 0.13.0", +] + [[package]] name = "version_check" version = "0.9.4" @@ -574,7 +624,7 @@ dependencies = [ name = "wat2wasm" version = "0.1.0" dependencies = [ - "logos", + "logos 0.12.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 7547d79..8cc6f7e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ members = [ "libraries/tar", "libraries/time", "libraries/trash_manifest", + "libraries/uri", "libraries/versioning", "programs/ari_client", diff --git a/libraries/uri/Cargo.toml b/libraries/uri/Cargo.toml new file mode 100644 index 0000000..bb78f11 --- /dev/null +++ b/libraries/uri/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "uri" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +logos = "0.13.0" diff --git a/libraries/uri/src/lib.rs b/libraries/uri/src/lib.rs new file mode 100644 index 0000000..b20f814 --- /dev/null +++ b/libraries/uri/src/lib.rs @@ -0,0 +1,48 @@ +use logos::{Lexer, Logos}; + +#[derive(Logos, Debug, PartialEq)] +#[logos(skip r"[ \t\n\f]+")] // Ignore this regex pattern between tokens +pub enum Token { + // Tokens can be literal strings, of any length. + #[regex("[a-zA-Z0-9]+:/?/?", scheme_parse)] + Scheme(String), + + #[regex(r#"[a-zA-Z0-9.]+"#, |lex| lex.slice().parse().ok(), priority = 10)] + Authority(String), + + #[regex(":[0-9]+", port)] + Port(u16), + + #[regex(r#"[a-zA-Z0-9./\-=_!-+]+"#, |lex| lex.slice().parse().ok(), priority = 1)] + Path(String), + + #[regex(r#"\?[a-zA-Z0-9./\-!-+=]+"#, fragment)] + Query(String), + + #[regex("#[a-zA-Z0-9./]+", fragment)] + Fragment(String), +} + +fn port(lex: &mut Lexer) -> Option { + let slice = lex.slice(); + // println!("{}", slice); + + let n: u16 = slice[1..slice.len()].parse().ok()?; + Some(n) +} + +pub fn scheme_parse(lex: &mut Lexer) -> Option { + let slice: &str = lex.slice(); + // println!("{}", slice); + + let mut slice: String = slice.to_string(); + // slice.pop(); + Some(slice) +} + +pub fn fragment(lex: &mut Lexer) -> Option { + let slice: &str = lex.slice(); + let mut slice: String = slice.to_string(); + slice.remove(0); + Some(slice) +} diff --git a/libraries/uri/src/main.rs b/libraries/uri/src/main.rs new file mode 100644 index 0000000..f50fdf1 --- /dev/null +++ b/libraries/uri/src/main.rs @@ -0,0 +1,12 @@ +use logos::Logos; +use uri::Token; + +fn main() { + let lex = Token::lexer("http://ablecorp.us:112?hello#ABC"); + let lex = Token::lexer("tel:+1-816-555-1212"); + let lex = Token::lexer("https://www.amazon.com/Apple-EarPods-3-5mm-Headphone-Plug/dp/B06X16Z7DZ/ref=sr_1_3?crid=1L0TPYCPI8BU5&keywords=wired+ear+buds&qid=1683087424&sprefix=wired+ear+buds%2Caps%2C117&sr=8-3"); + + for br in lex { + println!("{:?}", br); + } +} diff --git a/libraries/xml/src/lib.rs b/libraries/xml/src/lib.rs index 77294e0..ee9ea54 100644 --- a/libraries/xml/src/lib.rs +++ b/libraries/xml/src/lib.rs @@ -1,6 +1,6 @@ #![no_std] -use core::fmt::{Debug, Display}; +use core::fmt::{self, Debug, Display}; #[macro_use] extern crate alloc; @@ -16,6 +16,12 @@ pub struct Attribute { name: String, value: String, } +impl fmt::Display for Attribute { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}={}", self.name, self.value)?; + Ok(()) + } +} impl Attribute { fn to_string(&self) -> String {