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

[really good to do] Add a way to map Value into another Value rather then a String #490

Open
zhiburt opened this issue Jan 25, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@zhiburt
Copy link
Owner

zhiburt commented Jan 25, 2025

So we do have #[tabled(display_with)] which very good, but it does require us to return String.
Which sometimes is not what we are looking for.

Sometimes it's valuable to convert a value into another value so it could be #[tabled(inline)]ed.

Here's a good example from #486

// we have this structure
struct DbEntry {
    index: usize,
    time: u64,
    text: String,
}

// but we wanna to print the structure
pub struct Entry {
    index: usize,
    #[tabled(inline)]
    time: Date,
    text: String,
}

pub struct Date {
    y: usize,
    m: usize,
    d: usize,
}

So the result will be something like

struct DbEntry {
    index: usize,
    #[tabled(map = "Date::from_u64")]
    #[tabled(inline)]    
    time: u64,
    text: String,
}

pub struct Date {
    y: usize,
    m: usize,
    d: usize,
}

I guess we need one more macro helper?


And actually it's valuable not only for inline case,
but we could map and the value could impl Display so we would kind be ok with it.

So yessss
Very prominent idea.
I do very like it.

@zhiburt zhiburt added enhancement New feature or request good first issue Good for newcomers labels Jan 25, 2025
@zhiburt zhiburt changed the title Add a way to map Value into another Value rather then a String [really good to do] Add a way to map Value into another Value rather then a String Jan 25, 2025
@KekmaTime
Copy link

@zhiburt can i take up this issue

@zhiburt
Copy link
Owner Author

zhiburt commented Jan 26, 2025

Hi @KekmaTime sure,

But it's not the easiest issue to cover,
cause tabled_derive is not quite clean 😅 .
It's good but could be better.

You would need to take a look at how display_with implemented, the difference will be in return type of function and that's it I guess?
So maybe not that difficult then?:)

If you'll encounter problems please let me know and I'll try to help.

let value = if let Some(func) = &attr.display_with {
let args = match &attr.display_with_args {
None => None,
Some(args) => match args.is_empty() {
true => None,
false => {
let args = args
.iter()
.map(|arg| fnarg_tokens(arg, &Fields::Unit, struct_field_name))
.collect::<Vec<_>>();
Some(quote!( #(#args,)* ))
}
},
};
let result = match args {
Some(args) => use_function(&args, func),
None => use_function_no_args(func),
};
quote! { ::std::borrow::Cow::from(format!("{}", #result)) }

@KekmaTime
Copy link

@zhiburt 👍🏽 for sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants