-
Notifications
You must be signed in to change notification settings - Fork 55
Docs images helper #190
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
base: master
Are you sure you want to change the base?
Docs images helper #190
Changes from 2 commits
f1415d2
fd042d8
09609e1
1ea82af
b477fce
4005175
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,8 @@ once_cell = "1.3" | |
|
|
||
| cfg-if = "0.1" | ||
|
|
||
| turtle_docs_helper = { path = "turtle_docs_helper", optional = true } | ||
|
|
||
| [dependencies.futures-util] | ||
| version = "0.3" | ||
| default-features = false | ||
|
|
@@ -90,7 +92,8 @@ opt-level = 3 | |
|
|
||
| [features] | ||
| # The reason we do this is because doctests don't get cfg(test) | ||
| # See: https://github.com/rust-lang/cargo/issues/4669 | ||
| # See: https://github.com/rust-lang/cargo/issues/4669 (original issue) | ||
| # See: https://github.com/rust-lang/rust/issues/45599 (updated issue) | ||
JosephLing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # | ||
| # This allows us to write attributes like the following and have it work | ||
| # in all tests. | ||
|
|
@@ -105,3 +108,4 @@ test = [] | |
| # | ||
| # Users of the crate must explicitly opt-in to activate them. | ||
| unstable = [] | ||
| docs_image = ["turtle_docs_helper"] | ||
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,10 +2,19 @@ use std::fmt::Debug; | |||||
|
|
||||||
| use tokio::time; | ||||||
|
|
||||||
| use crate::radians::{self, Radians}; | ||||||
| use crate::ipc_protocol::{ProtocolClient, RotationDirection}; | ||||||
| use crate::radians::{self, Radians}; | ||||||
| use crate::renderer_server::TurtleId; | ||||||
| use crate::{Turtle, Color, Point, Speed}; | ||||||
| use crate::{Color, Point, Speed, Turtle}; | ||||||
|
|
||||||
| #[cfg(feature = "docs_image")] | ||||||
| use turtle_docs_helper; | ||||||
|
|
||||||
| #[cfg(feature = "docs_image")] | ||||||
| use std::path::Path; | ||||||
|
|
||||||
| #[cfg(feature = "docs_image")] | ||||||
| use crate::sync_runtime::block_on; | ||||||
|
|
||||||
| /// Any distance value (positive or negative) | ||||||
| pub type Distance = f64; | ||||||
|
|
@@ -45,6 +54,16 @@ pub struct AsyncTurtle { | |||||
| angle_unit: AngleUnit, | ||||||
| } | ||||||
|
|
||||||
| #[cfg(feature = "docs_image")] | ||||||
| impl turtle_docs_helper::SaveSvg for AsyncTurtle { | ||||||
| fn save_svg(&self, path: &Path) -> Result<(), String> { | ||||||
|
||||||
| fn save_svg(&self, path: &Path) -> Result<(), String> { | |
| fn save_svg(&self, path: &std::path::Path) -> Result<(), String> { |
We can avoid having to import Path by using its fully qualified name here. Please do this for the other impls as well.
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,12 @@ | ||||||||||
| use std::fmt::Debug; | ||||||||||
| use std::path::Path; | ||||||||||
|
|
||||||||||
| use crate::{Turtle, Color, Point, Size, ExportError}; | ||||||||||
| use crate::async_drawing::AsyncDrawing; | ||||||||||
| use crate::sync_runtime::block_on; | ||||||||||
| use crate::{Color, ExportError, Point, Size, Turtle}; | ||||||||||
|
|
||||||||||
| #[cfg(feature = "docs_image")] | ||||||||||
| use turtle_docs_helper; | ||||||||||
|
|
||||||||||
| /// Provides access to properties of the drawing that the turtle is creating | ||||||||||
| /// | ||||||||||
|
|
@@ -63,7 +66,17 @@ impl From<AsyncDrawing> for Drawing { | |||||||||
| fn from(drawing: AsyncDrawing) -> Self { | ||||||||||
| //TODO: There is no way to set `turtles` properly here, but that's okay since it is going | ||||||||||
| // to be removed soon. | ||||||||||
| Self {drawing, turtles: 1} | ||||||||||
| Self { drawing, turtles: 1 } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| #[cfg(feature = "docs_image")] | ||||||||||
| impl turtle_docs_helper::SaveSvg for Drawing { | ||||||||||
| fn save_svg(&self, path: &Path) -> Result<(), String> { | ||||||||||
| match block_on(self.drawing.save_svg(path)) { | ||||||||||
JosephLing marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||
| Ok(()) => Ok(()), | ||||||||||
| Err(e) => Err(e.to_string()), | ||||||||||
| } | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -142,14 +155,17 @@ impl Drawing { | |||||||||
| /// | ||||||||||
| /// # Example | ||||||||||
| /// | ||||||||||
| /// ```rust,no_run | ||||||||||
| /// ```rust | ||||||||||
| /// use turtle::Drawing; | ||||||||||
| /// | ||||||||||
| /// fn main() { | ||||||||||
| /// let mut drawing = Drawing::new(); | ||||||||||
| /// # #[allow(unused)] // Good to show turtle creation here even if unused | ||||||||||
| /// let mut turtle = drawing.add_turtle(); | ||||||||||
| /// drawing.set_title("My Fancy Title! - Yay!"); | ||||||||||
| /// # #[cfg(feature = "docs_image")] | ||||||||||
| /// # turtle_docs_helper::save_docs_image(&drawing, "changed_title"); | ||||||||||
| /// | ||||||||||
|
||||||||||
| /// # #[cfg(feature = "docs_image")] | |
| /// # turtle_docs_helper::save_docs_image(&drawing, "changed_title"); | |
| /// | |
| /// # #[cfg(feature = "docs_image")] turtle_docs_helper::save_docs_image(&drawing, "changed_title"); |
Let's put the #[cfg] on the same line as the function call in all places where we call save_docs_image. None of this code is "for display" since the leading # hides it all anyway, so I would prefer that we keep it as terse as possible. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should really be a dev-dependency, even if it is optional.
I wonder if the dependency cfg syntax would work here. Something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried this out in the latest commit but can't seem to get it too work as it can't seem to find the crate when I import it. (I added some additional info to the commit message as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the trouble! I pulled the code locally and was able to get it working. Here's what you need in your Cargo.toml file:
Note that the dependency is not optional since dev-dependencies are apparently not allowed to be optional (that's fine). I unfortunately wasn't able to get
target.'cfg(docs_images)'.dev-dependenciesto work. It seems like it only works for selecting platforms. :(To run this, I needed to use the command:
A couple of things to note about this:
cargo testinstead ofcargo docto make sure the tests are actually run (instead of just generating documentation)testandunstablefeatures to make sure none of the tests in the code are left out--docto make sure only doc tests are run since regular unit tests don't generate imagesI think the reason you ran into so many issues was because I pointed you to CONTRIBUTING.md which uses
cargo doc. I don't think there is any way to use a dev-dependency fromcargo doc. Sorry about that!Hopefully this resolves the issues you were running into. Don't hesitate to let me know if you have any other questions! :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet, that was going to be my default choice if
target.'cfg(docs_images)'.dev-dependenciescouldn't work :) I think the main difficulty was the first time playing around with cfg to this extent.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After implementing the dev-dependancy chance I ran into another issue, the
SaveSvgwasn't being implemented for the theTurtlestruct. From testing out a fresh project I haven't been able to get the cfg flags to work for implementing traits for structs. As it won't run the implment block of code it seems. Here is the example I created:It should run the first test then fail the assert but the compiler can't find says(). (This is ran by using cargo test)
Potentailly it's the end of the week and I am missing something obivious as you said you were able to get it working.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you tell me the command you used to run the program and paste the error message?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with
RUSTDOCFLAGSset to--cfg docs_imagesin theenvI can push the code as well although there haven't really be any major changes.
EDIT: perhaps as I am running as a docs flag it is only affecting the doctests and nothing else 🤦♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is possible! Totally not your fault. I would personally expect it to work the way we have it. You can try fixing it by setting
RUSTFLAGSin addition toRUSTDOCFLAGS. Let me know if it works!And thanks for taking the time to experiment!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep because doc tests run kind of like integeration tests that was the problem. As the code it was running on didn't get the cfg flag passed to it. For
turtle_docs_helperto work as a crate we need it to only be required when running tests so:As its a dev dependency so not found when running the main build (
cargo build). So that should fix everything but we will see if anything else explodes when I next work on it hahaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically I think a feature flag like orginal commit is the only way to get this work and the nicest way I think. Writing the code is a bit more verbose but it's really simple to run and actually runs.
One of the issues is in order for cfg flags to work they are seperate for
RUSTFLAGSandRUSTDOCFLAGSso that means you need both set if you want the doctests to run thecfg(docs_images).I think because of that I didn't notice that this:
Doesn't work as this won't run (for
testortests):The
use turtle_docs_helperwill run so that module does get imported however you get the error: