noosphere_core/data/headers/
header.rs

1use std::{convert::Infallible, fmt::Display, ops::Deref, str::FromStr};
2
3/// Well-known headers in the Noosphere
4pub enum Header {
5    /// The content length (in bytes) of associated binary data
6    ContentLength,
7    /// Content-type, for mimes
8    ContentType,
9    /// A proof, typically a UCAN JWT
10    Proof,
11    /// The author's DID
12    Author,
13    /// A title for the associated content body
14    Title,
15    /// A signature by the author's key
16    Signature,
17    /// The Noosphere protocol version
18    Version,
19    /// A file extension to use when rendering the content to
20    /// the file system
21    FileExtension,
22    /// The logical order relative to any ancestors
23    LamportOrder,
24    /// All others
25    Unknown(String),
26}
27
28impl Display for Header {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        write!(f, "{}", Deref::deref(self))
31    }
32}
33
34impl FromStr for Header {
35    type Err = Infallible;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        Ok(match s.to_lowercase().as_str() {
39            "content-length" => Header::ContentLength,
40            "content-type" => Header::ContentType,
41            "file-extension" => Header::FileExtension,
42            "proof" => Header::Proof,
43            "author" => Header::Author,
44            "title" => Header::Title,
45            "signature" => Header::Signature,
46            "version" => Header::Version,
47            "lamport-order" => Header::LamportOrder,
48            _ => Header::Unknown(s.to_string()),
49        })
50    }
51}
52
53impl Deref for Header {
54    type Target = str;
55
56    fn deref(&self) -> &Self::Target {
57        match self {
58            Header::ContentLength => "Content-Length",
59            Header::ContentType => "Content-Type",
60            Header::Proof => "Proof",
61            Header::Author => "Author",
62            Header::Title => "Title",
63            Header::Signature => "Signature",
64            Header::Version => "Version",
65            Header::FileExtension => "File-Extension",
66            Header::LamportOrder => "Lamport-Order",
67            Header::Unknown(name) => name.as_str(),
68        }
69    }
70}