Skip to content

Commit

Permalink
Data URLs & macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Jul 25, 2024
1 parent 021dc7a commit bd9f92f
Show file tree
Hide file tree
Showing 66 changed files with 796 additions and 117 deletions.
23 changes: 14 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@ readme = "README.md"

[features]
default = []
serde = ["dep:serde"]
ignore-grammars = []
macros = ["dep:iref-macros"]
serde = ["iref-core/serde"]
data = ["iref-core/data"]

[workspace.dependencies]
iref-core = { path = "crates/core", version = "3.1.4" }
iref-macros = { path = "crates/macros", version = "3.1.4" }

[dependencies]
pct-str = "2.0"
smallvec = "1.2"
thiserror = "1.0.40"
static-regular-grammar = "1.1.1"
serde = { version = "1.0", optional = true }
hashbrown = { version = "0.14.0", optional = true }
iref-core.workspace = true
iref-macros = { workspace = true, optional = true }

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[workspace]
members = ["crates/*"]

[[example]]
name = "serde"
required-features = [ "serde", "serde/derive" ]
required-features = [ "serde" ]
26 changes: 26 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "iref-core"
version = "3.1.4"
edition = "2021"
authors = ["Timothée Haudebourg <[email protected]>"]
categories = ["web-programming", "internationalization"]
keywords = ["iri", "uri", "url", "resource", "identifier"]
description = "Uniform & Internationalized Resource Identifiers (URIs/IRIs), borrowed and owned."
repository = "https://github.com/timothee-haudebourg/iref"
documentation = "https://docs.rs/iref"
license = "MIT/Apache-2.0"
readme = "README.md"

[features]
default = []
serde = ["dep:serde"]
data = ["dep:base64"]

[dependencies]
pct-str = "2.0"
smallvec = "1.2"
thiserror = "1.0.40"
static-regular-grammar = "2.0"
serde = { version = "1.0", optional = true }
hashbrown = { version = "0.14.0", optional = true }
base64 = { version = "0.22.1", optional = true }
9 changes: 9 additions & 0 deletions crates/core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Core type and traits for the `iref` library.

