Skip to content

Commit 6fc5339

Browse files
committed
Starting point for PR #190
1 parent ce7eb4a commit 6fc5339

File tree

8 files changed

+70
-1
lines changed

8 files changed

+70
-1
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ cfg-if = "0.1"
5151

5252
parking_lot = "0.10"
5353

54+
# This dependency should be uncommented when rebuilding the images in the API
55+
# documentation. In all other cases this dependency cannot be used since having
56+
# a path-only dependency would make it impossible to publish this crate.
57+
#turtle_docs_helpers = { path = "turtle_docs_helpers" }
58+
5459
[dependencies.futures-util]
5560
version = "0.3"
5661
default-features = false

src/async_drawing.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,10 @@ impl AsyncDrawing {
188188
self.client.debug_drawing().await
189189
}
190190
}
191+
192+
#[cfg(docs_images)]
193+
impl turtle_docs_helpers::SaveDocsSvg for AsyncDrawing {
194+
fn save_docs_svg(&self, path: std::path::PathBuf) {
195+
self.client.save_docs_svg(path);
196+
}
197+
}

src/async_turtle.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,10 @@ impl AsyncTurtle {
324324
self.client.debug_turtle(self.id, self.angle_unit).await
325325
}
326326
}
327+
328+
#[cfg(docs_images)]
329+
impl turtle_docs_helpers::SaveDocsSvg for AsyncTurtle {
330+
fn save_docs_svg(&self, path: std::path::PathBuf) {
331+
self.client.save_docs_svg(path);
332+
}
333+
}

src/drawing.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,13 @@ impl Drawing {
629629
}
630630
}
631631

632+
#[cfg(docs_images)]
633+
impl turtle_docs_helpers::SaveDocsSvg for Drawing {
634+
fn save_docs_svg(&self, path: std::path::PathBuf) {
635+
self.drawing.save_docs_svg(path);
636+
}
637+
}
638+
632639
#[cfg(test)]
633640
mod tests {
634641
use super::*;

src/ipc_protocol/protocol.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,12 @@ impl ProtocolClient {
408408
}
409409
}
410410
}
411+
412+
#[cfg(docs_images)]
413+
impl turtle_docs_helpers::SaveDocsSvg for ProtocolClient {
414+
fn save_docs_svg(&self, path: std::path::PathBuf) {
415+
use crate::sync_runtime::block_on;
416+
block_on(self.export_svg(path))
417+
.expect("unable to save docs image");
418+
}
419+
}

src/turtle.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ impl Turtle {
535535
///
536536
/// # Example
537537
///
538-
/// ```rust,no_run
538+
/// ```rust
539539
/// use turtle::Turtle;
540540
///
541541
/// fn main() {
@@ -557,6 +557,7 @@ impl Turtle {
557557
/// turtle.set_pen_color("#4CAF50"); // green
558558
/// turtle.set_pen_size(100.0);
559559
/// turtle.forward(200.0);
560+
/// # #[cfg(docs_images)] turtle_docs_helpers::save_docs_image(&turtle, "pen_thickness");
560561
/// }
561562
/// ```
562563
///
@@ -891,6 +892,13 @@ impl Turtle {
891892
}
892893
}
893894

895+
#[cfg(docs_images)]
896+
impl turtle_docs_helpers::SaveDocsSvg for Turtle {
897+
fn save_docs_svg(&self, path: std::path::PathBuf) {
898+
self.turtle.save_docs_svg(path);
899+
}
900+
}
901+
894902
#[cfg(test)]
895903
mod tests {
896904
use super::*;

turtle_docs_helpers/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "turtle_docs_helpers"
3+
version = "0.1.0"
4+
authors = ["Sunjay Varma <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]

turtle_docs_helpers/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::path::{Path, PathBuf};
2+
3+
/// Saves the image being drawn as an SVG and panics if an error occurs
4+
///
5+
/// This is different from the `save_svg` method on `Drawing` and `AsyncDrawing`
6+
/// because this is only meant to be used for automation and may need to access
7+
/// internal APIs.
8+
pub trait SaveDocsSvg {
9+
fn save_docs_svg(&self, path: PathBuf);
10+
}
11+
12+
/// Saves the currently drawn image to `docs/assets/images/docs/{output_name}`
13+
pub fn save_docs_image<T: SaveDocsSvg>(drawing: &T, output_name: &str) {
14+
let svg_path = Path::new("docs/assets/images/docs").join(output_name).with_extension("svg");
15+
drawing.save_docs_svg(svg_path);
16+
todo!()
17+
}

0 commit comments

Comments
 (0)