Skip to content

Commit 4b80f2d

Browse files
author
bors-servo
authored
Auto merge of #199 - derekdreery:macro_doc, r=jdm
Allow the generated macros to have documentation This PR provides functionality to add documentation to the macros created using `string_cache_codegen`. It also copies the example from the README to the crate root docs. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/string-cache/199) <!-- Reviewable:end -->
2 parents b5577a7 + 8b33173 commit 4b80f2d

File tree

1 file changed

+75
-1
lines changed

1 file changed

+75
-1
lines changed

string-cache-codegen/lib.rs

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,65 @@
66
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
77
// option. This file may not be copied, modified, or distributed
88
// except according to those terms.
9+
//! A crate to create static string caches at compiletime.
10+
//!
11+
//! # Examples
12+
//!
13+
//! With static atoms:
14+
//!
15+
//! In `Cargo.toml`:
16+
//!
17+
//! ```toml
18+
//! [package]
19+
//! build = "build.rs"
20+
//!
21+
//! [dependencies]
22+
//! string_cache = "0.7"
23+
//!
24+
//! [build-dependencies]
25+
//! string_cache_codegen = "0.4"
26+
//! ```
27+
//!
28+
//! In `build.rs`:
29+
//!
30+
//! ```no_run
31+
//! extern crate string_cache_codegen;
32+
//!
33+
//! use std::env;
34+
//! use std::path::Path;
35+
//!
36+
//! fn main() {
37+
//! string_cache_codegen::AtomType::new("foo::FooAtom", "foo_atom!")
38+
//! .atoms(&["foo", "bar"])
39+
//! .write_to_file(&Path::new(&env::var("OUT_DIR").unwrap()).join("foo_atom.rs"))
40+
//! .unwrap()
41+
//! }
42+
//! ```
43+
//!
44+
//! In `lib.rs`:
45+
//!
46+
//! ```ignore
47+
//! extern crate string_cache;
48+
//!
49+
//! mod foo {
50+
//! include!(concat!(env!("OUT_DIR"), "/foo_atom.rs"));
51+
//! }
52+
//! ```
53+
//!
54+
//! The generated code will define a `FooAtom` type and a `foo_atom!` macro.
55+
//! The macro can be used in expression or patterns, with strings listed in `build.rs`.
56+
//! For example:
57+
//!
58+
//! ```ignore
59+
//! fn compute_something(input: &foo::FooAtom) -> u32 {
60+
//! match *input {
61+
//! foo_atom!("foo") => 1,
62+
//! foo_atom!("bar") => 2,
63+
//! _ => 3,
64+
//! }
65+
//! }
66+
//! ```
67+
//!
968
1069
#![recursion_limit = "128"]
1170

@@ -24,6 +83,7 @@ use std::path::Path;
2483
pub struct AtomType {
2584
path: String,
2685
macro_name: String,
86+
macro_doc: Option<String>,
2787
atoms: HashSet<String>,
2888
}
2989

@@ -38,7 +98,7 @@ impl AtomType {
3898
///
3999
/// For example, `AtomType::new("foo::FooAtom", "foo_atom!")` will generate:
40100
///
41-
/// ```rust
101+
/// ```ignore
42102
/// pub type FooAtom = ::string_cache::Atom<FooAtomStaticSet>;
43103
/// pub struct FooAtomStaticSet;
44104
/// impl ::string_cache::StaticAtomSet for FooAtomStaticSet {
@@ -53,10 +113,19 @@ impl AtomType {
53113
AtomType {
54114
path: path.to_owned(),
55115
macro_name: macro_name[..macro_name.len() - "!".len()].to_owned(),
116+
macro_doc: None,
56117
atoms: HashSet::new(),
57118
}
58119
}
59120

121+
/// Add some documentation to the generated macro.
122+
///
123+
/// Note that `docs` should not contain the `///` at the front of normal docs.
124+
pub fn with_macro_doc(&mut self, docs: &str) -> &mut Self {
125+
self.macro_doc = Some(docs.to_owned());
126+
self
127+
}
128+
60129
/// Adds an atom to the builder
61130
pub fn atom(&mut self, s: &str) -> &mut Self {
62131
self.atoms.insert(s.to_owned());
@@ -107,6 +176,10 @@ impl AtomType {
107176
} else {
108177
&self.path
109178
};
179+
let macro_doc = match self.macro_doc {
180+
Some(ref doc) => quote!(#[doc = #doc]),
181+
None => quote!()
182+
};
110183
let static_set_name = quote::Ident::from(format!("{}StaticSet", type_name));
111184
let type_name = quote::Ident::from(type_name);
112185
let macro_name = quote::Ident::from(&*self.macro_name);
@@ -129,6 +202,7 @@ impl AtomType {
129202
#empty_string_index
130203
}
131204
}
205+
#macro_doc
132206
#[macro_export]
133207
macro_rules! #macro_name {
134208
#(

0 commit comments

Comments
 (0)