symbol.rs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use std::fmt::{self, Display};
  2. use syn::{Ident, Path};
  3. #[derive(Copy, Clone)]
  4. pub struct Symbol(&'static str);
  5. pub const PB_ATTRS: Symbol = Symbol("pb");
  6. pub const SKIP: Symbol = Symbol("skip"); //#[pb(skip)]
  7. pub const PB_INDEX: Symbol = Symbol("index"); //#[pb(index = "1")]
  8. pub const PB_ONE_OF: Symbol = Symbol("one_of"); //#[pb(one_of)]
  9. pub const DESERIALIZE_WITH: Symbol = Symbol("deserialize_with");
  10. pub const SKIP_DESERIALIZING: Symbol = Symbol("skip_deserializing");
  11. pub const SERIALIZE_WITH: Symbol = Symbol("serialize_with"); //#[pb(serialize_with = "...")]
  12. pub const SKIP_SERIALIZING: Symbol = Symbol("skip_serializing"); //#[pb(skip_serializing)]
  13. pub const PB_STRUCT: Symbol = Symbol("struct"); //#[pb(struct="some struct")]
  14. pub const PB_ENUM: Symbol = Symbol("enum"); //#[pb(enum="some enum")]
  15. pub const EVENT_INPUT: Symbol = Symbol("input");
  16. pub const EVENT_OUTPUT: Symbol = Symbol("output");
  17. pub const EVENT_IGNORE: Symbol = Symbol("ignore");
  18. pub const EVENT: Symbol = Symbol("event");
  19. pub const EVENT_ERR: Symbol = Symbol("event_err");
  20. impl PartialEq<Symbol> for Ident {
  21. fn eq(&self, word: &Symbol) -> bool {
  22. self == word.0
  23. }
  24. }
  25. impl<'a> PartialEq<Symbol> for &'a Ident {
  26. fn eq(&self, word: &Symbol) -> bool {
  27. *self == word.0
  28. }
  29. }
  30. impl PartialEq<Symbol> for Path {
  31. fn eq(&self, word: &Symbol) -> bool {
  32. self.is_ident(word.0)
  33. }
  34. }
  35. impl<'a> PartialEq<Symbol> for &'a Path {
  36. fn eq(&self, word: &Symbol) -> bool {
  37. self.is_ident(word.0)
  38. }
  39. }
  40. impl Display for Symbol {
  41. fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
  42. formatter.write_str(self.0)
  43. }
  44. }