1
1
#![ warn( missing_debug_implementations, missing_docs, rust_2018_idioms) ]
2
2
#![ deny( unsafe_op_in_unsafe_fn) ]
3
+ #![ cfg_attr( not( feature = "std" ) , no_std) ]
3
4
4
5
//! A generic URI parser that strictly adheres to IETF [RFC 3986].
5
6
//!
9
10
//!
10
11
//! # Feature flags
11
12
//!
12
- //! All features are disabled by default. However, note that these features each
13
- //! alter the enum [`HostData`] in a backward incompatible way that could make it
13
+ //! All features except `std` are disabled by default. Note that the last two features
14
+ //! each alter the enum [`HostData`] in a backward incompatible way that could make it
14
15
//! impossible for two crates that depend on different features of `fluent-uri` to
15
16
//! be used together.
16
17
//!
18
+ //! - `std`: Enables `std` support. This includes [`Error`] implementations
19
+ //! and `Ip{v4, v6}Addr` support in [`HostData`].
20
+ //!
17
21
//! - `ipv_future`: Enables the parsing of [IPvFuture] literal addresses,
18
22
//! which fails with [`InvalidIpLiteral`] when disabled.
19
23
//!
26
30
//!
27
31
//! This feature is based on the homonymous [draft] and is thus subject to change.
28
32
//!
33
+ //! [`Error`]: std::error::Error
29
34
//! [IPvFuture]: https://datatracker.ietf.org/doc/html/rfc3986/#section-3.2.2
30
35
//! [`InvalidIpLiteral`]: ParseErrorKind::InvalidIpLiteral
31
- //! [draft]: https://datatracker.ietf.org/doc/html/draft-ietf-6man-rfc6874bis-02
36
+ //! [draft]: https://datatracker.ietf.org/doc/html/draft-ietf-6man-rfc6874bis-05
37
+
38
+ extern crate alloc;
32
39
33
40
/// Utilities for percent-encoding.
34
41
pub mod enc;
@@ -41,13 +48,11 @@ pub use view::*;
41
48
mod parser;
42
49
43
50
use crate :: enc:: { EStr , Split } ;
44
- use std:: {
45
- marker:: PhantomData ,
46
- mem:: ManuallyDrop ,
47
- net:: { Ipv4Addr , Ipv6Addr } ,
48
- ptr:: NonNull ,
49
- slice, str,
50
- } ;
51
+ use alloc:: { string:: String , vec:: Vec } ;
52
+ use core:: { iter:: Iterator , marker:: PhantomData , mem:: ManuallyDrop , ptr:: NonNull , slice, str} ;
53
+
54
+ #[ cfg( feature = "std" ) ]
55
+ use std:: net:: { Ipv4Addr , Ipv6Addr } ;
51
56
52
57
mod internal;
53
58
use internal:: * ;
@@ -90,9 +95,10 @@ impl ParseError {
90
95
}
91
96
}
92
97
98
+ #[ cfg( feature = "std" ) ]
93
99
impl std:: error:: Error for ParseError { }
94
100
95
- type Result < T , E = ParseError > = std :: result:: Result < T , E > ;
101
+ type Result < T , E = ParseError > = core :: result:: Result < T , E > ;
96
102
97
103
#[ cold]
98
104
fn len_overflow ( ) -> ! {
@@ -135,7 +141,7 @@ fn len_overflow() -> ! {
135
141
/// # Examples
136
142
///
137
143
/// Create and convert between `Uri<&str>` and `Uri<String>`:
138
- ///
144
+ ///
139
145
/// ```
140
146
/// use fluent_uri::Uri;
141
147
///
@@ -882,19 +888,22 @@ impl<'i, 'o, T: Io<'i, 'o>> Host<T> {
882
888
/// Returns the structured host data.
883
889
#[ inline]
884
890
pub fn data ( & ' i self ) -> HostData < ' o > {
885
- let data = self . raw_data ( ) ;
891
+ let _data = self . raw_data ( ) ;
886
892
let tag = self . auth . uri . tag ;
887
893
// SAFETY: We only access the union after checking the tag.
888
894
unsafe {
889
895
if tag. contains ( Tag :: HOST_REG_NAME ) {
890
896
// SAFETY: The validation is done.
891
897
return HostData :: RegName ( EStr :: new_unchecked ( self . as_str ( ) . as_bytes ( ) ) ) ;
892
898
} else if tag. contains ( Tag :: HOST_IPV4 ) {
893
- return HostData :: Ipv4 ( data. ipv4_addr ) ;
899
+ return HostData :: Ipv4 (
900
+ #[ cfg( feature = "std" ) ]
901
+ _data. ipv4_addr ,
902
+ ) ;
894
903
}
895
904
#[ cfg( feature = "ipv_future" ) ]
896
905
if !tag. contains ( Tag :: HOST_IPV6 ) {
897
- let dot_i = data . ipv_future_dot_i ;
906
+ let dot_i = _data . ipv_future_dot_i ;
898
907
let bounds = self . bounds ( ) ;
899
908
// SAFETY: The indexes are within bounds and the validation is done.
900
909
return HostData :: IpvFuture {
@@ -903,10 +912,11 @@ impl<'i, 'o, T: Io<'i, 'o>> Host<T> {
903
912
} ;
904
913
}
905
914
HostData :: Ipv6 {
906
- addr : data. ipv6 . addr ,
915
+ #[ cfg( feature = "std" ) ]
916
+ addr : _data. ipv6 . addr ,
907
917
// SAFETY: The indexes are within bounds and the validation is done.
908
918
#[ cfg( feature = "rfc6874bis" ) ]
909
- zone_id : data
919
+ zone_id : _data
910
920
. ipv6
911
921
. zone_id_start
912
922
. map ( |start| self . auth . uri . slice ( start. get ( ) , self . bounds ( ) . 1 - 1 ) ) ,
@@ -919,10 +929,13 @@ impl<'i, 'o, T: Io<'i, 'o>> Host<T> {
919
929
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
920
930
pub enum HostData < ' a > {
921
931
/// An IPv4 address.
922
- Ipv4 ( Ipv4Addr ) ,
932
+ #[ cfg_attr( not( feature = "std" ) , non_exhaustive) ]
933
+ Ipv4 ( #[ cfg( feature = "std" ) ] Ipv4Addr ) ,
923
934
/// An IPv6 address.
935
+ #[ cfg_attr( not( feature = "std" ) , non_exhaustive) ]
924
936
Ipv6 {
925
937
/// The address.
938
+ #[ cfg( feature = "std" ) ]
926
939
addr : Ipv6Addr ,
927
940
/// An optional zone identifier.
928
941
///
0 commit comments