···52525353### Examples
54545555-Examples can be found within the
5555+Thoroughly commented examples can be found within the
5656[`examples/`](https://github.com/gemrest/germ/tree/main/examples) directory.
57575858## License
+10-1
examples/ast.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for parsing Gemtext into an
2020+//! abstract syntax tree.
2121+1922const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text
2023Here goes the pre-formatted text.
2124···4649That was a link without text."#;
47504851fn main() {
4949- for node in germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).inner() {
5252+ // Parse `EXAMPLE_GEMTEXT` into an abstract syntax tree
5353+ let ast = germ::ast::Ast::from_string(EXAMPLE_GEMTEXT);
5454+ // Get the nodes of the abstract syntax tree
5555+ let ast_nodes = ast.inner();
5656+5757+ // Print the abstract syntax tree nodes, one-by-one
5858+ for node in ast_nodes {
5059 println!("{:?}", node);
5160 }
5261}
+10-1
examples/ast_to_gemtext.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example converts Gemtext into an abstract syntax tree and then back
2020+//! into Gemtext, demonstrating both Germ's parsing and generation capabilities.
2121+1922const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text
2023Here goes the pre-formatted text.
2124···4649That was a link without text."#;
47504851fn main() {
4949- println!("{}", germ::ast::Ast::from_string(EXAMPLE_GEMTEXT).to_gemtext());
5252+ // Parse `EXAMPLE_GEMTEXT` into an abstract syntax tree
5353+ let ast = germ::ast::Ast::from_string(EXAMPLE_GEMTEXT);
5454+ // Convert the abstract syntax tree back to Gemtext
5555+ let gemtext = ast.to_gemtext();
5656+5757+ // Print the Gemtext, identical to `EXAMPLE_GEMTEXT`
5858+ println!("{gemtext}");
5059}
+10-5
examples/html.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for converting Gemtext to
2020+//! HTML.
2121+1922const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text
2023Here goes the pre-formatted text.
2124···4649That was a link without text."#;
47504851fn main() {
4949- std::fs::write(
5050- "examples/convert.html",
5151- germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML),
5252- )
5353- .expect("could not write to file");
5252+ // Convert the Gemtext to HTML
5353+ let html =
5454+ germ::convert::from_string(EXAMPLE_GEMTEXT, &germ::convert::Target::HTML);
5555+5656+ // Write the HTML to a file
5757+ std::fs::write("examples/convert.html", html)
5858+ .expect("could not write to file");
5459}
+11-8
examples/markdown.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for converting Gemtext to
2020+//! Markdown.
2121+1922const EXAMPLE_GEMTEXT: &str = r#"```This is alt-text
2023Here goes the pre-formatted text.
2124···4649That was a link without text."#;
47504851fn main() {
4949- std::fs::write(
5050- "examples/convert.md",
5151- germ::convert::from_string(
5252- EXAMPLE_GEMTEXT,
5353- &germ::convert::Target::Markdown,
5454- ),
5555- )
5656- .expect("could not write to file");
5252+ // Convert the Gemtext to Markdown
5353+ let html = germ::convert::from_string(
5454+ EXAMPLE_GEMTEXT,
5555+ &germ::convert::Target::Markdown,
5656+ );
5757+5858+ // Write the Markdown to a file
5959+ std::fs::write("examples/convert.md", html).expect("could not write to file");
5760}
+18-4
examples/meta.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for parsing Gemini meta
2020+//! sections.
2121+1922fn main() {
2020- println!(
2121- "{:?}",
2222- germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2")
2323- )
2323+ // Parse Gemini meta section into a structured meta representation
2424+ let meta = germ::meta::Meta::from_string("text/gemini; hi=2; hi2=string=2");
2525+2626+ // Debug view of the structured meta representation
2727+ println!("{:?}", meta);
2828+2929+ // Convert the structured meta representation back to a string, identical to
3030+ // the original meta section
3131+ println!("{}", meta.to_string());
3232+3333+ // The MIME type of the meta section
3434+ println!("{}", meta.mime());
3535+3636+ // A debug view of the parameters of the meta section
3737+ println!("{:?}", meta.parameters());
2438}
+29-4
examples/request.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for performing a non-blocking
2020+//! request to a Gemini capsule.
2121+1922#[tokio::main]
2023async fn main() {
2121- match germ::request::request(&url::Url::parse("gemini://fuwn.me").unwrap())
2222- .await
2323- {
2424- Ok(response) => println!("{:?}", response),
2424+ // Form a valid URL to a Gemini capsule
2525+ let url = url::Url::parse("gemini://fuwn.me").unwrap();
2626+ // Perform a non-blocking request to the Gemini capsule
2727+ let request = germ::request::request(&url).await;
2828+2929+ match request {
3030+ // If the request was successful, print a debug view of the response
3131+ Ok(response) => {
3232+ // Print the status of the response
3333+ println!("{:?}", response.status());
3434+3535+ // Print the meta string of the response
3636+ //
3737+ // More detailed meta usage can be found in the `meta` example
3838+ println!("{}", response.meta());
3939+4040+ // Print the content of the response, if present
4141+ println!("{:?}", response.content());
4242+4343+ // Print the size of the response
4444+ println!("{:?}", response.size());
4545+4646+ // Print a debug view of the SSL suite used
4747+ println!("{:?}", response.suite());
4848+ }
4949+ // If the request was unsuccessful, do nothing
2550 Err(_) => {}
2651 }
2752}
+29-4
examples/request_blocking.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates Germ's capabilities for performing a blocking
2020+//! request to a Gemini capsule.
2121+1922fn main() {
2020- match germ::request::blocking::request(
2121- &url::Url::parse("gemini://fuwn.me").unwrap(),
2222- ) {
2323- Ok(response) => println!("{:?}", response),
2323+ // Form a valid URL to a Gemini capsule
2424+ let url = url::Url::parse("gemini://fuwn.me").unwrap();
2525+ // Perform a blocking request to the Gemini capsule
2626+ let request = germ::request::blocking::request(&url);
2727+2828+ match request {
2929+ // If the request was successful, print a debug view of the response
3030+ Ok(response) => {
3131+ // Print the status of the response
3232+ println!("{:?}", response.status());
3333+3434+ // Print the meta string of the response
3535+ //
3636+ // More detailed meta usage can be found in the `meta` example
3737+ println!("{}", response.meta());
3838+3939+ // Print the content of the response, if present
4040+ println!("{:?}", response.content());
4141+4242+ // Print the size of the response
4343+ println!("{:?}", response.size());
4444+4545+ // Print a debug view of the SSL suite used
4646+ println!("{:?}", response.suite());
4747+ }
4848+ // If the request was unsuccessful, do nothing
2449 Err(_) => {}
2550 }
2651}
+27-10
examples/request_blocking_to_gemtext_from_ast.rs
···1616// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
1717// SPDX-License-Identifier: GPL-3.0-only
18181919+//! This example demonstrates a chain of Germ's capabilities by fetching a
2020+//! Gemini capsule, parsing the response content into an abstract syntax tree,
2121+//! and converting the abstract syntax tree back to Gemtext, identical to the
2222+//! Gemini response content.
2323+1924fn main() {
2020- match germ::request::blocking::request(
2121- &url::Url::parse("gemini://fuwn.me/").unwrap(),
2222- ) {
2323- Ok(response) => println!(
2424- "{}",
2525- germ::ast::Ast::from_string(
2626- &*response.content().clone().unwrap_or_else(|| "".to_string())
2727- )
2828- .to_gemtext()
2929- ),
2525+ // Form a valid URL to a Gemini capsule
2626+ let url = url::Url::parse("gemini://fuwn.me/").unwrap();
2727+ // Perform a blocking request to the Gemini capsule
2828+ let request = germ::request::blocking::request(&url);
2929+3030+ match request {
3131+ // If the request was successful:
3232+ Ok(response) => {
3333+ // Obtain the content of the Gemini response
3434+ let response_content =
3535+ &*response.content().clone().unwrap_or_else(|| "".to_string());
3636+ // Parse the Gemini response content into an abstract syntax tree
3737+ let ast = germ::ast::Ast::from_string(response_content);
3838+ // Convert the abstract syntax tree back to Gemtext, identical to the
3939+ // Gemini response content, constructed from the parsed abstract syntax
4040+ // tree
4141+ let gemtext = ast.to_gemtext();
4242+4343+ // Print the Gemtext
4444+ println!("{}", gemtext)
4545+ }
4646+ // If the request was unsuccessful, do nothing
3047 Err(_) => {}
3148 }
3249}
+3-7
src/request.rs
···2222mod status;
2323mod verifier;
24242525-#[cfg(feature = "blocking")]
2626-pub mod blocking;
2727-2828-#[cfg(feature = "request")]
2929-pub mod non_blocking;
2525+#[cfg(feature = "blocking")] pub mod blocking;
30263131-#[cfg(feature = "request")]
3232-pub use non_blocking::request;
2727+#[cfg(feature = "request")] pub mod non_blocking;
33282929+#[cfg(feature = "request")] pub use non_blocking::request;
3430pub(crate) use verifier::GermVerifier;
3531pub use {response::Response, status::Status};