apt_sources/signature.rs
1//! A module implementing `Signature` type that holds info about variants of the signature key used by the repository
2
3use std::path::PathBuf;
4
5use crate::error::RepositoryError;
6
7/// A type to store
8#[derive(Debug, PartialEq, Clone)]
9pub enum Signature {
10 /// The PGP key is stored inside the `.sources` files
11 KeyBlock(String), // TODO: shall we validate PGP Public Key?
12 /// The public key is store in a file of the given path
13 KeyPath(PathBuf)
14 // TODO: man page specifies fingerprints, but there's no example
15}
16
17impl std::str::FromStr for Signature {
18 type Err = RepositoryError;
19
20 fn from_str(text: &str) -> Result<Self, Self::Err> {
21 // Normal examples say PGP line shall start next line after `Signed-By` field
22 // but all my files have it starting after a space in the same line and that works.
23 // It's quite confusing, but let it be... we have to deal with reality.
24 if text.contains("\n") {
25 // If text is multiline, we assume PGP Public Key block
26 Ok(Signature::KeyBlock(text.to_string()))
27 } else {
28 // otherwise one-liner is a path
29 Ok(Signature::KeyPath(text.into()))
30 }
31
32 // if let Some((name, rest)) = text.split_once('\n') {
33 // if name.is_empty() {
34 // println!("& Name = {}", name);
35 // Ok(Signature::KeyBlock(rest.to_string()))
36 // } else {
37 // println!("& Name = {}", name);
38 // Err(RepositoryError::InvalidSignature)
39 // }
40 // } else {
41 // println!("& No name");
42 // Ok(Signature::KeyPath(text.into()))
43 // }
44 }
45}
46
47impl std::fmt::Display for Signature {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 match self {
50 Signature::KeyBlock(text) => write!(f, "\n{}", text),
51 Signature::KeyPath(path) => f.write_str(path.to_string_lossy().as_ref()),
52 }
53 }
54}