Created
December 27, 2020 19:14
-
-
Save yoshuawuyts/7316cc0aa7333848b0b4c1bfe566b7da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INPUT: &str = r#" o Relation Name: alternate | |
o Description: Designates a substitute for the link's context. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: appendix | |
o Description: Refers to an appendix. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: bookmark | |
o Description: Refers to a bookmark or entry point. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: chapter | |
o Description: Refers to a chapter in a collection of resources. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: contents | |
o Description: Refers to a table of contents. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: copyright | |
o Description: Refers to a copyright statement that applies to the | |
link's context. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: current | |
o Description: Refers to a resource containing the most recent | |
item(s) in a collection of resources. | |
o Reference: [RFC5005] | |
o Relation Name: describedby | |
o Description: Refers to a resource providing information about the | |
link's context. | |
o Documentation: <http://www.w3.org/TR/powder-dr/#assoc-linking> | |
o Relation Name: edit | |
o Description: Refers to a resource that can be used to edit the | |
link's context. | |
o Reference: [RFC5023] | |
o Relation Name: edit-media | |
o Description: Refers to a resource that can be used to edit media | |
associated with the link's context. | |
o Reference: [RFC5023] | |
o Relation Name: enclosure | |
o Description: Identifies a related resource that is potentially | |
large and might require special handling. | |
o Reference: [RFC4287] | |
o Relation Name: first | |
o Description: An IRI that refers to the furthest preceding resource | |
in a series of resources. | |
o Reference: [RFC5988] | |
o Notes: this relation type registration did not indicate a | |
reference. Originally requested by Mark Nottingham in December | |
2004. | |
o Relation Name: glossary | |
o Description: Refers to a glossary of terms. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: help | |
o Description: Refers to a resource offering help (more information, | |
links to other sources information, etc.) | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: hub | |
o Description: Refers to a hub that enables registration for | |
notification of updates to the context. | |
o Reference: <http://pubsubhubbub.googlecode.com/> <http:// | |
pubsubhubbub.googlecode.com/svn/trunk/pubsubhubbub-core-0.3.html> | |
o Notes: this relation type was requested by Brett Slatkin. | |
o Relation Name: index | |
o Description: Refers to an index. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: last | |
o Description: An IRI that refers to the furthest following resource | |
in a series of resources. | |
o Reference: [RFC5988] | |
o Notes: this relation type registration did not indicate a | |
reference. Originally requested by Mark Nottingham in December | |
2004. | |
o Relation Name: latest-version | |
o Description: Points to a resource containing the latest (e.g., | |
current) version of the context. | |
o Reference: [RFC5829] | |
o Relation Name: license | |
o Description: Refers to a license associated with the link's | |
context. | |
o Reference: [RFC4946] | |
o Relation Name: next | |
o Description: Refers to the next resource in a ordered series of | |
resources. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: next-archive | |
o Description: Refers to the immediately following archive resource. | |
o Reference: [RFC5005] | |
o Relation Name: payment | |
o Description: indicates a resource where payment is accepted. | |
o Reference: [RFC5988] | |
o Notes: this relation type registration did not indicate a | |
reference. Requested by Joshua Kinberg and Robert Sayre. It is | |
meant as a general way to facilitate acts of payment, and thus | |
this specification makes no assumptions on the type of payment or | |
transaction protocol. Examples may include a Web page where | |
donations are accepted or where goods and services are available | |
for purchase. rel="payment" is not intended to initiate an | |
automated transaction. In Atom documents, a link element with a | |
rel="payment" attribute may exist at the feed/channel level and/or | |
the entry/item level. For example, a rel="payment" link at the | |
feed/channel level may point to a "tip jar" URI, whereas an entry/ | |
item containing a book review may include a rel="payment" link | |
that points to the location where the book may be purchased | |
through an online retailer. | |
o Relation Name: prev | |
o Description: Refers to the previous resource in an ordered series | |
of resources. Synonym for "previous". | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: predecessor-version | |
o Description: Points to a resource containing the predecessor | |
version in the version history. | |
o Reference: [RFC5829] | |
o Relation Name: previous | |
o Description: Refers to the previous resource in an ordered series | |
of resources. Synonym for "prev". | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: prev-archive | |
o Description: Refers to the immediately preceding archive resource. | |
o Reference: [RFC5005] | |
o Relation Name: related | |
o Description: Identifies a related resource. | |
o Reference: [RFC4287] | |
o Relation Name: replies | |
o Description: Identifies a resource that is a reply to the context | |
of the link. | |
o Reference: [RFC4685] | |
o Relation Name: section | |
o Description: Refers to a section in a collection of resources. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: self | |
o Description: Conveys an identifier for the link's context. | |
o Reference: [RFC4287] | |
o Relation Name: service | |
o Description: Indicates a URI that can be used to retrieve a | |
service document. | |
o Reference: [RFC5023] | |
o Notes: When used in an Atom document, this relation type specifies | |
Atom Publishing Protocol service documents by default. Requested | |
by James Snell. | |
o Relation Name: start | |
o Description: Refers to the first resource in a collection of | |
resources. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: stylesheet | |
o Description: Refers to an external style sheet. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: subsection | |
o Description: Refers to a resource serving as a subsection in a | |
collection of resources. | |
o Reference: [W3C.REC-html401-19991224] | |
o Relation Name: successor-version | |
o Description: Points to a resource containing the successor version | |
in the version history. | |
o Reference: [RFC5829] | |
o Relation Name: up | |
o Description: Refers to a parent document in a hierarchy of | |
documents. | |
o Reference: [RFC5988] | |
o Notes: this relation type registration did not indicate a | |
reference. Requested by Noah Slater. | |
o Relation Name: version-history | |
o Description: points to a resource containing the version history | |
for the context. | |
o Reference: [RFC5829] | |
o Relation Name: via | |
o Description: Identifies a resource that is the source of the | |
information in the link's context. | |
o Reference: [RFC4287] | |
o Relation Name: working-copy | |
o Description: Points to a working copy for this resource. | |
o Reference: [RFC5829] | |
o Relation Name: working-copy-of | |
o Description: Points to the versioned resource from which this | |
working copy was obtained. | |
o Reference: [RFC5829] | |
"#; | |
fn main() { | |
let mut names: Vec<(String, String)> = vec![]; | |
println!("#[non_exhaustive]\npub enum RelationType {{"); | |
for group in INPUT.split("\n\n") { | |
// parse input data | |
let lines: Vec<String> = group.split(" o ").map(|l| l.trim().to_owned()).collect(); | |
let mut lines = lines.iter().skip(1); | |
let relation_name = lines.next().unwrap().replace("Relation Name: ", "").replace("\n ", "\n /// "); | |
let description = lines.next().unwrap().replace("Description: ", "").replace("\n ", "\n /// "); | |
let reference = lines.next().unwrap().replace("Reference: ", ""); | |
// prep values | |
let notes = lines.next().map(|notes| { | |
let notes = notes.replace("Notes:", ""); | |
let mut out = String::new(); | |
notes.lines().for_each(|line| { | |
out.push_str(&format!("\n /// {}", line.trim())); | |
}); | |
out | |
}); | |
let mut reference = reference.replace("[", "").replace("]", ""); | |
if reference.starts_with("W3C") { | |
reference = format!("rfc5988#ref-{}", reference); | |
} | |
let type_name = kebab(&relation_name); | |
names.push((relation_name.into(), type_name.clone().into())); | |
// print struct | |
println!(" /// {}", description.trim()); | |
if let Some(notes) = notes { | |
println!(" ///\n /// # Notes\n ///{}", notes); | |
} | |
println!(" ///\n /// # References\n ///"); | |
println!(" /// - [{}](https://tools.ietf.org/html/{})", reference, reference); | |
println!(" {},", type_name); | |
println!(""); | |
} | |
println!("}}\n"); | |
// Print Display | |
println!("impl Display for RelationType {{\n fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {{"); | |
println!(" match self {{"); | |
for (rel_name, ty_name) in &names { | |
println!(r#" Self::{} => write!(f, "{}"),"#, ty_name, rel_name); | |
} | |
println!(" }}"); | |
println!(" }}"); | |
println!("}}"); | |
// Print FromStr | |
println!("impl FromStr for RelationType {{\n type Err = crate::Error;\n\n fn from_str(s: &str) -> Result<Self, Self::Err> {{"); | |
println!(" match s {{"); | |
for (rel_name, ty_name) in names { | |
println!(r#" "{}" => Ok(Self::{}),"#, rel_name, ty_name); | |
} | |
println!(r#" s => bail!(400, "{{}} is not a recognized relation type" , s),"#); | |
println!(" }}"); | |
println!(" }}"); | |
println!("}}"); | |
} | |
fn kebab(s: &str) -> String { | |
let mut out = String::new(); | |
for s in s.trim().split('-') { | |
let mut chars = s.chars(); | |
let first = chars.next().unwrap().to_uppercase().to_string(); | |
out.push_str(first.as_str()); | |
chars.for_each(|c| out.push(c)); | |
} | |
out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment