Artem-Romanenia / o2o

Object to Object mapper for Rust
Apache License 2.0
31 stars 2 forks source link

Add support for lifetime in `ref_into` #19

Closed vnghia closed 2 months ago

vnghia commented 2 months ago

Hi @Artem-Romanenia, I am not sure how to add tests so could you help me 😅 ?

Closes: #16

Artem-Romanenia commented 2 months ago

Hi @vnghia, so, the tests are in ./o2o-tests/tests folder. You can check existing tests, for example 18_where_attr_tests.rs (actual tests start at line 58), but you will see right away that they are hard to follow, because I was trying to test a bunch of related things in the same file, and I was defining structs at file level, and it's unreasonably hard to trace what maps with what... For some time now I wanted to try new approach and define structs inside the test itself.

So, maybe start by creating a file 45_lifetime_tests.rs, and there you can start with a test like:

#[test]
fn ref_into_test() {
    #[derive(o2o::o2o)]
    #[ref_into(EntityDto<'a, 'b>)]
    pub struct Entity {
        #[into(~.as_str())]
        pub some_a: String,
        #[into(~.as_str())]
        pub some_b: String,
    }

    pub struct EntityDto<'a, 'b> {
        pub some_a: &'a str,
        pub some_b: &'b str,
    }

    let entity = &Entity {
        some_a: "A".into(),
        some_b: "B".into()
    };

    let dto: EntityDto = entity.into();

    // Assert that dto.some_a equals entity.some_a and so on
} 

Maybe we can also have a test exactly like that, but where one field has different name.

Then we would need a test for from_ref.

Then all the same tests, but where we map to 2 structs at the same time, like

    #[derive(o2o::o2o)]
    #[ref_into(EntityDto<'a, 'b>)]
    #[ref_into(AnotherEntityDto<'b, 'c>)]
    pub struct Entity {
        #[into(~.as_str())]
        pub some_a: String,
        #[into(~.as_str())]
        pub some_b: String,
    }

And then we would need a file 45_lifetime_tests_fallible.rs with all the same tests but this time for TryFrom and TryInto...

I'm currently running out of time, and I'm afraid I didn't make it any clearer for you, so maybe start with a couple of things, and then we'll see how it goes)

P.S. We would also need tests where we do 'compound' instructions, like "map" or "from" or "into", and check that ref conversions work without altering how 'non-ref' implementations are expanded...

Artem-Romanenia commented 2 months ago

Hi @vnghia, do you mind if I merge your changes in their current state and proceed from here myself?

vnghia commented 2 months ago

Yeah please do it. I am intent to do something with it this weekend but I think you will have a better insight than me about adding tests and also some syntax confusions we dicussed in the issue.

Artem-Romanenia commented 2 months ago

Good, thanks a lot for your contribution! I'll post in #16 when there's a crate release with your changes