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

Update tutorials for 1.0 and automate testing where possible #2281

Closed
sffc opened this issue Jul 29, 2022 · 18 comments
Closed

Update tutorials for 1.0 and automate testing where possible #2281

sffc opened this issue Jul 29, 2022 · 18 comments
Assignees
Labels
C-meta Component: Relating to ICU4X as a whole S-medium Size: Less than a week (larger bug fix or enhancement) T-docs-tests Type: Code change outside core library

Comments

@sffc
Copy link
Member

sffc commented Jul 29, 2022

The tutorials in files such as "intro.md" are woefully out of date. We should make sure they are updated for 1.0, and also automate testing as much as possible, e.g. by moving code samples into a module such as icu::tutorial and using docs tests.

@sffc sffc added T-docs-tests Type: Code change outside core library C-meta Component: Relating to ICU4X as a whole S-medium Size: Less than a week (larger bug fix or enhancement) labels Jul 29, 2022
@sffc sffc added this to the ICU4X 1.0 (Final) milestone Jul 29, 2022
@dsipasseuth dsipasseuth self-assigned this Sep 23, 2022
@dsipasseuth
Copy link
Contributor

Do you have a preferred location/structure for the module ?

For now, I'll just put it under component/tutorial on my PR, but happy to move it anywhere it makes sense.

@sffc
Copy link
Member Author

sffc commented Sep 23, 2022

components/icu/src/tutorial.rs would be a good spot. You can make the module #[cfg(doc)] and then it shows up in docs but not elsewhere.

@dsipasseuth
Copy link
Contributor

dsipasseuth commented Sep 23, 2022

Do you want to merge all 4 md files from the tutorial folder into one giant tutorial.rs file ?

@sffc
Copy link
Member Author

sffc commented Sep 23, 2022

You could make a directory components/icu/src/tutorials and then different .rs files inside

@dsipasseuth
Copy link
Contributor

There's actually a way to make .md files contain rust code that will be tested just by adding a few directive.

#[doc = include_str!("../../../docs/tutorials/intro.md")]
#[doc = include_str!("../../../docs/tutorials/data_provider.md")]
#[doc = include_str!("../../../docs/tutorials/writing_a_new_data_struct.md")]
#[cfg(doctest)]
pub struct Docs;

Also, I need some help with the "writing a new data struct". I could not manage to get a "simple version" that just compiles and run.
I think it needs a more in depth rewrite. (May be use hello_world.rs as a baseline for a "step by step" and explaining each concept as it move further in the implementation ?)

For now, I've remove the examples code from writing_a_new_data_struct.md and replace them with Github code refs.
IIUC, the tutorial is highlighing existing code, so replacing it should offer better readability with link to actual code, but it's still not going to be tested by cargo doc. (But if it links to an actual rust file that is compiled, so do we actually need it ?)

Exemple of how it looks:

impl DataProvider<HelloWorldV1Marker> for HelloWorldProvider {
fn load(&self, req: DataRequest) -> Result<DataResponse<HelloWorldV1Marker>, DataError> {
#[allow(clippy::indexing_slicing)] // binary_search
let data = Self::DATA
.binary_search_by(|(k, _)| req.locale.strict_cmp(k.as_bytes()).reverse())
.map(|i| Self::DATA[i].1)
.map(|s| HelloWorldV1 {
message: Cow::Borrowed(s),
})
.map_err(|_| DataErrorKind::MissingLocale.with_req(HelloWorldV1Marker::KEY, req))?;
Ok(DataResponse {
metadata: Default::default(),
payload: Some(DataPayload::from_owned(data)),
})
}
}

Only problem is that blob is linked to a specific version of the file, I don't think it can follow "head". (May be there's a trick I don't know about ?)
Updating github code ref should be more straightforward than changing code.

Let me know what you think :)

@Manishearth
Copy link
Member

Only problem is that blob is linked to a specific version of the file, I don't think it can follow "head". (May be there's a trick I don't know about ?)

Replace the commit hash with main

@dsipasseuth
Copy link
Contributor

@Manishearth
Copy link
Member

that link works, it won't inline it though, if that's what you were wondering.

@dsipasseuth
Copy link
Contributor

The point was to actually have it point to an actual code in repository, but displayable for doc.

Current doc right now points to files and have code snippet.
But files have changed a lot, and are definitely not up to date anymore.

I managed to point it back to where it moved.

@Manishearth
Copy link
Member

Yeah that's not really possible, sadly.

@sffc
Copy link
Member Author

sffc commented Sep 26, 2022

I'm unfortunately moving this issue to the 1.1 milestone since it is not a strict blocker to the 1.0 code release. However, we still want the tutorials ready for the 1.0 blog post and publicity which is in the coming week.

@dsipasseuth
Copy link
Contributor

I think we have a Chicken and egg problem in data_management.md. (mod version)

rust code need to compile, but using include on a non-existing mod.rs will fail the compilation.
So no binary to analyse in target folder.
Without binary, icu4-datagen will not run because --keys-for-bin is mandatory as far as I understand.

May be there's something I missed in the development process for the users ?

@Manishearth
Copy link
Member

@dsipasseuth you can do mod data_management {} so you don't need to define a mod.rs

I don't quite understand the rest of your problem

@dsipasseuth
Copy link
Contributor

It's not about the doc itself, it's about the tutorial itself.
I think the mod assume the application is already using ICU4X already. (either by blob or FS)

On an app that doesn't use ICU4X yet, and want to use it.

let's consider this app not using ICU4X

fn main {
  println!("Hello");
}

This commands fails. (there's nothing to generate, because... well, there's nothing !)

icu4x-datagen --cldr-tag latest --icuexport-tag latest --out my-data --format mod --keys-for-bin `target/debug/myapp` --locales ja

But if the user add this in his code, it doesn't compile anymore... because the file does not exists. (if it doesn't compile, there's nothing in target/debug/myapp to analyze for keys...)

include!("../my-data/mod.rs"); // defines BakedDataProvider

Not sure if I'm clear. May be it's just overthinking it.

@Manishearth
Copy link
Member

Ah, I see, you're talking about the BakedDataProvider stuff.

So yes, you cannot use static data slicing if your application only supports BakedDataProvider. There are other ways to get the list of keys, and there are other kinds of providers you can use in the meantime.

@robertbastian
Copy link
Member

I think the fix here could just be to not error in datagen if no keys are selected

@robertbastian
Copy link
Member

Actually this is a proper bug in datagen, the Rust API allows no keys, so the CLI should as well. #2731

@dsipasseuth
Copy link
Contributor

ok, doctest are being performed for tutorials whenever possible. closing this bug.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-meta Component: Relating to ICU4X as a whole S-medium Size: Less than a week (larger bug fix or enhancement) T-docs-tests Type: Code change outside core library
Projects
None yet
Development

No branches or pull requests

4 participants