1use std::collections::BTreeMap;
24
25use serde::{Deserialize, Serialize};
26
27use crate::geom::{Point3, Vec3};
28
29pub type Props = BTreeMap<String, Value>;
31
32#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
34#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
35#[non_exhaustive]
36pub struct Document {
37 #[serde(default)]
39 pub schema_version: SchemaVersion,
40 pub source: SourceInfo,
42 pub units: Units,
44 pub layers: Vec<Layer>,
46 pub linetypes: Vec<Linetype>,
48 pub text_styles: Vec<TextStyle>,
50 pub blocks: Vec<Block>,
52 pub models: Vec<Model>,
54 pub props: Props,
56 pub warnings: Vec<Warning>,
58}
59
60impl Document {
61 pub fn new() -> Self {
63 Self::default()
64 }
65
66 pub fn model_space(&self) -> Option<&Model> {
68 self.models
69 .iter()
70 .find(|m| m.kind == ModelKind::Model)
71 .or_else(|| self.models.first())
72 }
73
74 pub fn model_space_mut(&mut self) -> &mut Model {
76 if self.models.is_empty() {
77 self.models.push(Model {
78 name: "Model".to_owned(),
79 kind: ModelKind::Model,
80 ..Model::default()
81 });
82 }
83 let idx = self
84 .models
85 .iter()
86 .position(|m| m.kind == ModelKind::Model)
87 .unwrap_or(0);
88 #[allow(clippy::expect_used)]
89 self.models
90 .get_mut(idx)
91 .expect("idx is in bounds of non-empty models")
92 }
93
94 pub fn layouts(&self) -> impl Iterator<Item = &Model> {
96 self.models.iter().filter(|m| m.kind != ModelKind::Model)
97 }
98
99 pub fn all_entities(&self) -> impl Iterator<Item = &Entity> {
101 self.models.iter().flat_map(|m| m.entities.iter())
102 }
103
104 pub fn entities_on_layer<'a>(
107 &'a self,
108 layer: &'a str,
109 ) -> impl Iterator<Item = &'a Entity> + 'a {
110 self.all_entities()
111 .filter(move |e| e.layer.as_deref().unwrap_or("0") == layer)
112 }
113
114 pub fn layer(&self, name: &str) -> Option<&Layer> {
116 self.layers.iter().find(|l| l.name == name).or_else(|| {
117 self.layers
118 .iter()
119 .find(|l| l.name.eq_ignore_ascii_case(name))
120 })
121 }
122
123 pub fn block(&self, name: &str) -> Option<&Block> {
125 self.blocks.iter().find(|b| b.name == name).or_else(|| {
126 self.blocks
127 .iter()
128 .find(|b| b.name.eq_ignore_ascii_case(name))
129 })
130 }
131
132 pub fn entity_count(&self) -> usize {
134 fn count_slice(slice: &[Entity]) -> usize {
135 let mut total = 0usize;
136 let mut stack: Vec<&[Entity]> = vec![slice];
137 while let Some(items) = stack.pop() {
138 total = total.saturating_add(items.len());
139 for e in items {
140 if let EntityKind::Group { children, .. } = &e.kind
141 && !children.is_empty()
142 {
143 stack.push(children);
144 }
145 }
146 }
147 total
148 }
149 self.models.iter().fold(0usize, |acc, m| {
150 acc.saturating_add(count_slice(&m.entities))
151 })
152 }
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
162#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
163#[serde(transparent)]
164pub struct SchemaVersion(u32);
165
166impl SchemaVersion {
167 pub const CURRENT: Self = Self(1);
169
170 pub const fn get(self) -> u32 {
172 self.0
173 }
174}
175
176impl Default for SchemaVersion {
177 fn default() -> Self {
178 Self::CURRENT
179 }
180}
181
182#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
184#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
185#[non_exhaustive]
186pub struct SourceInfo {
187 pub format: Format,
189 pub version: String,
191 pub application: Option<String>,
193 pub codepage: Option<String>,
195}
196
197#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
199#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
200#[non_exhaustive]
201pub struct Summary {
202 pub format: Format,
204 pub version: String,
206 pub is_3d: Option<bool>,
208 pub units: Option<Units>,
210 pub codepage: Option<String>,
212 pub application: Option<String>,
214}
215
216impl Summary {
217 #[must_use]
219 pub fn new(format: Format, version: impl Into<String>) -> Self {
220 Self {
221 format,
222 version: version.into(),
223 is_3d: None,
224 units: None,
225 codepage: None,
226 application: None,
227 }
228 }
229
230 #[must_use]
232 pub fn with_is_3d(mut self, is_3d: Option<bool>) -> Self {
233 self.is_3d = is_3d;
234 self
235 }
236
237 #[must_use]
239 pub fn with_units(mut self, units: Option<Units>) -> Self {
240 self.units = units;
241 self
242 }
243
244 #[must_use]
246 pub fn with_codepage(mut self, codepage: Option<String>) -> Self {
247 self.codepage = codepage;
248 self
249 }
250
251 #[must_use]
253 pub fn with_application(mut self, application: Option<String>) -> Self {
254 self.application = application;
255 self
256 }
257}
258
259#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
261#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
262#[serde(rename_all = "snake_case")]
263#[non_exhaustive]
264pub enum Format {
265 #[default]
267 Unknown,
268 Dwg,
270 Dxf,
272 DgnV7,
274 DgnV8,
276 CityGml,
278}
279
280#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
282#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
283#[non_exhaustive]
284pub struct Units {
285 pub unit: LengthUnit,
287 pub meters_per_unit: Option<f64>,
290}
291
292#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
294#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
295#[serde(rename_all = "snake_case")]
296#[allow(missing_docs)]
297pub enum LengthUnit {
298 #[default]
299 Unitless,
300 Inch,
301 Foot,
302 Mile,
303 Millimeter,
304 Centimeter,
305 Meter,
306 Kilometer,
307 Microinch,
308 Mil,
309 Yard,
310 Angstrom,
311 Nanometer,
312 Micrometer,
313 Decimeter,
314 Decameter,
315 Hectometer,
316 Gigameter,
317 AstronomicalUnit,
318 LightYear,
319 Parsec,
320 UsSurveyFoot,
321 Custom,
323}
324
325#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
327#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
328#[non_exhaustive]
329pub struct Layer {
330 pub name: String,
332 pub id: Option<u64>,
334 pub color: Color,
336 pub linetype: Option<String>,
338 pub lineweight: Lineweight,
340 pub visible: bool,
342 pub frozen: bool,
344 pub locked: bool,
346 pub plottable: bool,
348 pub description: Option<String>,
350 pub props: Props,
352}
353
354#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
356#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
357#[non_exhaustive]
358pub struct Linetype {
359 pub name: String,
361 pub description: Option<String>,
363 pub pattern: Vec<f64>,
365 pub props: Props,
367}
368
369#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
371#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
372#[non_exhaustive]
373pub struct TextStyle {
374 pub name: String,
376 pub font: Option<String>,
378 pub height: f64,
380 pub width_factor: f64,
382 pub oblique: f64,
384 pub props: Props,
386}
387
388#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
390#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
391#[non_exhaustive]
392pub struct Block {
393 pub name: String,
395 pub id: Option<u64>,
397 pub base_point: Point3,
399 pub entities: Vec<Entity>,
401 pub is_xref: bool,
403 pub xref_path: Option<String>,
405 pub description: Option<String>,
407 pub props: Props,
409}
410
411#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
413#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
414#[non_exhaustive]
415pub struct Model {
416 pub name: String,
418 pub id: Option<u64>,
420 pub kind: ModelKind,
422 pub is_3d: bool,
424 pub entities: Vec<Entity>,
426 pub props: Props,
428}
429
430#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
432#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
433#[serde(rename_all = "snake_case")]
434#[non_exhaustive]
435pub enum ModelKind {
436 #[default]
438 Model,
439 Layout,
441 Sheet,
443}
444
445#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
447#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
448#[non_exhaustive]
449pub struct Entity {
450 pub id: Option<u64>,
452 pub layer: Option<String>,
454 pub color: Color,
456 pub linetype: Option<String>,
458 pub lineweight: Lineweight,
460 pub visible: bool,
462 pub kind: EntityKind,
464 pub attributes: Vec<Attribute>,
466 pub props: Props,
468 #[serde(default, skip_serializing_if = "Option::is_none")]
470 pub raw: Option<Raw>,
471}
472
473impl Entity {
474 pub fn new(kind: EntityKind) -> Self {
476 Self {
477 id: None,
478 layer: None,
479 color: Color::ByLayer,
480 linetype: None,
481 lineweight: Lineweight::ByLayer,
482 visible: true,
483 kind,
484 attributes: Vec::new(),
485 props: Props::new(),
486 raw: None,
487 }
488 }
489
490 pub fn point(position: Point3) -> Self {
492 Self::new(EntityKind::Point { position })
493 }
494
495 pub fn line(start: Point3, end: Point3) -> Self {
497 Self::new(EntityKind::Line { start, end })
498 }
499
500 pub fn polyline(points: impl IntoIterator<Item = Point3>, closed: bool) -> Self {
502 Self::new(EntityKind::Polyline {
503 vertices: points.into_iter().map(Vertex::at).collect(),
504 closed,
505 normal: Vec3::Z,
506 })
507 }
508
509 pub fn circle(center: Point3, radius: f64) -> Self {
511 Self::new(EntityKind::Circle {
512 center,
513 radius,
514 normal: Vec3::Z,
515 })
516 }
517
518 pub fn arc(center: Point3, radius: f64, start_angle: f64, end_angle: f64) -> Self {
520 Self::new(EntityKind::Arc {
521 center,
522 radius,
523 start_angle,
524 end_angle,
525 normal: Vec3::Z,
526 })
527 }
528
529 pub fn text(position: Point3, height: f64, value: impl Into<String>) -> Self {
531 Self::new(EntityKind::Text {
532 position,
533 end_point: None,
534 height,
535 rotation: 0.0,
536 width_factor: 1.0,
537 oblique: 0.0,
538 value: value.into(),
539 style: None,
540 halign: HAlign::Left,
541 valign: VAlign::Baseline,
542 normal: Vec3::Z,
543 })
544 }
545
546 pub fn mtext(position: Point3, height: f64, plain: impl Into<String>) -> Self {
548 let plain = plain.into();
549 let value = plain.replace('\n', "\\P");
550 Self::new(EntityKind::MText {
551 position,
552 height,
553 width: 0.0,
554 rotation: 0.0,
555 value,
556 plain,
557 style: None,
558 attachment: Attachment::TopLeft,
559 line_spacing: 1.0,
560 normal: Vec3::Z,
561 })
562 }
563
564 pub fn insert(block: impl Into<String>, position: Point3) -> Self {
566 Self::new(EntityKind::Insert {
567 block: block.into(),
568 position,
569 scale: Vec3::ONE,
570 rotation: 0.0,
571 normal: Vec3::Z,
572 columns: 1,
573 rows: 1,
574 column_spacing: 0.0,
575 row_spacing: 0.0,
576 })
577 }
578
579 pub fn on_layer(mut self, layer: impl Into<String>) -> Self {
581 self.layer = Some(layer.into());
582 self
583 }
584
585 pub fn with_color(mut self, color: Color) -> Self {
587 self.color = color;
588 self
589 }
590
591 pub fn with_linetype(mut self, linetype: impl Into<String>) -> Self {
593 self.linetype = Some(linetype.into());
594 self
595 }
596
597 pub fn with_lineweight(mut self, lineweight: Lineweight) -> Self {
599 self.lineweight = lineweight;
600 self
601 }
602
603 pub fn with_visible(mut self, visible: bool) -> Self {
605 self.visible = visible;
606 self
607 }
608
609 pub fn with_prop(mut self, key: impl Into<String>, value: impl Into<Value>) -> Self {
611 self.props.insert(key.into(), value.into());
612 self
613 }
614
615 pub fn with_attribute(mut self, tag: impl Into<String>, value: impl Into<Value>) -> Self {
617 self.attributes.push(Attribute {
618 tag: tag.into(),
619 value: value.into(),
620 invisible: true,
621 ..Attribute::default()
622 });
623 self
624 }
625}
626
627#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
629#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
630#[serde(tag = "type", rename_all = "snake_case")]
631#[non_exhaustive]
632pub enum EntityKind {
633 Point {
635 position: Point3,
637 },
638 Line {
640 start: Point3,
642 end: Point3,
644 },
645 Polyline {
648 vertices: Vec<Vertex>,
650 closed: bool,
652 normal: Vec3,
655 },
656 Circle {
658 center: Point3,
660 radius: f64,
662 normal: Vec3,
664 },
665 Arc {
667 center: Point3,
669 radius: f64,
671 start_angle: f64,
673 end_angle: f64,
675 normal: Vec3,
677 },
678 Ellipse {
680 center: Point3,
682 major_axis: Vec3,
684 ratio: f64,
686 start_param: f64,
688 end_param: f64,
690 normal: Vec3,
692 },
693 Spline {
695 degree: u32,
697 knots: Vec<f64>,
699 control_points: Vec<Point3>,
701 weights: Vec<f64>,
703 fit_points: Vec<Point3>,
705 closed: bool,
707 },
708 Text {
710 position: Point3,
714 end_point: Option<Point3>,
716 height: f64,
718 rotation: f64,
720 width_factor: f64,
722 oblique: f64,
724 value: String,
726 style: Option<String>,
728 halign: HAlign,
730 valign: VAlign,
732 normal: Vec3,
734 },
735 MText {
737 position: Point3,
739 height: f64,
741 width: f64,
743 rotation: f64,
745 value: String,
747 plain: String,
749 style: Option<String>,
751 attachment: Attachment,
753 line_spacing: f64,
755 normal: Vec3,
757 },
758 Insert {
761 block: String,
763 position: Point3,
765 scale: Vec3,
767 rotation: f64,
769 normal: Vec3,
771 columns: u32,
773 rows: u32,
775 column_spacing: f64,
777 row_spacing: f64,
779 },
780 Hatch {
782 loops: Vec<HatchLoop>,
784 solid: bool,
786 pattern: Option<String>,
788 pattern_scale: f64,
790 pattern_angle: f64,
792 normal: Vec3,
794 },
795 Dimension {
797 dimension_type: DimensionType,
799 points: Vec<Point3>,
801 text_position: Option<Point3>,
803 measurement: Option<f64>,
805 text: Option<String>,
807 block: Option<String>,
809 style: Option<String>,
811 },
812 Face {
814 points: Vec<Point3>,
816 filled: bool,
818 },
819 Polygon {
822 exterior: Vec<Point3>,
824 interiors: Vec<Vec<Point3>>,
826 },
827 Leader {
829 vertices: Vec<Point3>,
831 arrowhead: bool,
833 },
834 Image {
836 path: Option<String>,
838 position: Point3,
840 u_vector: Vec3,
842 v_vector: Vec3,
844 size_px: Option<[u32; 2]>,
846 },
847 Viewport {
849 center: Point3,
851 width: f64,
853 height: f64,
855 view_center: Point3,
857 view_height: f64,
859 },
860 Mesh {
862 vertices: Vec<Point3>,
864 faces: Vec<Vec<u32>>,
866 },
867 Group {
870 group_kind: GroupKind,
872 name: Option<String>,
874 origin: Option<Point3>,
876 children: Vec<Entity>,
878 },
879 Unknown {
881 type_name: String,
883 },
884}
885
886#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
888#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
889pub struct Vertex {
890 pub position: Point3,
892 pub bulge: f64,
894 pub start_width: f64,
896 pub end_width: f64,
898}
899
900impl Vertex {
901 pub fn at(position: Point3) -> Self {
903 Self {
904 position,
905 ..Self::default()
906 }
907 }
908}
909
910#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
912#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
913#[serde(rename_all = "snake_case")]
914#[allow(missing_docs)]
915pub enum HAlign {
916 #[default]
917 Left,
918 Center,
919 Right,
920 Aligned,
921 Middle,
922 Fit,
923}
924
925#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
927#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
928#[serde(rename_all = "snake_case")]
929#[allow(missing_docs)]
930pub enum VAlign {
931 #[default]
932 Baseline,
933 Bottom,
934 Middle,
935 Top,
936}
937
938#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
940#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
941#[serde(rename_all = "snake_case")]
942#[allow(missing_docs)]
943pub enum Attachment {
944 #[default]
945 TopLeft,
946 TopCenter,
947 TopRight,
948 MiddleLeft,
949 MiddleCenter,
950 MiddleRight,
951 BottomLeft,
952 BottomCenter,
953 BottomRight,
954}
955
956#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
958#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
959#[serde(rename_all = "snake_case")]
960#[allow(missing_docs)]
961pub enum DimensionType {
962 #[default]
963 Linear,
964 Aligned,
965 Angular,
966 Angular3Point,
967 Diameter,
968 Radius,
969 Ordinate,
970 ArcLength,
971 Other,
972}
973
974#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
976#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
977#[serde(rename_all = "snake_case")]
978#[non_exhaustive]
979pub enum GroupKind {
980 ComplexChain,
982 ComplexShape,
984 TextNode,
986 Cell,
988 #[default]
990 Other,
991}
992
993#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
995#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
996pub struct HatchLoop {
997 pub edges: Vec<HatchEdge>,
999 pub external: bool,
1001}
1002
1003#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1005#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1006#[serde(tag = "type", rename_all = "snake_case")]
1007#[allow(missing_docs)]
1008pub enum HatchEdge {
1009 Line {
1010 start: Point3,
1011 end: Point3,
1012 },
1013 Arc {
1014 center: Point3,
1015 radius: f64,
1016 start_angle: f64,
1017 end_angle: f64,
1018 ccw: bool,
1019 },
1020 Ellipse {
1021 center: Point3,
1022 major_axis: Vec3,
1023 ratio: f64,
1024 start_param: f64,
1025 end_param: f64,
1026 ccw: bool,
1027 },
1028 Spline {
1029 degree: u32,
1030 knots: Vec<f64>,
1031 control_points: Vec<Point3>,
1032 weights: Vec<f64>,
1033 },
1034 Polyline {
1035 vertices: Vec<Vertex>,
1036 closed: bool,
1037 },
1038}
1039
1040#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
1042#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1043#[serde(tag = "type", rename_all = "snake_case")]
1044pub enum Color {
1045 #[default]
1047 ByLayer,
1048 ByBlock,
1050 Aci {
1053 index: u8,
1055 },
1056 Rgb {
1058 r: u8,
1060 g: u8,
1062 b: u8,
1064 },
1065}
1066
1067impl Color {
1068 pub const fn aci(index: u8) -> Self {
1070 Self::Aci { index }
1071 }
1072
1073 pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
1075 Self::Rgb { r, g, b }
1076 }
1077}
1078
1079#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
1081#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1082#[serde(tag = "type", content = "value", rename_all = "snake_case")]
1083pub enum Lineweight {
1084 #[default]
1086 ByLayer,
1087 ByBlock,
1089 Default,
1091 Millimeters(f64),
1093}
1094
1095#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
1097#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1098#[non_exhaustive]
1099pub struct Attribute {
1100 pub tag: String,
1102 pub value: Value,
1104 pub set: Option<String>,
1106 pub position: Option<Point3>,
1108 pub invisible: bool,
1110 #[serde(default, skip_serializing_if = "Option::is_none")]
1113 pub layer: Option<String>,
1114 #[serde(default, skip_serializing_if = "Option::is_none")]
1116 pub display: Option<AttributeDisplay>,
1117 #[serde(default, skip_serializing_if = "Option::is_none")]
1122 pub color: Option<Color>,
1123 #[serde(default, skip_serializing_if = "Option::is_none")]
1126 pub lineweight: Option<Lineweight>,
1127 #[serde(default, skip_serializing_if = "Props::is_empty")]
1129 pub props: Props,
1130}
1131
1132#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
1134#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1135#[non_exhaustive]
1136pub struct AttributeDisplay {
1137 pub height: f64,
1139 pub width: f64,
1141 pub style: Option<String>,
1143 pub halign: HAlign,
1145 pub valign: VAlign,
1147 pub rotation: f64,
1149}
1150
1151#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
1153#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1154#[serde(untagged)]
1155#[non_exhaustive]
1156pub enum Value {
1157 #[default]
1159 Null,
1160 Bool(bool),
1162 Int(i64),
1164 Float(f64),
1166 Text(String),
1168 List(Vec<Value>),
1170 Bytes(Vec<u8>),
1172}
1173
1174impl Value {
1175 pub const fn is_null(&self) -> bool {
1177 matches!(self, Self::Null)
1178 }
1179
1180 pub const fn as_bool(&self) -> Option<bool> {
1182 match self {
1183 Self::Bool(v) => Some(*v),
1184 _ => None,
1185 }
1186 }
1187
1188 pub const fn as_i64(&self) -> Option<i64> {
1190 match self {
1191 Self::Int(v) => Some(*v),
1192 _ => None,
1193 }
1194 }
1195
1196 pub const fn as_f64(&self) -> Option<f64> {
1198 match self {
1199 Self::Float(v) => Some(*v),
1200 Self::Int(v) => Some(*v as f64),
1201 _ => None,
1202 }
1203 }
1204
1205 pub fn as_str(&self) -> Option<&str> {
1207 match self {
1208 Self::Text(s) => Some(s.as_str()),
1209 _ => None,
1210 }
1211 }
1212
1213 pub fn as_list(&self) -> Option<&[Value]> {
1215 match self {
1216 Self::List(items) => Some(items.as_slice()),
1217 _ => None,
1218 }
1219 }
1220
1221 pub fn as_bytes(&self) -> Option<&[u8]> {
1223 match self {
1224 Self::Bytes(bytes) => Some(bytes.as_slice()),
1225 _ => None,
1226 }
1227 }
1228}
1229
1230impl From<bool> for Value {
1231 fn from(v: bool) -> Self {
1232 Value::Bool(v)
1233 }
1234}
1235impl From<i64> for Value {
1236 fn from(v: i64) -> Self {
1237 Value::Int(v)
1238 }
1239}
1240impl From<i32> for Value {
1241 fn from(v: i32) -> Self {
1242 Value::Int(i64::from(v))
1243 }
1244}
1245impl From<i16> for Value {
1246 fn from(v: i16) -> Self {
1247 Value::Int(i64::from(v))
1248 }
1249}
1250impl From<u32> for Value {
1251 fn from(v: u32) -> Self {
1252 Value::Int(i64::from(v))
1253 }
1254}
1255impl From<u16> for Value {
1256 fn from(v: u16) -> Self {
1257 Value::Int(i64::from(v))
1258 }
1259}
1260impl From<u8> for Value {
1261 fn from(v: u8) -> Self {
1262 Value::Int(i64::from(v))
1263 }
1264}
1265impl From<f64> for Value {
1266 fn from(v: f64) -> Self {
1267 Value::Float(v)
1268 }
1269}
1270impl From<f32> for Value {
1271 fn from(v: f32) -> Self {
1272 Value::Float(f64::from(v))
1273 }
1274}
1275impl From<String> for Value {
1276 fn from(v: String) -> Self {
1277 Value::Text(v)
1278 }
1279}
1280impl From<&str> for Value {
1281 fn from(v: &str) -> Self {
1282 Value::Text(v.to_owned())
1283 }
1284}
1285impl From<Vec<u8>> for Value {
1286 fn from(v: Vec<u8>) -> Self {
1287 Value::Bytes(v)
1288 }
1289}
1290impl From<Vec<Value>> for Value {
1291 fn from(v: Vec<Value>) -> Self {
1292 Value::List(v)
1293 }
1294}
1295
1296#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1298#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1299pub struct Raw {
1300 pub type_code: u32,
1302 pub bytes: Vec<u8>,
1304}
1305
1306#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
1308#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
1309#[non_exhaustive]
1310pub struct Warning {
1311 pub code: String,
1313 pub message: String,
1315 pub offset: Option<u64>,
1317 pub object: Option<u64>,
1319}