Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vec Slices with pointer? #1063

Open
naterichman opened this issue Sep 5, 2023 · 0 comments
Open

Vec Slices with pointer? #1063

naterichman opened this issue Sep 5, 2023 · 0 comments

Comments

@naterichman
Copy link

naterichman commented Sep 5, 2023

Still fairly new to rust so apologies if I'm misunderstanding and/or using lingo wrong.

Looking at the code for pointer it doesn't seem like it would support slices, i.e.

{
    "test": {
        "test2": [
            "a",
            "b",
            "c"
        ]
    }
}

I'd like to do something like: /test/test2/0..2 and also support nested structures. I've been trying to implement this myself (somewhat as a learning exercise, but also it would be useful), but I'm getting stuck on Value::Array needing owned values. I tried modifying the pointer function:

       pointer
            .split('/')
            .skip(1)
            .map(|x| x.replace("~1", "/").replace("~0", "~"))
            .try_fold(self, |target, token| match target {
                Value::Object(map) => map.get(&token),
                Value::Array(list) => {
                    let val = parse_index(&token).and_then(|x| list.get(x));
                    if val.is_some() {
                        return val
                   } else {
                        return Some(&parse_range(&token)
                            .and_then(|range| Value::from(&list[range]))
                        )
                },
                _ => None,
            })

But I get a dangling reference. I understand why I get it, but I can't figure out a way around it. Would appreciate some guidance on how I'm approaching this, thanks in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant