Git Product home page Git Product logo

Comments (9)

LPardue avatar LPardue commented on July 17, 2024 1

Thanks. And thanks for making this library!

I can live with the syntax as is, it's idiomatic Rust and that is good.

I think it would help people like me to document how to extract items from lists or dictionaries with a couple of examples. Just so that we don't have to bother you with questions.

from sfv.

LPardue avatar LPardue commented on July 17, 2024

Ok, hunting through the code and tweaking things slightly gives me a snippet that seems to achieve what I want

  if let Some(ListEntry::Item(item)) = dict.get("u") {
        let item_value = item
            .bare_item
            .as_int()
            .ok_or("unexpected BareItem variant").unwrap();
        assert_eq!(item_value, 2);
    }

I wonder if there is a way to do this with some more syntactic sugar, how do other libraries approach it.

from sfv.

LPardue avatar LPardue commented on July 17, 2024

I guess this can be reduced down to

let urgency = match dict.get("u") {
        Some(ListEntry::Item(item)) => item.bare_item.as_int(),
        _ => None
    };

which would probably work for me.

from sfv.

undef1nd avatar undef1nd commented on July 17, 2024

Hello @LPardue
Thanks for checking this crate.
Your question is a good one. I agree, that it's too verbose and getting value requires many levels of matching. Hopefully, I'll find a way to make the api better.

For the time being I unwrap values like this, pretty similar to your snippet:

use sfv::*;

fn main() {
    let ext_priority = "u=2,i";
    let dict = Parser::parse_dictionary(ext_priority.as_bytes()).unwrap();


    if let Some(ListEntry::Item(item)) = dict.get("u") {
        match item.bare_item {
            BareItem::Integer(val) => { println!("{}", val);}
            _ => ()
        }
    }
}

from sfv.

undef1nd avatar undef1nd commented on July 17, 2024

I guess this can be reduced down to

let urgency = match dict.get("u") {
        Some(ListEntry::Item(item)) => item.bare_item.as_int(),
        _ => None
    };

which would probably work for me.

I think you'll still need either to match on urgency to get value returned by as_int(), or just call urgency.is_some() to make sure it's indeed sh-integer.

use sfv::*;

fn main() {
    let ext_priority = "u=2,i";
    let dict = Parser::parse_dictionary(ext_priority.as_bytes()).unwrap();

    let urgency = match dict.get("u") {
        Some(ListEntry::Item(item)) => item.bare_item.as_int(),
        _ => None,
    };
    
    if let Some(val) = urgency {
        // do something with val
        println!("{}", val);
    }
}

from sfv.

LPardue avatar LPardue commented on July 17, 2024

Yeah I left that off my snippet thanks.

My intent was to collapse down the two matches into a single Option but I wonder if that is actually the right thing to do in this case, let me explain.

In extensible priorities, omitting a parameter implies a default value. So if we received a header without "u", we should assume the default value 3. This needs to be done in my code not your parser code.

I'm not exactly sure how to handle the case whe "u" is present but not a valid sh-integer. I can assume a default, or reject a request, or maybe something else.

from sfv.

undef1nd avatar undef1nd commented on July 17, 2024

yeah, looks like you still need separate branches - one to handle missing u in dict, and the other one if u's value is not int.
Maybe if you need to fallback to default value when u is missing, this will work better? a bit more concise

const DEFAULT: i64 = 3;
let dict = Parser::parse_dictionary(ext_priority.as_bytes()).unwrap();
let priority = {
        if let Some(ListEntry::Item(item)) = dict.get("u") {
            item.bare_item.as_int().unwrap_or(DEFAULT) // if u is not sh-integer
        } else {
            DEFAULT // if u is missing
        }
    };

from sfv.

undef1nd avatar undef1nd commented on July 17, 2024

You're right, I will definitely add a few examples to the docs.
Sorry for the confusion around the api and thanks for raising this issue.

from sfv.

LPardue avatar LPardue commented on July 17, 2024

I'm closing this issue as we found a good solution to my problem and you have a new issue for the documentation thingie. Thanks again.

from sfv.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.