| 1 | //! A module implementing [`Signature`] type that holds info about variants of the signature key used by the repository |
| 2 | |
| 3 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#; |
| 4 | |
| 5 | use craterust-analyzer cargo apt-sources 0.3.0 crate/::errorrust-analyzer cargo apt-sources 0.3.0 error/::RepositoryErrorrust-analyzer cargo apt-sources 0.3.0 error/RepositoryError#; |
| 6 | |
| 7 | /// A type to store variants of cryptographic public key to validate the [`super::Repository`]. |
| 8 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 9 | pub enum Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature# { |
| 10 | /// The PGP key is stored inside the `.sources` files |
| 11 | KeyBlockrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyBlock#(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#), // TODO: shall we validate PGP Public Key? |
| 12 | /// The public key is store in a file of the given path |
| 13 | KeyPathrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyPath#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#), // TODO: man page specifies fingerprints, but there's no example |
| 14 | } |
| 15 | |
| 16 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/::FromStrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr# for Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature# { |
| 17 | type Errrust-analyzer cargo apt-sources 0.3.0 signature/impl#[Signature][FromStr]Err# = RepositoryErrorrust-analyzer cargo apt-sources 0.3.0 error/RepositoryError#; |
| 18 | |
| 19 | fn from_strrust-analyzer cargo apt-sources 0.3.0 signature/impl#[Signature][FromStr]from_str().(text: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo apt-sources 0.3.0 signature/impl#[Signature][FromStr], Selfrust-analyzer cargo apt-sources 0.3.0 signature/impl#[Signature][FromStr]::Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/traits/FromStr#Err#> { |
| 20 | // Normal examples say PGP line shall start next line after `Signed-By` field |
| 21 | // but all my files have it starting after a space in the same line and that works. |
| 22 | // It's quite confusing, but let it be... we have to deal with reality. |
| 23 | if text.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().("\n") { |
| 24 | // If text is multiline, we assume PGP Public Key block |
| 25 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyBlockrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyBlock#(text.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().())) |
| 26 | } else { |
| 27 | // otherwise one-liner is a path |
| 28 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyPathrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyPath#(text.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().())) |
| 29 | } |
| 30 | |
| 31 | // if let Some((name, rest)) = text.split_once('\n') { |
| 32 | // if name.is_empty() { |
| 33 | // println!("& Name = {}", name); |
| 34 | // Ok(Signature::KeyBlock(rest.to_string())) |
| 35 | // } else { |
| 36 | // println!("& Name = {}", name); |
| 37 | // Err(RepositoryError::InvalidSignature) |
| 38 | // } |
| 39 | // } else { |
| 40 | // println!("& No name"); |
| 41 | // Ok(Signature::KeyPath(text.into())) |
| 42 | // } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature# { |
| 47 | fn fmtrust-analyzer cargo apt-sources 0.3.0 signature/impl#[Signature][Display]fmt().(&self, f: &mut stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 48 | match self { |
| 49 | Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyBlockrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyBlock#(text) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "\n{text}"), |
| 50 | Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyPathrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyPath#(path) => f.write_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/impl#[`Formatter<'a>`]write_str().(path.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().().as_refrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/impl#[`Cow<'_, T>`][`AsRef<T>`]as_ref().()), |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | #[cfg(test)] |
| 56 | mod testsrust-analyzer cargo apt-sources 0.3.0 signature/tests/ { |
| 57 | use superrust-analyzer cargo apt-sources 0.3.0 signature/::*; |
| 58 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#; |
| 59 | |
| 60 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 61 | fn test_signature_displayrust-analyzer cargo apt-sources 0.3.0 signature/tests/test_signature_display().() { |
| 62 | // Test KeyPath display |
| 63 | let path_sig = Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyPathrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyPath#(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[PathBuf][`From<&T>`]from().("/etc/apt/trusted.gpg")); |
| 64 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(path_sig.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), "/etc/apt/trusted.gpg"); |
| 65 | |
| 66 | // Test KeyBlock display |
| 67 | let key_block = |
| 68 | "-----BEGIN PGP PUBLIC KEY BLOCK-----\ntest key\n-----END PGP PUBLIC KEY BLOCK-----"; |
| 69 | let block_sig = Signaturerust-analyzer cargo apt-sources 0.3.0 signature/Signature#::KeyBlockrust-analyzer cargo apt-sources 0.3.0 signature/Signature#KeyBlock#(key_block.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()); |
| 70 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(block_sig.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(), formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("\n{}", key_block)); |
| 71 | } |
| 72 | } |