code_blocks_server/
types.rs

1use code_blocks::{Block, BlockTree};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug, Default)]
5#[serde(rename_all = "camelCase")]
6pub struct BlockLocation {
7    pub start_byte: usize,
8    pub end_byte: usize,
9    pub start_row: usize,
10    pub start_col: usize,
11    pub end_row: usize,
12    pub end_col: usize,
13}
14
15impl From<Block<'_>> for BlockLocation {
16    fn from(value: Block) -> Self {
17        BlockLocation::from(&value)
18    }
19}
20
21impl From<&Block<'_>> for BlockLocation {
22    fn from(value: &Block) -> Self {
23        let (head, tail) = value.head_tail();
24
25        Self {
26            start_byte: head.start_byte(),
27            end_byte: tail.end_byte(),
28            start_row: head.start_position().row,
29            start_col: head.start_position().column,
30            end_row: tail.end_position().row,
31            end_col: tail.end_position().column,
32        }
33    }
34}
35
36#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone, Hash, Debug, Default)]
37#[serde(rename_all = "camelCase")]
38pub struct BlockLocationTree {
39    pub block: BlockLocation,
40    pub children: Vec<BlockLocationTree>,
41}
42
43impl From<BlockTree<'_>> for BlockLocationTree {
44    fn from(value: BlockTree<'_>) -> Self {
45        Self {
46            block: value.block.into(),
47            children: value.children.iter().map(|c| c.into()).collect(),
48        }
49    }
50}
51
52impl From<&BlockTree<'_>> for BlockLocationTree {
53    fn from(value: &BlockTree<'_>) -> Self {
54        Self {
55            block: value.block.clone().into(),
56            children: value.children.iter().map(Into::into).collect(),
57        }
58    }
59}