Skip to content

Commit

Permalink
hopefully fixes std feature by marking serde_json optional
Browse files Browse the repository at this point in the history
  • Loading branch information
chanced committed Jul 1, 2024
1 parent 9f95d72 commit 3d52ca8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ delete = ["resolve"]
impl = []
json = ["dep:serde_json", "serde"]
resolve = []
std = ["serde/std", "serde_json/std"]
std = ["serde/std", "serde_json?/std"]
toml = ["dep:toml", "serde", "std"]
43 changes: 20 additions & 23 deletions src/pointer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,21 @@ impl Pointer {
///
/// This is a zero-copy constructor
fn new<S: AsRef<str> + ?Sized>(s: &S) -> &Self {
unsafe { &*(core::ptr::from_ref::<str>(s.as_ref()) as *const Self) }
// unsafe { &*(core::ptr::from_ref::<str>(s.as_ref()) as *const Self) }
#[allow(clippy::ref_as_ptr)]
unsafe {
&*(s.as_ref() as *const str as *const Self)
}
}

/// Constant reference to a root pointer
pub const fn root() -> &'static Self {
unsafe { &*(core::ptr::from_ref::<str>("") as *const Self) }
// unsafe { &*(core::ptr::from_ref::<str>("") as *const Self) }
#[allow(clippy::ref_as_ptr)]
unsafe {
&*("" as *const str as *const Self)
}
}

/// Attempts to parse a string into a `Pointer`.
Expand Down Expand Up @@ -88,26 +96,27 @@ impl Pointer {
/// let bar = data.resolve(POINTER).unwrap();
/// assert_eq!(bar, "baz");
/// ````
pub const fn from_static(s: &'static str) -> &'static Self {
assert!(validate(s).is_ok(), "invalid json pointer");
unsafe { &*(core::ptr::from_ref::<str>(s) as *const Self) }
// unsafe { &*(core::ptr::from_ref::<str>(s) as *const Self) }
#[allow(clippy::ref_as_ptr)]
unsafe {
&*(s as *const str as *const Self)
}
}

/// The encoded string representation of this `Pointer`
pub fn as_str(&self) -> &str {
&self.0
}

/// Converts into an owned [`PointerBuf`]
pub fn to_buf(&self) -> PointerBuf {
PointerBuf(self.0.to_string())
}

/// Returns an iterator of `Token`s in the `Pointer`.
pub fn tokens(&self) -> Tokens {
let mut s = self.0.split('/');
// skipping the first '/'
Expand All @@ -116,26 +125,22 @@ impl Pointer {
}

/// Returns the number of tokens in the `Pointer`.
pub fn count(&self) -> usize {
self.tokens().count()
}

/// Returns `true` if the JSON Pointer equals `""`.
pub fn is_root(&self) -> bool {
self.0.is_empty()
}

/// Returns a `serde_json::Value` representation of this `Pointer`
#[cfg(feature = "json")]
pub fn to_json_value(&self) -> serde_json::Value {
serde_json::Value::String(self.0.to_string())
}

/// Returns the last `Token` in the `Pointer`.
pub fn back(&self) -> Option<Token> {
self.0
.rsplit_once('/')
Expand All @@ -145,13 +150,11 @@ impl Pointer {
/// Returns the last token in the `Pointer`.
///
/// alias for `back`
pub fn last(&self) -> Option<Token> {
self.back()
}

/// Returns the first `Token` in the `Pointer`.
pub fn front(&self) -> Option<Token> {
if self.is_root() {
return None;
Expand All @@ -168,13 +171,11 @@ impl Pointer {
/// Returns the first `Token` in the `Pointer`.
///
/// alias for `front`
pub fn first(&self) -> Option<Token> {
self.front()
}

/// Splits the `Pointer` into the first `Token` and a remainder `Pointer`.
pub fn split_front(&self) -> Option<(Token, &Self)> {
if self.is_root() {
return None;
Expand Down Expand Up @@ -215,7 +216,6 @@ impl Pointer {
/// assert_eq!(tail, Pointer::from_static("/bar/baz"));
/// assert_eq!(ptr.split_at(3), None);
/// ```
pub fn split_at(&self, idx: usize) -> Option<(&Self, &Self)> {
if self.0.as_bytes().get(idx).copied() != Some(b'/') {
return None;
Expand All @@ -225,15 +225,13 @@ impl Pointer {
}

/// Splits the `Pointer` into the parent path and the last `Token`.
pub fn split_back(&self) -> Option<(&Self, Token)> {
self.0
.rsplit_once('/')
.map(|(front, back)| (Self::new(front), Token::from_encoded_unchecked(back)))
}

/// A pointer to the parent of the current path.
pub fn parent(&self) -> Option<&Self> {
self.0.rsplit_once('/').map(|(front, _)| Self::new(front))
}
Expand All @@ -258,7 +256,7 @@ impl Pointer {
/// let ptr = Pointer::root();
/// assert_eq!(ptr.get(0), None);
/// ```
pub fn get(&self, index: usize) -> Option<Token> {
self.tokens().nth(index).clone()
}
Expand Down Expand Up @@ -309,7 +307,6 @@ impl Pointer {
}

/// Finds the commonality between this and another `Pointer`.
pub fn intersection<'a>(&'a self, other: &Self) -> &'a Self {
if self.is_root() || other.is_root() {
return Self::root();
Expand Down Expand Up @@ -587,7 +584,7 @@ pub struct PointerBuf(String);

impl PointerBuf {
/// Creates a new `PointerBuf` pointing to a document root.
pub fn new() -> Self {
Self(String::new())
}
Expand All @@ -614,7 +611,7 @@ impl PointerBuf {
}

/// Coerces to a Pointer slice.
pub fn as_ptr(&self) -> &Pointer {
self
}
Expand Down

0 comments on commit 3d52ca8

Please sign in to comment.