| 1 | use std::borrow::Cow; |
| 2 | use std::fmt::{self, Display}; |
| 3 | use std::str::FromStr; |
| 4 | |
| 5 | use anyhow::{ensure, Error, Result}; |
| 6 | use serde::Serialize; |
| 7 | use wasm_encoder::{ComponentSection, CustomSection, Encode, Section}; |
| 8 | use wasmparser::CustomSectionReader; |
| 9 | |
| 10 | /// Source control revision identifier for the packaged software. |
| 11 | #[derive (Debug, Clone, PartialEq)] |
| 12 | pub struct Revision(CustomSection<'static>); |
| 13 | |
| 14 | impl Revision { |
| 15 | /// Create a new instance of `Desrciption`. |
| 16 | pub fn new<S: Into<Cow<'static, str>>>(s: S) -> Self { |
| 17 | Self(CustomSection { |
| 18 | name: "revision" .into(), |
| 19 | data: match s.into() { |
| 20 | Cow::Borrowed(s: &str) => Cow::Borrowed(s.as_bytes()), |
| 21 | Cow::Owned(s: String) => Cow::Owned(s.into()), |
| 22 | }, |
| 23 | }) |
| 24 | } |
| 25 | |
| 26 | /// Parse an `revision` custom section from a wasm binary. |
| 27 | pub(crate) fn parse_custom_section(reader: &CustomSectionReader<'_>) -> Result<Self> { |
| 28 | ensure!( |
| 29 | reader.name() == "revision" , |
| 30 | "The `revision` custom section should have a name of 'revision'" |
| 31 | ); |
| 32 | let data: String = String::from_utf8(vec:reader.data().to_owned())?; |
| 33 | Ok(Self::new(data)) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl FromStr for Revision { |
| 38 | type Err = Error; |
| 39 | |
| 40 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 41 | Ok(Self::new(s.to_owned())) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl Serialize for Revision { |
| 46 | fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error> |
| 47 | where |
| 48 | S: serde::Serializer, |
| 49 | { |
| 50 | serializer.serialize_str(&self.to_string()) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | impl Display for Revision { |
| 55 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 56 | // NOTE: this will never panic since we always guarantee the data is |
| 57 | // encoded as utf8, even if we internally store it as [u8]. |
| 58 | let data: String = String::from_utf8(self.0.data.to_vec()).unwrap(); |
| 59 | write!(f, " {data}" ) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl ComponentSection for Revision { |
| 64 | fn id(&self) -> u8 { |
| 65 | ComponentSection::id(&self.0) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | impl Section for Revision { |
| 70 | fn id(&self) -> u8 { |
| 71 | Section::id(&self.0) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Encode for Revision { |
| 76 | fn encode(&self, sink: &mut Vec<u8>) { |
| 77 | self.0.encode(sink); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg (test)] |
| 82 | mod test { |
| 83 | use super::*; |
| 84 | use wasm_encoder::Component; |
| 85 | use wasmparser::Payload; |
| 86 | |
| 87 | #[test ] |
| 88 | fn roundtrip() { |
| 89 | let mut component = Component::new(); |
| 90 | component.section(&Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad" )); |
| 91 | let component = component.finish(); |
| 92 | |
| 93 | let mut parsed = false; |
| 94 | for section in wasmparser::Parser::new(0).parse_all(&component) { |
| 95 | if let Payload::CustomSection(reader) = section.unwrap() { |
| 96 | let revision = Revision::parse_custom_section(&reader).unwrap(); |
| 97 | assert_eq!( |
| 98 | revision.to_string(), |
| 99 | "de978e17a80c1118f606fce919ba9b7d5a04a5ad" |
| 100 | ); |
| 101 | parsed = true; |
| 102 | } |
| 103 | } |
| 104 | assert!(parsed); |
| 105 | } |
| 106 | |
| 107 | #[test ] |
| 108 | fn serialize() { |
| 109 | let revision = Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad" ); |
| 110 | let json = serde_json::to_string(&revision).unwrap(); |
| 111 | assert_eq!(r#""de978e17a80c1118f606fce919ba9b7d5a04a5ad""# , json); |
| 112 | } |
| 113 | } |
| 114 | |