[![CI](https://github.com/timothee-haudebourg/iref/workflows/CI/badge.svg)](https://github.com/timothee-haudebourg/iref/actions)
[![Crate informations](https://img.shields.io/crates/v/iref.svg?style=flat-square)](https://crates.io/crates/iref)
[![License](https://img.shields.io/crates/l/iref.svg?style=flat-square)](https://github.com/timothee-haudebourg/iref#license)
[![Documentation](https://img.shields.io/badge/docs-latest-blue.svg?style=flat-square)](https://docs.rs/iref)

<!-- cargo-rdme start -->
<!-- cargo-rdme end -->
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 9 additions & 1 deletion src/common/parse.rs → crates/core/src/common/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,12 @@ pub fn port(bytes: &[u8], i: usize) -> (bool, usize) {
/// `bytes` must end at the end of the authority.
pub fn find_port(bytes: &[u8], mut i: usize) -> Option<Range<usize>> {
'host: while i < bytes.len() {
if bytes[i] == b':' {
// IP-literal.
if bytes[i] == b'[' {
while i < bytes.len() && bytes[i] != b']' {
i += 1
}
} else if bytes[i] == b':' {
let start = i + 1;

while i < bytes.len() {
Expand Down Expand Up @@ -546,6 +551,7 @@ mod tests {
("", Err(0)),
("/foo:bar", Err(0)),
("//user:[email protected]/", Ok("user:[email protected]")),
("//[::]", Ok("[::]")),
];

for (input, expected) in vectors {
Expand All @@ -563,6 +569,7 @@ mod tests {
("example.org", (UserInfoOrHost::Host, 11)),
("example.org:12", (UserInfoOrHost::Host, 11)),
(":12", (UserInfoOrHost::Host, 0)),
("[::]", (UserInfoOrHost::Host, 4)),
];

for (input, expected) in vectors {
Expand Down Expand Up @@ -635,6 +642,7 @@ mod tests {
("example.org", None),
("user:[email protected]:12", Some("12")),
("user:[email protected]", None),
("[::]", None),
];

for (input, expected) in vectors {
Expand Down
1 change: 1 addition & 0 deletions src/common/path.rs → crates/core/src/common/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@ pub trait PathImpl: 'static {
/// # Example
/// ```
/// # use std::convert::TryFrom;
/// # use iref_core as iref;
/// use iref::iri::{Path, PathBuf};
///
/// let prefix = Path::new("/foo/bar").unwrap();
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/iri/authority.rs → crates/core/src/iri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ pub use userinfo::*;

#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "iauthority",
name = "IRI authority",
cache = "automata/iri/authority.aut.cbor"
cache = "crates/core/automata/iri/authority.aut.cbor"
)]
#[grammar(sized(
AuthorityBuf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::common::authority::HostImpl;
/// IRI authority host.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "ihost",
name = "IRI host",
no_deref,
cache = "automata/iri/host.aut.cbor"
cache = "crates/core/automata/iri/host.aut.cbor"
)]
#[grammar(sized(HostBuf, derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)))]
#[cfg_attr(feature = "serde", grammar(serde))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use crate::common::authority::UserInofImpl;

#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "iuserinfo",
name = "IRI user info",
no_deref,
cache = "automata/iri/userinfo.aut.cbor"
cache = "crates/core/automata/iri/userinfo.aut.cbor"
)]
#[grammar(sized(
UserInfoBuf,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/iri/fragment.rs → crates/core/src/iri/fragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::common::FragmentImpl;
/// IRI fragment.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "ifragment",
name = "IRI fragment",
no_deref,
cache = "automata/iri/fragment.aut.cbor"
cache = "crates/core/automata/iri/fragment.aut.cbor"
)]
#[grammar(sized(
FragmentBuf,
Expand Down
File renamed without changes.
12 changes: 8 additions & 4 deletions src/iri.rs → crates/core/src/iri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ iri_error! {
/// # Example
///
/// ```rust
/// # use iref_core as iref;
/// use iref::iri::{Iri, Scheme, Authority, Path, Query, Fragment};
/// # fn main() -> Result<(), iref::iri::InvalidIri<&'static str>> {
/// let iri = Iri::new("https://www.rust-lang.org/foo/bar?query#fragment")?;
Expand All @@ -120,9 +121,9 @@ iri_error! {
/// ```
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "IRI",
cache = "automata/iri.aut.cbor"
cache = "crates/core/automata/iri.aut.cbor"
)]
#[grammar(sized(IriBuf, derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)))]
#[cfg_attr(feature = "serde", grammar(serde))]
Expand Down Expand Up @@ -218,7 +219,7 @@ impl Iri {
///
/// # Example
/// ```
/// # use iref::Iri;
/// # use iref_core::Iri;
/// let a = Iri::new("https://crates.io/").unwrap();
/// let b = Iri::new("https://crates.io/crates/iref").unwrap();
/// let c = Iri::new("https://crates.io/crates/json-ld").unwrap();
Expand Down Expand Up @@ -251,7 +252,7 @@ impl Iri {
///
/// # Example
/// ```
/// # use iref::Iri;
/// # use iref_core::Iri;
/// let a = Iri::new("https://crates.io/crates/iref?query#fragment").unwrap();
/// let b = Iri::new("https://crates.io/crates/iref/?query#fragment").unwrap();
/// assert_eq!(a.base(), "https://crates.io/crates/");
Expand Down Expand Up @@ -477,6 +478,7 @@ impl IriBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriBuf, iri::Scheme};
///
/// let mut a = IriBuf::new("http://example.org/path".to_string()).unwrap();
Expand All @@ -499,6 +501,7 @@ impl IriBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriBuf, iri::Authority};
///
/// let mut a = IriBuf::new("scheme:/path".to_string()).unwrap();
Expand Down Expand Up @@ -536,6 +539,7 @@ impl IriBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriBuf, iri::Path};
///
/// let mut a = IriBuf::new("http://example.org/old/path".to_string()).unwrap();
Expand Down
11 changes: 6 additions & 5 deletions src/iri/path.rs → crates/core/src/iri/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use super::PathMut;
/// IRI path.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "ipath",
name = "IRI path",
cache = "automata/iri/path.aut.cbor"
cache = "crates/core/automata/iri/path.aut.cbor"
)]
#[grammar(sized(PathBuf, derive(Debug, Display)))]
#[cfg_attr(feature = "serde", grammar(serde))]
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Path {
/// # Example
///
/// ```
/// # use iref::iri::Path;
/// # use iref_core::iri::Path;
/// assert_eq!(Path::new("/foo/bar").unwrap().directory(), "/foo/");
/// assert_eq!(Path::new("/foo").unwrap().directory(), "/");
/// assert_eq!(Path::new("//foo").unwrap().directory(), "//");
Expand All @@ -159,7 +159,7 @@ impl Path {
/// Returns the path without its final segment, if there is one.
///
/// ```
/// # use iref::iri::Path;
/// # use iref_core::iri::Path;
/// assert_eq!(Path::new("/foo/bar").unwrap().parent().unwrap(), "/foo");
/// assert_eq!(Path::new("/foo").unwrap().parent().unwrap(), "/");
/// assert_eq!(Path::new("//foo").unwrap().parent().unwrap(), "/./");
Expand All @@ -173,7 +173,7 @@ impl Path {
/// Returns the path without its final segment, if there is one.
///
/// ```
/// # use iref::iri::Path;
/// # use iref_core::iri::Path;
/// assert_eq!(Path::new("/foo/bar").unwrap().parent_or_empty(), "/foo");
/// assert_eq!(Path::new("/foo").unwrap().parent_or_empty(), "/");
/// assert_eq!(Path::new("//foo").unwrap().parent_or_empty(), "/./");
Expand All @@ -196,6 +196,7 @@ impl Path {
/// # Example
/// ```
/// # use std::convert::TryFrom;
/// # use iref_core as iref;
/// use iref::iri::{Path, PathBuf};
///
/// let prefix = Path::new("/foo/bar").unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::common::path::SegmentImpl;
/// IRI path segment.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "isegment",
name = "IRI path segment",
no_deref,
cache = "automata/iri/segment.aut.cbor"
cache = "crates/core/automata/iri/segment.aut.cbor"
)]
#[grammar(sized(
SegmentBuf,
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/iri/query.rs → crates/core/src/iri/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use crate::common::QueryImpl;
/// IRI query.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "iquery",
name = "IRI query",
no_deref,
cache = "automata/iri/query.aut.cbor"
cache = "crates/core/automata/iri/query.aut.cbor"
)]
#[grammar(sized(QueryBuf, derive(Debug, Display, PartialEq, Eq, PartialOrd, Ord, Hash)))]
#[cfg_attr(feature = "serde", grammar(serde))]
Expand Down
11 changes: 7 additions & 4 deletions src/iri/reference.rs → crates/core/src/iri/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ use super::{Authority, AuthorityMut, Fragment, Path, PathBuf, PathMut, Query, Sc
/// IRI reference.
#[derive(RegularGrammar)]
#[grammar(
file = "src/iri/grammar.abnf",
file = "crates/core/src/iri/grammar.abnf",
entry_point = "IRI-reference",
name = "IRI reference",
cache = "automata/iri/reference.aut.cbor"
cache = "crates/core/automata/iri/reference.aut.cbor"
)]
#[grammar(sized(
IriRefBuf,
Expand Down Expand Up @@ -128,7 +128,7 @@ impl IriRef {
///
/// # Example
/// ```
/// # use iref::IriRef;
/// # use iref_core::IriRef;
/// let a = IriRef::new("https://crates.io/").unwrap();
/// let b = IriRef::new("https://crates.io/crates/iref").unwrap();
/// let c = IriRef::new("https://crates.io/crates/json-ld").unwrap();
Expand Down Expand Up @@ -161,7 +161,7 @@ impl IriRef {
///
/// # Example
/// ```
/// # use iref::IriRef;
/// # use iref_core::IriRef;
/// let a = IriRef::new("https://crates.io/crates/iref?query#fragment").unwrap();
/// let b = IriRef::new("https://crates.io/crates/iref/?query#fragment").unwrap();
/// assert_eq!(a.base(), "https://crates.io/crates/");
Expand Down Expand Up @@ -388,6 +388,7 @@ impl IriRefBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriRefBuf, iri::Scheme};
///
/// let mut a = IriRefBuf::new("foo/bar".to_string()).unwrap();
Expand Down Expand Up @@ -418,6 +419,7 @@ impl IriRefBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriRefBuf, iri::Authority};
///
/// let mut a = IriRefBuf::new("scheme:/path".to_string()).unwrap();
Expand Down Expand Up @@ -455,6 +457,7 @@ impl IriRefBuf {
/// # Example
///
/// ```
/// # use iref_core as iref;
/// use iref::{IriRefBuf, iri::Path};
///
/// let mut a = IriRefBuf::new("http://example.org/old/path".to_string()).unwrap();
Expand Down
7 changes: 7 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub(crate) mod common;
pub mod iri;
pub mod uri;
pub(crate) mod utils;

pub use iri::{InvalidIri, Iri, IriBuf, IriError, IriRef, IriRefBuf};
pub use uri::{InvalidUri, Uri, UriBuf, UriError, UriRef, UriRefBuf};
4 changes: 2 additions & 2 deletions src/uri/authority.rs → crates/core/src/uri/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ pub use userinfo::*;

#[derive(RegularGrammar)]
#[grammar(
file = "src/uri/grammar.abnf",
file = "crates/core/src/uri/grammar.abnf",
entry_point = "authority",
name = "URI authority",
ascii,
cache = "automata/uri/authority.aut.cbor"
cache = "crates/core/automata/uri/authority.aut.cbor"
)]
#[grammar(sized(
AuthorityBuf,
Expand Down
Loading

0 comments on commit bd9f92f

Please sign in to comment.